Learn SQL with Examples
Every clause you'll actually use in day-to-day SQL — no theory dumps, just short examples against one small, consistent dataset. Follow along top to bottom, or jump to a section from the nav.
Sample data — used in every example below
employees
| id | name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Asha Verma | Engineering | 95,000 | 2019-03-12 | NULL |
| 2 | Rahul Singh | Engineering | 82,000 | 2020-07-01 | 1 |
| 3 | Meera Nair | Sales | 65,000 | 2021-01-15 | 4 |
| 4 | Vikram Rao | Sales | 90,000 | 2018-11-20 | NULL |
| 5 | Priya Paswan | Marketing | 72,000 | 2022-05-30 | 6 |
| 6 | Arjun Kisku | Marketing | 88,000 | 2017-09-05 | NULL |
departments
| id | name | location |
|---|---|---|
| 1 | Engineering | Bangalore |
| 2 | Sales | Mumbai |
| 3 | Marketing | Delhi |
| 4 | HR | Pune |
| 5 | Finance | Chennai |
What is SQL
SQL is the language you use to ask a database questions — "give me these rows," "total this column," "update that record." You describe what you want; the database figures out how to get it.
SELECT & FROM
Pick which columns you want, and which table to pull them from.
| name | salary |
|---|---|
| Asha Verma | 95,000 |
| Rahul Singh | 82,000 |
| Meera Nair | 65,000 |
| Vikram Rao | 90,000 |
| Priya Paswan | 72,000 |
| Arjun Kisku | 88,000 |
Use SELECT * to get every column instead of naming them — try changing the query above to SELECT * FROM employees; and rerun it.
WHERE
Filter which rows come back, using comparisons. Combine conditions with AND / OR.
| name | department | salary |
|---|---|---|
| Asha Verma | Engineering | 95,000 |
More ways to filter
These are fragments, not full queries — not runnable on their own, but each is a valid WHERE line you can paste into the query above (right after the table name).
WHERE department IN ('Sales', 'Marketing') -- matches either value
WHERE salary BETWEEN 70000 AND 90000 -- inclusive range
WHERE name LIKE 'A%' -- starts with "A"
WHERE manager_id IS NULL -- no manager on fileORDER BY
Sort the results. ASC (ascending, the default) goes low to high; DESC goes high to low.
| name | salary |
|---|---|
| Asha Verma | 95,000 |
| Vikram Rao | 90,000 |
| Arjun Kisku | 88,000 |
| Rahul Singh | 82,000 |
| Priya Paswan | 72,000 |
| Meera Nair | 65,000 |
LIMIT
Only return the first N rows — the standard way to answer "top 5" style questions.
| name | salary |
|---|---|
| Asha Verma | 95,000 |
| Vikram Rao | 90,000 |
| Arjun Kisku | 88,000 |
DISTINCT
Remove duplicate values from the result, keeping only unique rows.
| department |
|---|
| Engineering |
| Sales |
| Marketing |
Aggregates
Collapse a column into a single summary number: COUNT, SUM, AVG, MIN, MAX.
| total_employees | total_salary | avg_salary | min_salary | max_salary |
|---|---|---|---|---|
| 6 | 492,000 | 82,000 | 65,000 | 95,000 |
GROUP BY
Pair an aggregate with GROUP BY to get one summary row per category instead of one grand total.
| department | num_employees | avg_salary |
|---|---|---|
| Engineering | 2 | 88,500 |
| Marketing | 2 | 80,000 |
| Sales | 2 | 77,500 |
HAVING
WHERE filters individual rows before grouping. HAVING filters the groups after aggregation — use it when the condition involves an aggregate like AVG or COUNT.
| department | avg_salary |
|---|---|
| Engineering | 88,500 |
Sales (77,500) and Marketing (exactly 80,000) both get dropped — HAVING ran after the averages were calculated, and neither is greater than 80,000.
JOINs
INNER JOIN keeps only rows that match in both tables. LEFT JOIN keeps every row from the left table regardless, filling in NULL where there's no match.
INNER JOIN
| name | department | location |
|---|---|---|
| Asha Verma | Engineering | Bangalore |
| Rahul Singh | Engineering | Bangalore |
| Meera Nair | Sales | Mumbai |
| Vikram Rao | Sales | Mumbai |
| Priya Paswan | Marketing | Delhi |
| Arjun Kisku | Marketing | Delhi |
LEFT JOIN — departments with no employees still show up
| department | employee |
|---|---|
| Engineering | Asha Verma |
| Engineering | Rahul Singh |
| Sales | Meera Nair |
| Sales | Vikram Rao |
| Marketing | Arjun Kisku |
| Marketing | Priya Paswan |
| HR | NULL |
| Finance | NULL |
Aliases
AS renames a column or table in the output. It's purely cosmetic — makes results easier to read, and (as you saw above) makes joins easier to write.
| employee_name | monthly_pay |
|---|---|
| Meera Nair | 65,000 |
| Vikram Rao | 90,000 |
CASE WHEN
Add if/else logic right inside a query — handy for labeling or bucketing values.
| name | salary | salary_band |
|---|---|---|
| Asha Verma | 95,000 | High |
| Rahul Singh | 82,000 | Medium |
| Meera Nair | 65,000 | Entry |
| Vikram Rao | 90,000 | High |
| Priya Paswan | 72,000 | Entry |
| Arjun Kisku | 88,000 | Medium |
Subquery
A query nested inside another query — often used to compare each row against an aggregate, like "above average."
| name | salary |
|---|---|
| Asha Verma | 95,000 |
| Vikram Rao | 90,000 |
| Arjun Kisku | 88,000 |
The inner query computes the average salary (82,000) first; the outer query then keeps only rows above it. Rahul, at exactly 82,000, doesn't qualify.
Modify data
Three statements change data instead of reading it: add a row, change one, remove one. Each example below runs against its own private copy of the data — go ahead and run them, nothing you do here touches any other example or exercise on this page. Hit "Reset data" to put a block back the way it started.
INSERT — add a row
employees right now
| id | name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Asha Verma | Engineering | 95,000 | 2019-03-12 | NULL |
| 2 | Rahul Singh | Engineering | 82,000 | 2020-07-01 | 1 |
| 3 | Meera Nair | Sales | 65,000 | 2021-01-15 | 4 |
| 4 | Vikram Rao | Sales | 90,000 | 2018-11-20 | NULL |
| 5 | Priya Paswan | Marketing | 72,000 | 2022-05-30 | 6 |
| 6 | Arjun Kisku | Marketing | 88,000 | 2017-09-05 | NULL |
UPDATE — change existing rows
employees right now
| id | name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Asha Verma | Engineering | 95,000 | 2019-03-12 | NULL |
| 2 | Rahul Singh | Engineering | 82,000 | 2020-07-01 | 1 |
| 3 | Meera Nair | Sales | 65,000 | 2021-01-15 | 4 |
| 4 | Vikram Rao | Sales | 90,000 | 2018-11-20 | NULL |
| 5 | Priya Paswan | Marketing | 72,000 | 2022-05-30 | 6 |
| 6 | Arjun Kisku | Marketing | 88,000 | 2017-09-05 | NULL |
DELETE — remove rows
employees right now
| id | name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Asha Verma | Engineering | 95,000 | 2019-03-12 | NULL |
| 2 | Rahul Singh | Engineering | 82,000 | 2020-07-01 | 1 |
| 3 | Meera Nair | Sales | 65,000 | 2021-01-15 | 4 |
| 4 | Vikram Rao | Sales | 90,000 | 2018-11-20 | NULL |
| 5 | Priya Paswan | Marketing | 72,000 | 2022-05-30 | 6 |
| 6 | Arjun Kisku | Marketing | 88,000 | 2017-09-05 | NULL |
WHERE id = 6 line above and running it again to see exactly that happen (safely — it's a sandbox).Order of execution
A query reads top to bottom, but the database doesn't run it in that order. Knowing the real order explains quirks like why HAVING can use an aggregate but WHERE can't.
What each step actually does
- FROM — Load the table (or tables, if there's a JOIN).
- WHERE — Throw away rows that don't match. Nothing from SELECT exists yet, so you can't filter on a SELECT alias here.
- GROUP BY — Collapse the surviving rows into one row per group.
- HAVING — Throw away whole groups that don't match. Aggregates like COUNT(*) and AVG(...) work here because the grouping has already happened.
- SELECT — Compute the actual output columns, including anything you named with AS.
- ORDER BY — Sort the finished rows. SELECT has already run, so its aliases are available here.
- LIMIT — Cut the sorted list down to the number of rows you asked for.
Why this matters
Take salary_band from the CASE WHEN section — an alias created inside SELECT. By the execution order above, WHERE runs before SELECT, so in most databases (PostgreSQL, SQL Server, MySQL) a filter like this fails with "no such column: salary_band":
| name | salary | salary_band |
|---|---|---|
| Asha Verma | 95,000 | High |
| Vikram Rao | 90,000 | High |
SQLite is unusually lenient here — as a convenience, it lets WHERE reference a SELECT alias by quietly substituting the original expression back in, so the query above actually runs. It's a handy shortcut, but it's SQLite-specific: don't rely on it if your code also has to run against Postgres or MySQL. ORDER BY has no such ambiguity — it genuinely does run after SELECT in every engine, so the same alias works everywhere:
| name | salary | salary_band |
|---|---|---|
| Rahul Singh | 82,000 | Entry |
| Meera Nair | 65,000 | Entry |
| Priya Paswan | 72,000 | Entry |
| Arjun Kisku | 88,000 | Entry |
| Asha Verma | 95,000 | High |
| Vikram Rao | 90,000 | High |
Cheat sheet
Every clause from this page, in one table.
| Clause | What it does | Example |
|---|---|---|
| SELECT | Choose which columns to return | SELECT name, salary |
| FROM | Choose which table to read | FROM employees |
| WHERE | Filter individual rows | WHERE salary > 80000 |
| ORDER BY | Sort the results | ORDER BY salary DESC |
| LIMIT | Return only the first N rows | LIMIT 5 |
| DISTINCT | Remove duplicate rows | SELECT DISTINCT department |
| COUNT / SUM / AVG / MIN / MAX | Summarize a column into one number | AVG(salary) |
| GROUP BY | One summary row per group | GROUP BY department |
| HAVING | Filter groups after aggregation | HAVING COUNT(*) > 1 |
| INNER JOIN | Keep only matching rows from both tables | JOIN departments d ON e.department = d.name |
| LEFT JOIN | Keep all left-table rows, matched or not | LEFT JOIN employees e ON … |
| AS | Rename a column or table in the output | salary AS monthly_pay |
| CASE WHEN | If/else logic inside a query | CASE WHEN … THEN … ELSE … END |
| Subquery | A query nested inside another | WHERE salary > (SELECT AVG(salary) …) |
| INSERT | Add a new row | INSERT INTO employees (…) VALUES (…) |
| UPDATE | Change existing rows | UPDATE employees SET salary = 97000 WHERE id = 1 |
| DELETE | Remove rows | DELETE FROM employees WHERE id = 7 |
Console
A blank scratchpad against the same employees / departments data as the rest of the page. Try anything from above, or write your own.
Practice
Write a query, hit Run to see what comes back, then Check answer when you think it's right.
0/3 solved
Find every employee in the Sales department earning more than 70,000.
Show the number of employees and average salary per department, but only for departments with more than one employee.
List every department with its employees, including departments that have none.