Free · No signup required

Learn SQL with Interactive Lessons

43 lessons with worked examples, result tables and a quiz each. Go from your first SELECT to window functions, and run the examples yourself as you go.

📚 43 lessons📝 Quizzes after every lesson⚡ 100% free🚀 No signup
SQL Fundamentals Track
⬡   Lesson 01
The BIG 6 SQL Clauses

SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY: what each clause does and the order they run in, with an interactive query builder and a 6-question quiz.

5 clausesInteractive builder6-question quiz~15 min
→
▣   Lesson 02
Data Types

INT, DECIMAL, FLOAT, VARCHAR, CHAR, TEXT, DATE and BOOLEAN: what each holds, why money needs DECIMAL and phone numbers need text, how SQLite stores types, and the type names in each database, with runnable examples, 4 practice exercises and a 6-question quiz.

INT · DECIMAL · VARCHAR · DATEChoosing the right typeRunnable examples4 practice exercises6-question quiz~15 min
→
◈   Lesson 02
SELECT DISTINCT

Return each different value once: DISTINCT on one and several columns, COUNT(DISTINCT), how NULLs are treated, DISTINCT vs GROUP BY, and DISTINCT with ORDER BY and LIMIT, with runnable examples, 5 practice exercises and a 5-question quiz.

Unique valuesCOUNT(DISTINCT)Runnable examples5 practice exercises5-question quiz~12 min
→
⊡   Lesson 03
WHERE Clause

Filter rows with 9 operators (=, !=, >, <, >=, <=, BETWEEN, LIKE, IN), AND/OR logic, NULL handling and WHERE vs HAVING, with syntax, real-world examples and practice exercises.

9 operatorsAND/OR demo10-question quiz~25 min
→
∈   Lesson 04
WHERE & IN

IN as a shorter way to write several OR conditions on one column, numbers and text in the list, NOT IN and the NULL trap shown on real data, IN with a subquery and with AND, with runnable examples, 5 practice exercises and a 9-question quiz.

IN · NOT ININ vs ORRunnable examples5 practice exercises9-question quiz~12 min
→
≈   Lesson 05
LIKE

Search by pattern with % and _: starts with, ends with and contains, fixed-format checks, NOT LIKE, case sensitivity in each database, escaping a literal % or _, and when LIKE can use an index, with runnable examples, 5 practice exercises and a 6-question quiz.

% and _ wildcardsRunnable examples5 practice exercises6-question quiz~15 min
→
Σ   Lesson 06
GROUP BY

Summarize data with COUNT, SUM, AVG, MIN, MAX. Learn the syntax, multi-column grouping, WHERE vs HAVING, ORDER BY, GROUP BY with JOIN and the most common mistakes, with examples you can run in the Playground.

COUNT SUM AVG MIN MAXRunnable examples8 practice exercises10-question quiz~30 min
→
/   Lesson 07
Comments & Aliases

Column aliases with AS, table aliases in joins and self joins, which clauses can see an alias in each database, single-line and multi-line comments, commenting code out and writing comments that explain why, with runnable examples, 5 practice exercises and a 6-question quiz.

AS column & table aliases-- and /* */ commentsRunnable examples5 practice exercises6-question quiz~12 min
→
⊞   Lesson 08
Advanced GROUP BY

GROUP BY with two and three columns, several aggregates per group, COUNT(DISTINCT), how NULLs are grouped and counted, HAVING on combined groups, and subtotals with ROLLUP or UNION ALL, with runnable examples, 5 practice exercises and a 6-question quiz.

Multi-column groupingCOUNT DISTINCT · NULLs · subtotalsRunnable examples5 practice exercises6-question quiz~20 min
→
▽   Lesson 09
HAVING Clause

Filter groups after GROUP BY with COUNT, SUM, AVG, MIN and MAX. Covers WHERE vs HAVING, multiple conditions, JOINs, aliases and HAVING without GROUP BY, with runnable Playground examples, 9 practice problems and a 10-question quiz.

WHERE vs HAVINGRunnable examples9 practice problems10-question quiz~30 min
→
⇅   Lesson 10
ORDER BY

