A 30-minute crash course

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

idnamedepartmentsalaryhire_datemanager_id
1Asha VermaEngineering95,0002019-03-12NULL
2Rahul SinghEngineering82,0002020-07-011
3Meera NairSales65,0002021-01-154
4Vikram RaoSales90,0002018-11-20NULL
5Priya PaswanMarketing72,0002022-05-306
6Arjun KiskuMarketing88,0002017-09-05NULL

departments

idnamelocation
1EngineeringBangalore
2SalesMumbai
3MarketingDelhi
4HRPune
5FinanceChennai
01

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.

02

SELECT & FROM

Pick which columns you want, and which table to pull them from.

or press Ctrl/Cmd + Enter
6 rows
namesalary
Asha Verma95,000
Rahul Singh82,000
Meera Nair65,000
Vikram Rao90,000
Priya Paswan72,000
Arjun Kisku88,000

Use SELECT * to get every column instead of naming them — try changing the query above to SELECT * FROM employees; and rerun it.

03

WHERE

Filter which rows come back, using comparisons. Combine conditions with AND / OR.

or press Ctrl/Cmd + Enter
1 row
namedepartmentsalary
Asha VermaEngineering95,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).

SQL
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 file
04

ORDER BY

Sort the results. ASC (ascending, the default) goes low to high; DESC goes high to low.

or press Ctrl/Cmd + Enter
6 rows
namesalary
Asha Verma95,000
Vikram Rao90,000
Arjun Kisku88,000
Rahul Singh82,000
Priya Paswan72,000
Meera Nair65,000
05

LIMIT

Only return the first N rows — the standard way to answer "top 5" style questions.

or press Ctrl/Cmd + Enter
3 rows
namesalary
Asha Verma95,000
Vikram Rao90,000
Arjun Kisku88,000
06

DISTINCT

Remove duplicate values from the result, keeping only unique rows.

or press Ctrl/Cmd + Enter
3 rows
department
Engineering
Sales
Marketing
07

Aggregates

Collapse a column into a single summary number: COUNT, SUM, AVG, MIN, MAX.

or press Ctrl/Cmd + Enter
1 row
total_employeestotal_salaryavg_salarymin_salarymax_salary
6492,00082,00065,00095,000
08

GROUP BY

Pair an aggregate with GROUP BY to get one summary row per category instead of one grand total.

or press Ctrl/Cmd + Enter
3 rows
departmentnum_employeesavg_salary
Engineering288,500
Marketing280,000
Sales277,500
09

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.

or press Ctrl/Cmd + Enter
1 row
departmentavg_salary
Engineering88,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.

10

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 JOINemployeesdepartmentsLEFT JOINemployeesdepartments
INNER JOIN keeps only the shaded overlap — rows that exist in both tables. LEFT JOIN keeps the entire employees circle, matched or not.

INNER JOIN

or press Ctrl/Cmd + Enter
6 rows
namedepartmentlocation
Asha VermaEngineeringBangalore
Rahul SinghEngineeringBangalore
Meera NairSalesMumbai
Vikram RaoSalesMumbai
Priya PaswanMarketingDelhi
Arjun KiskuMarketingDelhi

LEFT JOIN — departments with no employees still show up

or press Ctrl/Cmd + Enter
8 rows
departmentemployee
EngineeringAsha Verma
EngineeringRahul Singh
SalesMeera Nair
SalesVikram Rao
MarketingArjun Kisku
MarketingPriya Paswan
HRNULL
FinanceNULL
11

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.

or press Ctrl/Cmd + Enter
2 rows
employee_namemonthly_pay
Meera Nair65,000
Vikram Rao90,000
12

CASE WHEN

Add if/else logic right inside a query — handy for labeling or bucketing values.

or press Ctrl/Cmd + Enter
6 rows
namesalarysalary_band
Asha Verma95,000High
Rahul Singh82,000Medium
Meera Nair65,000Entry
Vikram Rao90,000High
Priya Paswan72,000Entry
Arjun Kisku88,000Medium
13

Subquery

A query nested inside another query — often used to compare each row against an aggregate, like "above average."

or press Ctrl/Cmd + Enter
3 rows
namesalary
Asha Verma95,000
Vikram Rao90,000
Arjun Kisku88,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.

14

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

or press Ctrl/Cmd + Enter

employees right now

idnamedepartmentsalaryhire_datemanager_id
1Asha VermaEngineering95,0002019-03-12NULL
2Rahul SinghEngineering82,0002020-07-011
3Meera NairSales65,0002021-01-154
4Vikram RaoSales90,0002018-11-20NULL
5Priya PaswanMarketing72,0002022-05-306
6Arjun KiskuMarketing88,0002017-09-05NULL

