Before you start
Nothing. This is the first SQL lesson.
It helps to know two words:
- A table is data in rows and columns, like a sheet in Excel. Each row is one thing (one customer, one order), and each column is one fact about it (their city, the order date).
- A database is a set of tables that belong together.
All lessons use the course's shop database, an online shop in India. In this lesson we use two of its tables:
| Table | One row is… | Some columns |
|---|---|---|
customers |
one customer (40 rows) | customer_id, first_name, city, signup_date, is_premium |
products |
one product we sell (20 rows) | product_id, product_name, category, price, stock |
You can run every query below yourself: open a practice question in this lesson and use Run.
What it does
SQL (say "sequel" or "S-Q-L") is the language for asking a database questions. A question written in SQL is called a query.
The most common query starts with SELECT. It says which columns you want and from which table. Think of a huge register book in a shop: SELECT is you telling the clerk "from the customer register, read me just the names and cities."
SELECT never changes the data. It only reads it and shows you a result, so you can't break anything by experimenting.
Analysts write SELECT queries all day: to look at data before analysing it, to pull a list for a manager, or to feed a dashboard. Every bigger query you'll learn later (filters, joins, grouping) is built on top of this one.
Step by step
Step 1: Ask for every column
The smallest useful query reads every column of a table. * means "all columns". LIMIT 5 says "show only 5 rows", so we don't flood the screen.
SELECT *
FROM products
LIMIT 5;
| product_id | product_name | category | price | stock |
|---|---|---|---|---|
| 1 | Wireless Earbuds | Electronics | 1999 | 45 |
| 2 | Smartwatch | Electronics | 4499 | 12 |
| 3 | USB-C Charger | Electronics | 799 | 120 |
| 4 | Bluetooth Speaker | Electronics | 2499 | 0 |
| 5 | Laptop Stand | Electronics | 1299.5 | 30 |
5 rows.
Read it as: select all columns from the products table, but only 5 rows. Notice the query ends with a semicolon ;. It marks the end of the query.
Step 2: Ask for only the columns you need
Usually you don't need every column. List the ones you want, separated by commas, in the order you want them:
SELECT product_name, price
FROM products
LIMIT 5;
| product_name | price |
|---|---|
| Bluetooth Speaker | 2499 |
| Laptop Stand | 1299.5 |
| Smartwatch | 4499 |
| USB-C Charger | 799 |
| Wireless Earbuds | 1999 |
5 rows.
The columns come back in the order you wrote them, not the order they're stored in.
Step 3: Calculate a new column
You can do arithmetic in SELECT. Prices in the shop don't include GST, so let's add 18%:
SELECT product_name,
price,
price * 1.18
FROM products
LIMIT 5;
| product_name | price | (price * 1.18) |
|---|---|---|
| Bluetooth Speaker | 2499 | 2948.82 |
| Laptop Stand | 1299.5 | 1533.41 |
| Smartwatch | 4499 | 5308.82 |
| USB-C Charger | 799 | 942.82 |
| Wireless Earbuds | 1999 | 2358.82 |
5 rows.
It works, but the new column has an ugly name. That's the next step.
Step 4: Name a column with AS
AS gives a column (or a calculation) a name in the result. This is called an alias:
SELECT product_name,
price,
ROUND(price * 1.18, 2) AS price_with_gst
FROM products
LIMIT 5;
| product_name | price | price_with_gst |
|---|---|---|
| Bluetooth Speaker | 2499 | 2948.82 |
| Laptop Stand | 1299.5 | 1533.41 |
| Smartwatch | 4499 | 5308.82 |
| USB-C Charger | 799 | 942.82 |
| Wireless Earbuds | 1999 | 2358.82 |
5 rows.
ROUND(x, 2) rounds to 2 decimal places, so paise don't run on forever. The alias only renames the column in this result. The table itself doesn't change.
Step 5: Remove repeated rows with DISTINCT
Which cities do our customers live in? Asking for city gives one row per customer, with lots of repeats. DISTINCT keeps only one copy of each:
SELECT DISTINCT city
FROM customers;
| city |
|---|
| Bengaluru |
| Chennai |
| Coimbatore |
| Delhi |
| Hyderabad |
| Kochi |
| Madurai |
| Mumbai |
| Pune |
| NULL |
10 rows.
Look closely: one row says NULL. That's a customer whose city we don't know. DISTINCT treats "unknown" as one value of its own and keeps it.
Step 6: Put the rows in a predictable order
Without instructions, a database returns rows in whatever order is quickest for it. That order can change. If you want "the first 5", you must say first by what. ORDER BY does that (it gets its own lesson soon):
SELECT product_name, price
FROM products
ORDER BY price DESC
LIMIT 3;
| product_name | price |
|---|---|
| Silk Saree | 5999 |
| Smartwatch | 4499 |
| Cricket Bat | 3299 |
3 rows.
DESC means largest first. This is the honest way to ask for "the 3 most expensive products".
Syntax
SELECT [DISTINCT] column_or_expression [AS alias], ...
FROM table_name
[ORDER BY column [ASC | DESC]]
[LIMIT n];
Parts in [square brackets] are optional.
| Part | Meaning |
|---|---|
SELECT * |
all columns. Fine for exploring, avoid in real reports |
SELECT a, b |
only these columns, in this order |
expression AS name |
a calculation or column with a new name in the result |
SELECT DISTINCT a |
one row per unique value of a |
SELECT DISTINCT a, b |
one row per unique combination of a and b |
ORDER BY a DESC |
sort by a, largest first (ASC, the default, is smallest first) |
LIMIT n |
return at most n rows |
The order of the parts matters: SELECT, then FROM, then ORDER BY, then LIMIT. SQL keywords aren't case-sensitive (select works too), but writing them in capitals makes queries easier to read.
Worked example
Question 1: The marketing team wants a price list for the Electronics category with GST included. Show the name, the price before tax, and the price with 18% GST.
We don't know how to keep only Electronics yet (that's the next lesson, WHERE). So for now, show every product with the category, and they can filter it in Excel:
SELECT category,
product_name,
price AS price_before_gst,
ROUND(price * 1.18, 2) AS price_with_gst
FROM products
ORDER BY category, product_name;
| category | product_name | price_before_gst | price_with_gst |
|---|---|---|---|
| Books | Learning SQL (Book) | 650 | 767 |
| Books | Notebook Pack | 249 | 293.82 |
| Books | Ponniyin Selvan Vol 1 | 450 | 531 |
| Books | Python Crash Course (Book) | 899 | 1060.82 |
| Clothing | Cotton Kurta | 1199 | 1414.82 |
| Clothing | Silk Saree | 5999 | 7078.82 |
| Electronics | Bluetooth Speaker | 2499 | 2948.82 |
| Electronics | Laptop Stand | 1299.5 | 1533.41 |
| Electronics | Smartwatch | 4499 | 5308.82 |
| Electronics | USB-C Charger | 799 | 942.82 |
Showing 10 of 20 rows.
In words: one row per product, sorted by category and then name, with both prices side by side.
Question 2: How many different categories do we sell, and what are they?
SELECT DISTINCT category
FROM products
ORDER BY category;
| category |
|---|
| Books |
| Clothing |
| Electronics |
| Home |
| Sports |
| NULL |
6 rows.
In words: count the rows in the result. That's the number of categories, and the rows are their names.
Question 3: Which combinations of city and premium membership exist among our customers?
SELECT DISTINCT city, is_premium
FROM customers
ORDER BY city, is_premium;
| city | is_premium |
|---|---|
| Bengaluru | false |
| Chennai | false |
| Chennai | true |
| Coimbatore | false |
| Coimbatore | true |
| Delhi | false |
| Hyderabad | false |
| Hyderabad | true |
| Kochi | true |
| Madurai | false |
Showing 10 of 15 rows.
In words: DISTINCT on two columns gives each pair once. A city appears twice if it has both premium and non-premium customers.
Common mistakes
Thinking
DISTINCTworks on just one column. Wrong idea:SELECT DISTINCT city, is_premiumgives one row per city. What happens: you get one row per unique pair of city and premium status. Fix: if you want one row per city, select onlycity.Using
LIMITto get "the top" rows withoutORDER BY. Wrong:SELECT product_name, price FROM products LIMIT 3;for "the 3 most expensive". What happens: you get any 3 rows, and they can change between runs. Fix:ORDER BY price DESC LIMIT 3.Single quotes around an alias. Wrong:
price * 1.18 AS 'price with gst'. What happens: in SQL, single quotes make a piece of text, not a name. Some databases fail, others behave oddly. Fix: use a simple name (price_with_gst), or double quotes for names with spaces:AS "Price with GST".Forgetting the commas between columns. Wrong:
SELECT product_name price FROM products;What happens: SQL readspriceas an alias, so you get one column calledpricethat holds the product names. Fix:SELECT product_name, price FROM products;Leaving
SELECT *in a finished report. What happens: it reads more data than needed (slow on big tables), and the report changes when someone adds a column to the table. Fix: list the columns you actually need.
Interview corner
What's the difference between SELECT DISTINCT a, b and SELECT DISTINCT a?
DISTINCT applies to the whole row. With two columns you get each unique pair; with one column you get each unique value.
Does DISTINCT remove NULLs?
No. All NULLs count as one value, so NULL appears once in the result. (But COUNT(DISTINCT a) ignores NULL. You'll see this in the aggregates lesson.)
Why might SELECT * … LIMIT 10 return different rows each time?
Without ORDER BY, the database can return rows in any order. Only ORDER BY guarantees an order.
Can you use an alias defined in SELECT in the same query?
In ORDER BY, yes. In WHERE, standard SQL says no, because WHERE runs before SELECT names its columns, and PostgreSQL, MySQL and SQL Server all give an error. DuckDB, which this course runs on, is friendlier and allows it. In an interview, give the standard answer: repeat the expression in WHERE.
Recap
SELECT columns FROM tablereads data. It never changes it.*means all columns. List the columns you need in real queries.ASnames a column or calculation in the result.DISTINCTkeeps one copy of each unique row, and treatsNULLas a value.LIMIT ncaps the rows. For "top N", always addORDER BY.