Sort results ascending or descending, by several columns with tie-breakers, by dates, text, aliases and aggregates, in a custom order with CASE, and control where NULLs go, with runnable examples, 5 practice exercises and a 6-question quiz.

ASC & DESCMulti-column sortRunnable examples5 practice exercises6-question quiz~15 min
→
⛁   Lesson 11
LIMIT / TOP / FETCH

Return only the first N rows: top N with ORDER BY, ties at the cut-off, OFFSET and the Nth highest value, page-by-page results and keyset pagination, and LIMIT vs SQL Server TOP vs standard FETCH FIRST, with runnable examples, 5 practice exercises and a 10-question quiz.

Top N & OFFSETLIMIT vs TOP vs FETCHRunnable examples5 practice exercises10-question quiz~15 min
→
⟗   Lesson 12
SQL JOINs: All 6 Types

All 6 JOIN types side by side (INNER, LEFT, RIGHT, FULL OUTER, CROSS and SELF) on real tables you can run, with a which-join-to-use guide, joining 3+ tables, a cheat sheet, 8 practice exercises and 15 interview questions.

6 JOIN types comparedJOIN cheat sheet8 practice exercises15 interview Q&As12-question quiz~25 min
→
⋈   Lesson 13
SQL INNER JOIN

Match rows from two or more tables and return only the pairs that fit, with runnable examples on real tables, joining 3 and 4 tables, INNER JOIN with WHERE and GROUP BY, why rows go missing or repeat, 8 practice exercises and 12 interview questions.

Multi-table JOINsINNER vs LEFT JOIN8 practice exercises12 interview Q&As10-question quiz~20 min
→
⟕   Lesson 14
SQL LEFT JOIN

Keep every row from the left table, handle the NULLs, and find rows with no match, with runnable examples on real tables, LEFT JOIN vs INNER JOIN, ON vs WHERE, multiple tables, 8 practice exercises and 15 interview questions.

Find rows with no matchON vs WHERE8 practice exercises15 interview Q&As12-question quiz~25 min
→
⟖   Lesson 15
SQL RIGHT JOIN

Keep every row from the right table, and see exactly how RIGHT JOIN compares with LEFT, INNER and FULL OUTER JOIN, with runnable examples on real tables, finding unmatched rows, ON vs WHERE, 7 practice exercises and 12 interview questions.

RIGHT vs LEFT JOINFind unmatched rows7 practice exercises12 interview Q&As10-question quiz~20 min
→
⟗   Lesson 16
SQL FULL OUTER JOIN

Keep every row from both tables and line up two lists side by side, with runnable examples on real tables, finding mismatches in both directions, COALESCE, the MySQL UNION workaround, 7 practice exercises and 12 interview questions.

Find mismatchesMySQL workaround7 practice exercises12 interview Q&As10-question quiz~20 min
→
⟲   Lesson 17
SQL SELF JOIN

Join a table to itself with two aliases: employees and their managers, comparing rows, finding duplicates and building pairs, with runnable examples on real tables, 7 practice exercises and 12 interview questions.

Employee–managerFind duplicates7 practice exercises12 interview Q&As10-question quiz~20 min
→
⊗   Lesson 18
SQL CROSS JOIN

Pair every row of one table with every row of another: how the Cartesian product and its row counts work, report grids with zeros, percent of total, pairs within a table and how to avoid accidental cross joins, with 7 practice exercises and 12 interview questions.

Cartesian productReport grids · % of total7 practice exercises12 interview Q&As10-question quiz~20 min
→
⊂   Lesson 19
SQL Subqueries

Single-value and multi-row subqueries, subqueries in WHERE, SELECT and FROM, derived tables, nested subqueries read from the inside out, and when a JOIN is the better choice, with runnable examples, 6 practice exercises and a 15-question quiz.

Query inside a queryWHERE · SELECT · FROMRunnable examples6 practice exercises15-question quiz~25 min
→
⇌   Lesson 20
SQL Correlated Subqueries

