Skip to main content

Command Palette

Search for a command to run...

SQL intro

Updated
8 min readView as Markdown

What is SQL?

  • SQL means Structured Query Language.

  • It is used to work with data in databases.

  • With SQL, you can get, add, change, and remove data in a database.

  • SQL became a standard language in 1986 (ANSI) and 1987 (ISO).


What Can SQL Do?

  • SQL can ask questions (queries) to get data from a database.

  • It can retrieve (fetch) data.

  • It can insert (add) new data.

  • It can update existing data.

  • It can delete unwanted data.

  • It can create a new database.

  • It can create tables in a database.

  • It can create stored procedures (pre-written blocks of code).

  • It can create views (virtual tables made from other tables).

  • It can set permissions (control who can do what with the data).


SQL is a Standard, But...

  • Even though SQL is a standard language, different database companies (like MySQL, Oracle, etc.) have slightly different versions.

  • They all support the main commands like SELECT, INSERT, DELETE, etc.

  • But they may also have extra features specific to their own software.


Using SQL on a Website

If you want your website to show data from a database, you’ll need:

  1. A database program (like MySQL, SQL Server, MS Access).

  2. A server-side language (like PHP or ASP) to run SQL commands.

  3. SQL to get the data from the database.

  4. HTML and CSS to display and style the data on the web page.


What is RDBMS?

  • RDBMS means Relational Database Management System.

  • It is the system behind all modern databases like MySQL, Oracle, and MS SQL Server.

  • Data in RDBMS is stored in tables.


What is a Table?

  • A table is like an Excel sheet.

  • It has columns (fields) and rows (records).

  • Each column holds one type of information (like name, address, etc.).

  • Each row is one complete entry (like one customer).

Example table: Customers
It may have columns like:

  • CustomerID

  • CustomerName

  • ContactName

  • Address

  • City

  • PostalCode

  • Country


In Short:

  • A field is a column in a table.

  • A record is a row in a table.

  • A column is vertical — it holds the same kind of data for all records.

  • A row is horizontal — it shows all the data for one person or item.

💬 SQL Statements — What Are They?

  • Most things you want to do with a database are done using SQL statements.

  • These are commands written in English-like keywords, so they’re not hard to read.


✅ Example

This SQL command shows all the data from a table named "Customers":

SELECT * FROM Customers;
  • SELECT means get data.

  • * means all columns.

  • FROM Customers means from the Customers table.


📘 What You Will Learn

In this tutorial, you'll learn about many different types of SQL statements and how to use them to work with data.


🗃️ What Are Database Tables?

  • A database usually has one or more tables.

  • Each table has a name (like Customers or Orders).

  • Tables store data in rows and columns.

For example, this is part of a Customers table:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden
  • This table has 5 rows (one for each customer).

  • It has 7 columns: CustomerID, CustomerName, ContactName, Address, City, PostalCode, and Country.


📌 Things to Remember

  • SQL commands are not case-sensitive:
    Writing select or SELECT means the same thing.
    (But in tutorials, we use capital letters for keywords to make them easier to see.)

  • Semicolon ; at the end of SQL commands:

    • Some databases need it, some don’t.

    • It is used to separate multiple SQL commands.

    • In this tutorial, we will use semicolons.


📋 Most Common SQL Commands

CommandWhat It Does
SELECTGets data from a table
UPDATEChanges existing data
DELETERemoves data from a table
INSERT INTOAdds new data to a table
CREATE DATABASEMakes a new database
ALTER DATABASEChanges a database
CREATE TABLEMakes a new table
ALTER TABLEChanges an existing table
DROP TABLEDeletes a table
CREATE INDEXCreates an index (used for faster search)
DROP INDEXDeletes an index

❓Practice Question:

Which SQL command shows all records from the table "Customers"?

A. SELECT FROM Customers;
B. SELECT ALL FROM Customers;
C. ✅ SELECT * FROM Customers;
D. GET ALL FROM Customers;

✔️ Correct Answer: C

🟡 What is the SELECT Statement in SQL?

  • The SELECT statement is used to get data from a table in a database.

  • It tells the database: “Show me these columns from this table.”


✅ Example

Let’s say you want to get only the Customer Name and City from the Customers table:

SELECT CustomerName, City FROM Customers;

🧾 Syntax of SELECT:

SELECT column1, column2, ...
FROM table_name;
  • column1, column2: The names of the columns you want to see.

  • table_name: The name of the table you're selecting from.


📊 Sample Table: Customers

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

🔄 Select All Columns

If you want to get all the data from the table (not just specific columns), use:

SELECT * FROM Customers;
  • * means "all columns".

  • This command gives you every column and every row from the table.


🟢 INSERT Command in SQL

  • The INSERT INTO statement is used to add new data (a row) to a table.

✅ Example

Add a new customer to the Customers table:

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Singh Store', 'Amit Singh', 'Main Street 21', 'Delhi', '110001', 'India');

🧾 Syntax of INSERT:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
  • table_name: The name of the table you're adding to.

  • (column1, column2, ...): The names of the columns you're inserting data into.

  • (value1, value2, ...): The actual data you want to add.

🔷 What is SELECT DISTINCT in SQL?

  • The SELECT DISTINCT command is used when you want to get only different values from a column — not repeated ones.

  • It helps you to remove duplicates and get unique results.


🎯 Why Use DISTINCT?

Imagine a table where many customers are from the same country, like "Mexico".
If you just want to know which countries your customers are from (not how many), you don't want to see "Mexico" 10 times — just once.

That’s when you use DISTINCT.


✅ Simple Example

Table: Customers

CustomerIDCustomerNameCountry
1Alfreds FutterkisteGermany
2Ana TrujilloMexico
3Antonio MorenoMexico
4Around the HornUK
5Berglunds snabbköpSweden

Normal SELECT:

SELECT Country FROM Customers;

📌 This will show:

Germany  
Mexico  
Mexico  
UK  
Sweden

🙁 See? "Mexico" is repeated.


Using DISTINCT:

SELECT DISTINCT Country FROM Customers;

✅ This will show:

Germany  
Mexico  
UK  
Sweden

✅ "Mexico" comes only once now. Much cleaner!


🧾 Easy Syntax

SELECT DISTINCT column1, column2, ...
FROM table_name;
  • DISTINCT removes rows with same values in the selected columns.

  • You can use it for one or more columns.


🧠 Real-life Example

Let's say you have this student table:

IDNameClass
1Raj10
2Simran9
3Amit10
4Riya8

You want to see which different classes students are in:

SELECT DISTINCT Class FROM Students;

Result:

10  
9  
8

🔢 Count How Many Unique Values?

If you want to know how many different countries there are in the Customers table, use:

SELECT COUNT(DISTINCT Country) FROM Customers;

🎯 This gives you a number — the count of unique countries.


❗ For Microsoft Access Users

Microsoft Access does not support COUNT(DISTINCT column) directly.
So instead, use this trick:

SELECT COUNT(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers);

📌 This creates a mini-table of unique countries, and then counts them.


💡 Summary

CommandMeaning
SELECT Country FROM Customers;Shows all countries (with duplicates)
SELECT DISTINCT Country FROM Customers;Shows only different countries
SELECT COUNT(DISTINCT Country)Tells you how many different countries there are
SELECT DISTINCT column1, column2

More from this blog

Amit singh's blog

235 posts