Skip to content

Lessons · SQL

Learn SQL step by step: 8 lessons with runnable examples

Each lesson pairs a plain-English explanation with code you can run. Every example was executed through the same sandbox the playground uses before it was published, so what you read has been checked, not just typed.

  1. Lesson 1

    SQL Tables, Rows and Your First SELECT

    Learn what a SQL table really is, how to create one and put rows in it, and how SELECT asks a database for exactly the columns you want — every example runnable in a real SQLite database in your browser.

    sql · select · tables · 1 exercise to practise it

  2. Lesson 2

    Filtering Rows in SQL with WHERE

    Cut a SQL result down to the rows you mean: comparison operators, AND, OR and brackets, IN, BETWEEN, LIKE pattern matching, and why NULL makes rows vanish from a filter that looks correct.

    sql · where · null · 1 exercise to practise it

  3. Lesson 3

    Sorting and Limiting SQL Results: ORDER BY, LIMIT, DISTINCT

    Put SQL rows in a deliberate order with ORDER BY, take just the first few with LIMIT and OFFSET, and remove repeated rows with DISTINCT — including where NULLs and capital letters end up.

    sql · order-by · limit · distinct · 1 exercise to practise it

  4. Lesson 4

    SQL Aggregates, GROUP BY and HAVING

    Collapse many SQL rows into one answer with COUNT, SUM, AVG, MIN and MAX, split the answer per group with GROUP BY, and filter the groups themselves with HAVING — plus what NULL does to every one of them.

    sql · group-by · aggregate · having · 1 exercise to practise it

  5. Lesson 5

    SQL Joins: INNER, LEFT, and Why Rows Duplicate or Disappear

    Put related SQL tables back together with INNER JOIN and LEFT JOIN, find the rows that have no match at all, and understand the two things that surprise everyone: a join that returns more rows than you started with, and one that returns fewer.

    sql · joins · left-join · 2 exercises to practise it

  6. Lesson 6

    SQL Subqueries and CTEs (WITH)

    Build a SQL query out of other queries: scalar subqueries, IN and EXISTS, derived tables, and common table expressions with WITH — including the aggregate-then-join pattern that fixes double counting.

    sql · subquery · cte · with · 1 exercise to practise it

  7. Lesson 7

    Changing Data Safely in SQL: INSERT, UPDATE, DELETE, Transactions

    Write to a SQL table without regretting it: INSERT and INSERT ... SELECT, UPDATE and DELETE with the WHERE you must not forget, transactions with BEGIN and ROLLBACK, and the constraints that stop bad rows getting in.

    sql · insert · update · delete · transactions · constraints · 1 exercise to practise it

  8. Lesson 8

    SQL Window Functions: ROW_NUMBER, RANK and Running Totals

    Use SQL window functions to see other rows without collapsing them: OVER and PARTITION BY, ROW_NUMBER and RANK for ranking and top-N-per-group, SUM() OVER for running totals, and LAG for row-to-row comparisons.

    sql · window-functions · rank · running-total · 2 exercises to practise it

Reading is half of it

The SQL exercises are where you write the code these lessons describe, one idea at a time, with the output checked for you. The playground is for trying any example on this page with your own numbers.