Subqueries that use a value from the current outer row: comparing a row with its own group, the top value per group, per-row values in SELECT, EXISTS and NOT EXISTS, and the same questions written as a JOIN or a window function, with runnable examples, 6 practice exercises and a 20-question quiz.

Depends on the outer rowEXISTS / NOT EXISTSRunnable examples6 practice exercises20-question quiz~25 min
→
∃   Lesson 21
SQL EXISTS

How EXISTS checks whether a subquery returns any row, why SELECT 1 is enough, NOT EXISTS for missing records, EXISTS across several tables and in CASE, and EXISTS vs IN, JOIN and COUNT, with runnable examples, 6 practice exercises and a 20-question quiz.

Checks row existenceNOT EXISTSRunnable examples6 practice exercises20-question quiz~25 min
→
⇄   Lesson 22
IN vs EXISTS

When to use IN and when EXISTS: how each is evaluated, why NOT IN returns nothing when the list contains a NULL, performance and when a JOIN is the better tool, with runnable examples, 6 practice exercises and a 20-question quiz.

∈ vs ∃NOT IN and NULLsRunnable examples6 practice exercises20-question quiz~25 min
→
Σ   Lesson 23
SQL Aggregate Functions

COUNT, SUM, AVG, MIN and MAX: COUNT(*) vs COUNT(column) vs COUNT(DISTINCT), how NULLs and empty results are handled, aggregates with GROUP BY and HAVING, conditional totals and GROUP_CONCAT, with runnable examples, 6 practice exercises and a 25-question quiz.

COUNT · SUM · AVG · MAX · MINNULL handlingRunnable examples6 practice exercises25-question quiz~25 min
→
"   Lesson 24
SQL String Functions

CONCAT, UPPER, LOWER, LENGTH, TRIM, SUBSTRING and REPLACE on real tables you can run, a complete data cleaning query, the differences between databases, 6 practice exercises and a 25-question quiz.

CONCAT · UPPER · LOWER · TRIMSUBSTRING & REPLACERunnable examples6 practice exercises25-question quiz~35 min
→
◷   Lesson 25
SQL Date Functions

Get the current date, extract date parts, add and subtract time, and calculate date differences: CURRENT_DATE, YEAR/MONTH/DAY, DATEADD, DATEDIFF, EXTRACT and SQLite strftime, with syntax for SQLite, MySQL, PostgreSQL and SQL Server, 35 business scenarios, 8 practice exercises and a 30-question quiz.

Current date · extract · add · differenceSQLite · MySQL · PostgreSQL · SQL Server35 business scenarios8 practice exercises50 interview Q&As30-question quiz~70 min
→
⑂   Lesson 26
SQL CASE WHEN

Simple and searched CASE, multiple conditions and their order, CASE in WHERE, ORDER BY and GROUP BY, conditional aggregation with SUM and COUNT, and NULL handling, on real tables you can run, with 6 practice exercises and a 29-question quiz.

Simple & multiple conditionsConditional aggregation (SUM/COUNT)Runnable examples6 practice exercises29-question quiz~35 min
→
⁇   Lesson 27
SQL COALESCE

Return the first non-NULL value: default labels, multi-column fallbacks, safe calculations, COALESCE with SUM, AVG and COUNT, after a LEFT JOIN and with NULLIF, and COALESCE vs ISNULL, IFNULL and NVL, on real tables you can run, with 6 practice exercises and a 30-question quiz.

First non-NULL valueMulti-column fallback chainsRunnable examples6 practice exercises30-question quiz~30 min
→
÷   Lesson 28
SQL NULLIF

NULLIF returns NULL when two values match: preventing division by zero, turning placeholders such as 0 or N/A into NULL, its effect on AVG and COUNT, and NULLIF with COALESCE and CASE, with runnable examples, 6 practice exercises and a 30-question quiz.

a = b → NULLDivide-by-zero fixRunnable examples6 practice exercises30-question quiz~25 min
→
▦   Lesson 29
SQL Window Functions

OVER and PARTITION BY, ROW_NUMBER, RANK, DENSE_RANK and NTILE, running totals and moving averages with window frames, LAG, LEAD, FIRST_VALUE and LAST_VALUE, top N per group and duplicates, with runnable examples, 8 practice exercises and a 41-question quiz.