UPDATE — change existing rows

or press Ctrl/Cmd + Enter

employees right now

idnamedepartmentsalaryhire_datemanager_id
1Asha VermaEngineering95,0002019-03-12NULL
2Rahul SinghEngineering82,0002020-07-011
3Meera NairSales65,0002021-01-154
4Vikram RaoSales90,0002018-11-20NULL
5Priya PaswanMarketing72,0002022-05-306
6Arjun KiskuMarketing88,0002017-09-05NULL

DELETE — remove rows

or press Ctrl/Cmd + Enter

employees right now

idnamedepartmentsalaryhire_datemanager_id
1Asha VermaEngineering95,0002019-03-12NULL
2Rahul SinghEngineering82,0002020-07-011
3Meera NairSales65,0002021-01-154
4Vikram RaoSales90,0002018-11-20NULL
5Priya PaswanMarketing72,0002022-05-306
6Arjun KiskuMarketing88,0002017-09-05NULL
Always include a WHERE clause on UPDATE and DELETE. Leave it off, and the statement runs against every row in the table — there's usually no undo. Try deleting the WHERE id = 6 line above and running it again to see exactly that happen (safely — it's a sandbox).
15

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.

FROMWHEREGROUP BYHAVINGSELECTORDER BYLIMIT
Each box is a step the database works through, left to right, before it hands back a single row of your result.

What each step actually does

  1. FROM — Load the table (or tables, if there's a JOIN).
  2. WHERE — Throw away rows that don't match. Nothing from SELECT exists yet, so you can't filter on a SELECT alias here.
  3. GROUP BY — Collapse the surviving rows into one row per group.
  4. HAVING — Throw away whole groups that don't match. Aggregates like COUNT(*) and AVG(...) work here because the grouping has already happened.
  5. SELECT — Compute the actual output columns, including anything you named with AS.
  6. ORDER BY — Sort the finished rows. SELECT has already run, so its aliases are available here.
  7. 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":

or press Ctrl/Cmd + Enter
2 rows
namesalarysalary_band
Asha Verma95,000High
Vikram Rao90,000High

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:

or press Ctrl/Cmd + Enter
6 rows
namesalarysalary_band
Rahul Singh82,000Entry
Meera Nair65,000Entry
Priya Paswan72,000Entry
Arjun Kisku88,000Entry
Asha Verma95,000High
Vikram Rao90,000High

Cheat sheet

Every clause from this page, in one table.

ClauseWhat it doesExample
SELECTChoose which columns to returnSELECT name, salary
FROMChoose which table to readFROM employees
WHEREFilter individual rowsWHERE salary > 80000
ORDER BYSort the resultsORDER BY salary DESC
LIMITReturn only the first N rowsLIMIT 5
DISTINCTRemove duplicate rowsSELECT DISTINCT department
COUNT / SUM / AVG / MIN / MAXSummarize a column into one numberAVG(salary)
GROUP BYOne summary row per groupGROUP BY department
HAVINGFilter groups after aggregationHAVING COUNT(*) > 1
INNER JOINKeep only matching rows from both tablesJOIN departments d ON e.department = d.name
LEFT JOINKeep all left-table rows, matched or notLEFT JOIN employees e ON …
ASRename a column or table in the outputsalary AS monthly_pay
CASE WHENIf/else logic inside a queryCASE WHEN … THEN … ELSE … END
SubqueryA query nested inside anotherWHERE salary > (SELECT AVG(salary) …)
INSERTAdd a new rowINSERT INTO employees (…) VALUES (…)
UPDATEChange existing rowsUPDATE employees SET salary = 97000 WHERE id = 1
DELETERemove rowsDELETE 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.

or press Ctrl/Cmd + Enter

Practice

Write a query, hit Run to see what comes back, then Check answer when you think it's right.

0/3 solved

1

Find every employee in the Sales department earning more than 70,000.

2

Show the number of employees and average salary per department, but only for departments with more than one employee.

3

List every department with its employees, including departments that have none.

What's next

Once this feels comfortable, the natural next step is two things this page skipped on purpose: CTEs (a WITH clause that names a subquery so you can reuse it, instead of nesting queries inside queries) and window functions (ROW_NUMBER, RANK, running totals — calculations across a set of rows without collapsing them with GROUP BY). They show up constantly in real-world SQL, but didn't fit in 30 minutes.

Built for skimming, not memorizing — keep this open in a tab next to your query editor.