OVER · PARTITION BYRanking · running totals · LAGRunnable examples8 practice exercises41-question quiz~40 min
→
⌘   Lesson 30
SQL CTEs (WITH Clause)

The WITH clause: single, multiple and chained CTEs, CTE vs subquery, temp table and view, top N per group, second-highest salary, duplicates, cohorts and retention, and how recursive CTEs work, with runnable examples, 6 practice exercises and a 40-question quiz.

WITH · Multiple · Chained CTEsRecursive CTEsRunnable examples6 practice exercises40-question quiz~35 min
→
◉   Lesson 31
SQL Views

CREATE VIEW and DROP VIEW, what a view stores and what it does not, hiding joins, restricting columns and sharing one definition of a figure, views vs tables, CTEs and temporary tables, materialized and updatable views, with runnable examples, 4 practice exercises and a 40-question quiz.

Virtual tablesMaterialized viewsRunnable examples4 practice exercises40-question quiz~25 min
→
▤   Lesson 32
SQL Indexes

How B-tree indexes work, CREATE INDEX syntax, clustered vs non-clustered, unique, composite, covering, partial and expression indexes, reading execution plans, selectivity on real columns and when an index is ignored, with runnable examples, 5 practice exercises and a 50-question quiz.

B-tree · composite · coveringReading EXPLAINRunnable examples5 practice exercises50-question quiz~35 min
→
↺   Lesson 33
SQL Transactions

BEGIN, COMMIT and ROLLBACK, savepoints, the ACID properties, isolation levels and the read problems they prevent, locks, lost updates and deadlocks, and two-phase commit, with 4 practice exercises and a 35-question quiz.

BEGIN · COMMIT · ROLLBACKACID · isolation · locks4 practice exercises35-question quiz~30 min
→
⚙   Lesson 34
SQL Stored Procedures

CREATE PROCEDURE in MySQL, SQL Server and PostgreSQL, input and output parameters, variables and IF, error handling with transactions, dynamic SQL without injection, and procedures vs functions, views and triggers, with 4 practice exercises and a 40-question quiz.

CREATE PROCEDURE · CALL · EXECParameters · error handling4 practice exercises40-question quiz~25 min
→
⚡   Lesson 35
SQL Triggers

CREATE TRIGGER, BEFORE, AFTER and INSTEAD OF, OLD and NEW values, audit logs, validation and soft deletes, the differences in PostgreSQL, MySQL and SQL Server, and triggers vs stored procedures, with 4 practice exercises and a 41-question quiz.

BEFORE · AFTER · INSTEAD OFOLD and NEW4 practice exercises41-question quiz~25 min
→
🎤   Lesson 36
SQL Interview Questions

121 SQL interview questions with answers, from beginner to advanced: JOINs, GROUP BY, subqueries, CTEs, window functions, indexes and transactions, plus data analyst metrics, scenario problems and coding practice. Every runnable query is shown with its real output.

121 questionsBeginner → AdvancedData analyst questionsScenario problemsRunnable in the Playground
→
↔   Lesson 37
BETWEEN

Filter a range of numbers, dates or text. Covers the inclusive ends, NOT BETWEEN and the date-time trap, with runnable Playground examples, 4 practice problems and a 5-question quiz.

Inclusive rangesRunnable examples4 practice problems5-question quiz~12 min
→
∅   Lesson 38
IS NULL & IS NOT NULL

Find missing values the right way. Why = NULL never matches, how NULL affects counts, "not equal" filters, NOT IN and sorting, with runnable Playground examples, 4 practice problems and a 5-question quiz.

IS NULL · IS NOT NULLRunnable examples4 practice problems5-question quiz~15 min
→
∪   Lesson 39
UNION & UNION ALL

Stack the results of two queries. UNION vs UNION ALL, the column rules, labelling the source, UNION vs JOIN, INTERSECT and EXCEPT, with runnable Playground examples, 4 practice problems and a 5-question quiz.

UNION vs UNION ALLRunnable examples4 practice problems5-question quiz~15 min
→
#   Lesson 40
ROW_NUMBER()

Number rows, restart the count with PARTITION BY, get the top N rows per group and keep the latest row per customer, with runnable Playground examples, 4 practice problems and a 5-question quiz.

PARTITION BYTop N per groupRunnable examples4 practice problems5-question quiz~18 min
→
🏅   Lesson 41
RANK() vs DENSE_RANK()

How each function handles ties, ranking within groups and finding the Nth highest value, with runnable Playground examples, 4 practice problems and a 5-question quiz.

Ranking with tiesNth highest valueRunnable examples4 practice problems5-question quiz~15 min
→
⇅   Lesson 42
LAG() & LEAD()

Compare each row with the previous or next one: month-over-month change, gaps between a customer's orders, offsets and defaults, with runnable Playground examples, 4 practice problems and a 5-question quiz.

Previous & next rowMonth-over-month changeRunnable examples4 practice problems5-question quiz~18 min
→

What the Lessons Cover

There are 43 lessons, in the order a beginner would need them:

  • Fundamentals: the six core clauses, data types, DISTINCT, WHERE, IN, LIKE, GROUP BY, HAVING, ORDER BY and LIMIT
  • JOINs: an overview, then a lesson each for INNER, LEFT, RIGHT, FULL OUTER, SELF and CROSS JOIN
  • Subqueries: plain and correlated subqueries, EXISTS, and IN compared with EXISTS
  • Functions: aggregates, strings, dates, CASE WHEN, COALESCE and NULLIF
  • Advanced: window functions, CTEs, views, indexes, transactions, stored procedures and triggers
  • Interview preparation: a page of SQL interview questions with worked answers

How the Lessons Work

A lesson explains one topic, shows the syntax, and works through examples with their result tables. In the lessons that query the practice database, each example has a link that opens it in the Playground so you can run it and change it. Every lesson ends with a quiz.

After finishing a lesson, head to the SQL Playground to practise against the same database, or try the SQL Challenges, which range from single-table SELECT queries to window functions and CTEs. You can also take a timed SQL Skill Test.

No Setup Required

There is nothing to install and no database to configure. You type a query in the browser, it is sent to the SQLab Hub server, run on a SQLite database in an isolated sandbox, and the rows come back in well under a second. The practice database is read-only, so you cannot break it.

The lessons are written and maintained by Rahul Kumar. How the content is made and checked.

Frequently Asked Questions

Do I need to create an account to access the SQL lessons?

No. SQLab Hub is 100% free and every lesson can be read without registering. A free account is only needed if you want your quiz and lesson progress saved across devices.

What SQL dialect do the lessons cover?

The lessons teach standard SQL and point out where MySQL, PostgreSQL, SQL Server and SQLite differ. The examples you can run execute on SQLite, because that is the engine behind the Playground.

How long does it take to complete all the lessons?

It depends on the lesson. Short ones such as SELECT DISTINCT or LIKE take 10 to 15 minutes; long references such as Window Functions, Views and Indexes are an hour or more. Each card shows an estimate. With 43 lessons, plan on several weeks of regular practice rather than a weekend.

Can I practise what I learn in the lessons?

Yes. The SQL Playground runs your queries on a SQLite practice database with 8 tables and 1,476 rows, and the challenges give you tasks with an expected result to match.

Are there quizzes to test my knowledge?

Yes. Every lesson ends with a multiple-choice quiz with an explanation for each answer. The length varies by lesson and is shown on its card.

In what order should I study the lessons?

Start with The BIG 6, then Data Types, SELECT DISTINCT and WHERE, and continue down the list. JOINs, subqueries and window functions assume you are comfortable with the earlier lessons.

Can I run every statement from the lessons in the Playground?

No. The Playground accepts read-only queries (SELECT and WITH). Lessons on views, indexes, transactions, stored procedures and triggers explain statements that change a database, so their examples are shown with their results rather than run live.

Ready to practise what you learned?

Write real SQL queries against the 8-table practice database. No signup needed.

⚡ Open SQL Playground🎯 Browse Challenges