Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Programming languages

SQL (PostgreSQL, MySQL, SQLite)

SQL for PostgreSQL, MySQL and SQLite, from a first SELECT to joins, window functions, transactions, indexes and schema design, with every query run.

113 lessons to read 22 modules

113 lessons: 15 free, 31 free with Pro answers, 48 with a free core and 19 with a free intro.

Examples run with sql.js 1.14.2.

Who it is for

Students, analysts, back-end developers and anyone preparing for the SQL round of a technical interview.

What you will be able to do

  • Write SELECT queries that filter, sort, join and summarise data in PostgreSQL, MySQL and SQLite
  • Summarise data with aggregates, GROUP BY, HAVING and window functions
  • Combine results with subqueries, common table expressions and set operations
  • Create tables with keys and constraints, and change data safely inside transactions
  • Read query plans and choose indexes that make slow queries fast
  • Design normalised schemas and decide when to denormalise
  • Recognise how SQL dialects differ and write portable SQL
  • Run SQL from application code with parameters instead of string building

Revise and practise

Modules and lessons

What the labels mean

Free
the whole lesson and every answer in it
Free lesson, Pro answers
the whole lesson is free; the model answers to its interview questions open with a pass
Free core, Pro depth
the core of the lesson is free; its in-depth parts open with a pass
Free intro, Pro
the first part is free; the rest opens with a pass
Pro
opens with a Pro, Premium or Ultimate pass

Minutes are reading time: “8 of 25 min free” means 8 minutes of the lesson are free.

What a pass opens in this track

133 in-depth parts and 260 interview answers. The same pass opens every other track and every Pro tool.

  1. Module 1

    Start here: databases and your first query

    What SQL and relational databases are, running queries in the browser and on your own computer, the practice databases, and SELECT with columns, expressions and aliases.

    1. What SQL is and how relational databases work Beginner Free: 15 minutes
    2. Run SQL in your browser and on your computer Beginner Free: 15 minutes
    3. Meet the practice database and its tables Beginner Free: 20 minutes
    4. SELECT and FROM: columns, expressions, aliases Beginner Free: 20 minutes
  2. Module 2

    Filtering and sorting rows

    WHERE and comparisons, LIKE patterns, NULL and three-valued logic, ORDER BY with LIMIT and OFFSET, DISTINCT and CASE.

    1. WHERE, comparison and logical operators Beginner Free: 20 minutes
    2. LIKE and pattern matching Beginner Free: 15 minutes
    3. NULL and three-valued logic Beginner Free: 25 minutes
    4. ORDER BY, LIMIT and OFFSET Beginner Free: 20 minutes
    5. DISTINCT and CASE expressions Beginner Free: 20 minutes
  3. Module 3

    Data types and built-in functions

    Types and casting, numbers and arithmetic, text functions, dates and times, and time zones in PostgreSQL, MySQL and SQLite.

    1. Data types and type conversion Beginner Free lesson, Pro answers: 25 minutes
    2. Numbers, arithmetic and rounding Beginner Free lesson, Pro answers: 25 minutes
    3. Text functions and Unicode Beginner Free lesson, Pro answers: 25 minutes
    4. Dates, times and date arithmetic Beginner Free lesson, Pro answers: 30 minutes
    5. Time zones, UTC and timestamps Intermediate Free lesson, Pro answers: 25 minutes
  4. Module 4

    Aggregates and GROUP BY

    COUNT, SUM, AVG, MIN and MAX, GROUP BY and HAVING, conditional and string aggregation, subtotals with ROLLUP, and the order in which a query is evaluated.

    1. SQL aggregates: COUNT, SUM, AVG, MIN and MAX Beginner Free: 20 minutes
    2. GROUP BY in SQL: one row per group Beginner Free: 25 minutes
    3. HAVING in SQL: filtering groups Beginner Free: 15 minutes
    4. Conditional aggregation with CASE and FILTER Intermediate Free: 25 minutes
    5. String aggregation: GROUP_CONCAT and STRING_AGG Intermediate Free: 15 minutes
    6. Subtotals with ROLLUP, CUBE and GROUPING SETS Intermediate Free lesson, Pro answers: 25 minutes
    7. SQL order of execution: how a query is evaluated Beginner Free: 15 minutes
  5. Module 5

    Joining tables

    Keys and relationships, inner and outer joins, self-joins, cross joins, multi-table joins and fan-out, range joins and LATERAL.

    1. Primary keys, foreign keys and relationships Beginner Free lesson, Pro answers: 20 minutes
    2. INNER JOIN: combining rows from two tables Beginner Free lesson, Pro answers: 25 minutes
    3. LEFT, RIGHT and FULL OUTER JOIN Beginner Free lesson, Pro answers: 30 minutes
    4. Self-joins: comparing rows of one table Intermediate Free lesson, Pro answers: 20 minutes
    5. CROSS JOIN and combinations Intermediate Free lesson, Pro answers: 20 minutes
    6. Joining many tables without double counting Intermediate Free lesson, Pro answers: 25 minutes
    7. Range joins, as-of joins and LATERAL Advanced Free lesson, Pro answers: 30 minutes
  6. Module 6

    Subqueries and set operations

    UNION, INTERSECT and EXCEPT, scalar and derived subqueries, IN and EXISTS with anti-joins, and correlated subqueries.

    1. UNION, INTERSECT and EXCEPT in SQL Beginner Free lesson, Pro answers: 20 minutes
    2. Subqueries in SELECT, WHERE and FROM Intermediate Free core, Pro depth: 8 of 25 minutes free
    3. IN, EXISTS, ANY/ALL and anti-joins Intermediate Free core, Pro depth: 11 of 30 minutes free
    4. Correlated subqueries in SQL Intermediate Free core, Pro depth: 8 of 25 minutes free
  7. Module 7

    Common table expressions and recursive queries

    WITH queries, recursive common table expressions, hierarchies and trees, and graph queries in SQL.

    1. WITH and common table expressions (CTEs) Intermediate Free lesson, Pro answers: 25 minutes
    2. Recursive CTEs, series and calendar tables Intermediate Free core, Pro depth: 10 of 35 minutes free
    3. Querying hierarchies: org charts and trees Intermediate Free core, Pro depth: 13 of 35 minutes free
    4. Graph problems in SQL: routes and reachability Advanced Free core, Pro depth: 13 of 35 minutes free
  8. Module 8

    Window functions

    Window function basics, ranking, LAG and LEAD, window frames, top-N per group, running totals and growth, and gaps and islands.

    1. Window functions: OVER and PARTITION BY Intermediate Free lesson, Pro answers: 25 minutes
    2. ROW_NUMBER, RANK, DENSE_RANK and NTILE Intermediate Free core, Pro depth: 11 of 30 minutes free
    3. LAG, LEAD, FIRST_VALUE and NTH_VALUE Intermediate Free intro, Pro: 7 of 30 minutes free
    4. Window frames: ROWS, RANGE and GROUPS Advanced Free intro, Pro: 6 of 30 minutes free
    5. Running totals, shares and growth rates Intermediate Free intro, Pro: 8 of 30 minutes free
    6. Gaps and islands, streaks and sessions Advanced Free intro, Pro: 8 of 35 minutes free
  9. Module 9

    Creating tables and constraints

    CREATE TABLE and types, primary keys and identity, NOT NULL, UNIQUE and CHECK, foreign keys, ALTER TABLE and migrations, generated columns and exclusion constraints.

    1. CREATE TABLE and choosing column types Beginner Free lesson, Pro answers: 30 minutes
    2. NOT NULL, UNIQUE, CHECK and DEFAULT Beginner Free core, Pro depth: 9 of 25 minutes free
    3. Foreign keys and ON DELETE actions Intermediate Free core, Pro depth: 9 of 25 minutes free
    4. ALTER TABLE and safe schema migrations Intermediate Free core, Pro depth: 11 of 30 minutes free
    5. Generated columns Intermediate Free core, Pro depth: 8 of 20 minutes free
    6. Preventing overlaps: ranges and exclusion Advanced Free core, Pro depth: 11 of 30 minutes free
  10. Module 10

    Changing data: INSERT, UPDATE, DELETE and UPSERT

    INSERT with RETURNING, UPDATE and DELETE, upsert and MERGE, and loading and exporting data in bulk.

    1. INSERT, defaults and getting generated keys Beginner Free lesson, Pro answers: 20 minutes
    2. UPDATE and DELETE: changing rows safely Beginner Free core, Pro depth: 7 of 20 minutes free
    3. UPSERT and MERGE: insert or update in one step Intermediate Free core, Pro depth: 8 of 20 minutes free
    4. Bulk loading and exporting CSV Intermediate Free core, Pro depth: 7 of 20 minutes free
  11. Module 11

    Transactions, isolation and locking

    Transactions and ACID, isolation levels and their anomalies, MVCC, locking reads and lost updates, and deadlocks with retries.

    1. Transactions, ACID and savepoints Intermediate Free lesson, Pro answers: 25 minutes
    2. Isolation levels and read anomalies Intermediate Free core, Pro depth: 12 of 30 minutes free
    3. MVCC: how readers and writers coexist Advanced Free core, Pro depth: 10 of 25 minutes free
    4. Lost updates, locking reads and job queues Advanced Free core, Pro depth: 11 of 35 minutes free
    5. Deadlocks, serialization failures and retries Advanced Free core, Pro depth: 9 of 30 minutes free
  12. Module 12

    Indexes, query plans and performance

    How indexes work, reading query plans, composite, covering, partial and expression indexes, sargable queries, join algorithms, keyset pagination and a tuning workflow.

    1. How indexes work: B-trees in practice Intermediate Free lesson, Pro answers: 25 minutes
    2. Reading EXPLAIN plans in three engines Intermediate Free core, Pro depth: 11 of 35 minutes free
    3. Composite indexes and column order Intermediate Free intro, Pro: 7 of 30 minutes free
    4. Covering, partial and expression indexes Advanced Free intro, Pro: 7 of 30 minutes free
    5. Writing index-friendly (sargable) conditions Intermediate Free intro, Pro: 7 of 30 minutes free
    6. Join algorithms and optimizer statistics Advanced Free intro, Pro: 7 of 35 minutes free
    7. Fast pagination with keyset (seek) paging Intermediate Free intro, Pro: 7 of 30 minutes free
    8. A query-tuning workflow Advanced Free intro, Pro: 9 of 35 minutes free
  13. Module 13

    Designing database schemas

    From requirements to an ER model, normalisation and denormalisation, relationships and trees, money, time, ids and enums, history and soft deletes, and a design review.

    1. From requirements to an ER model Intermediate Free lesson, Pro answers: 30 minutes
    2. Normalisation: 1NF to BCNF in practice Intermediate Free core, Pro depth: 12 of 35 minutes free
    3. Denormalisation and derived data Advanced Free core, Pro depth: 10 of 30 minutes free
    4. Many-to-many, one-to-one and polymorphic links Intermediate Free core, Pro depth: 10 of 30 minutes free
    5. Storing trees: adjacency, paths and closure tables Advanced Free core, Pro depth: 10 of 30 minutes free
  14. Module 14

    Views, functions, procedures and triggers

    Views, materialized views, functions and procedures, and triggers.

    1. Views and updatable views Intermediate Free lesson, Pro answers: 25 minutes
    2. Materialized views and summary tables Intermediate Free core, Pro depth: 10 of 30 minutes free
    3. Stored functions and procedures Intermediate Free core, Pro depth: 13 of 35 minutes free
    4. Triggers: automatic actions on change Intermediate Free core, Pro depth: 13 of 35 minutes free
  15. Module 15

    JSON in SQL

    Storing JSON, querying it, turning JSON into rows and building JSON, and updating and indexing JSON.

    1. Storing JSON: json, jsonb and JSON columns Intermediate Free lesson, Pro answers: 25 minutes
    2. Querying JSON values and paths Intermediate Free core, Pro depth: 11 of 30 minutes free
    3. Turning JSON into rows and rows into JSON Advanced Free core, Pro depth: 10 of 30 minutes free
    4. Updating and indexing JSON Advanced Free core, Pro depth: 9 of 30 minutes free
  16. Module 17

    Security: injection, privileges and data protection

    SQL injection and query parameters, roles and privileges, row-level security for multi-tenant data, and backups and data protection.

    1. SQL injection and query parameters Intermediate Free lesson, Pro answers: 30 minutes
    2. Users, roles and least privilege Intermediate Free core, Pro depth: 6 of 20 minutes free
    3. Row-level security and multi-tenant data Advanced Free core, Pro depth: 6 of 20 minutes free
    4. Backups, restores and protecting personal data Intermediate Free core, Pro depth: 6 of 20 minutes free
  17. Module 18

    Using SQL from application code

    SQL from Python, JavaScript and Java, ORMs and query builders, testing SQL and data quality, and reviewing SQL that AI tools write.

    1. SQL from Python: sqlite3 and psycopg Intermediate Free lesson, Pro answers: 30 minutes
    2. SQL from Node.js and Java Intermediate Free core, Pro depth: 10 of 30 minutes free
    3. ORMs, query builders and migration tools Intermediate Free core, Pro depth: 10 of 30 minutes free
    4. Testing SQL queries and data quality Advanced Free core, Pro depth: 10 of 30 minutes free
    5. Reviewing SQL written by AI assistants Intermediate Free core, Pro depth: 9 of 25 minutes free
  18. Module 19

    SQL for data analysis

    Pivoting and unpivoting, time-series bucketing, cohorts, retention and funnels, percentiles and distributions, and star schemas.

    1. Pivot and unpivot in SQL: rows to columns and back Intermediate Free lesson, Pro answers: 30 minutes
    2. Time buckets in SQL: weeks, months, fiscal years Intermediate Free core, Pro depth: 9 of 30 minutes free
    3. Cohort analysis in SQL: retention and funnels Advanced Free intro, Pro: 9 of 35 minutes free
    4. Median in SQL: percentiles, histograms, spread Intermediate Free intro, Pro: 7 of 30 minutes free
    5. Star schemas: facts, dimensions and SCD type 2 Advanced Free intro, Pro: 8 of 35 minutes free
  19. Module 20

    Dialects and portability

    How PostgreSQL, MySQL and SQLite differ, the SQL standard and portable SQL, moving between engines, and other SQL dialects.

    1. PostgreSQL vs MySQL vs SQLite: differences Intermediate Free lesson, Pro answers: 30 minutes
    2. The SQL standard and portable SQL Intermediate Free core, Pro depth: 9 of 24 minutes free
    3. Migrating between SQL engines safely Advanced Free core, Pro depth: 12 of 30 minutes free
    4. Other dialects: SQL Server, Oracle, DuckDB Intermediate Free core, Pro depth: 8 of 22 minutes free
  20. Module 21

    SQL Server and Oracle side by side

    First queries in SQL Server and Oracle, and how they differ in paging, identity, MERGE, dates, strings, NULLs, collations, temporary tables, hierarchies and PIVOT.

    1. SQL Server and Oracle: first queries side by side Intermediate Free lesson, Pro answers: 25 minutes
    2. Row limits: TOP, OFFSET...FETCH and ROWNUM Intermediate Free core, Pro depth: 10 of 25 minutes free
    3. Dates and times: SQL Server vs Oracle Intermediate Free core, Pro depth: 13 of 35 minutes free
    4. String functions: SQL Server vs Oracle Intermediate Free core, Pro depth: 12 of 30 minutes free
    5. NULLs and empty strings: Oracle versus SQL Server Intermediate Free core, Pro depth: 9 of 25 minutes free
    6. Collations and case in SQL Server and Oracle Intermediate Free core, Pro depth: 11 of 30 minutes free
    7. Hierarchies: recursive CTEs and Oracle CONNECT BY Advanced Free core, Pro depth: 11 of 30 minutes free
    8. PIVOT, APPLY and windows in SQL Server and Oracle Advanced Free core, Pro depth: 11 of 30 minutes free
  21. Module 22

    SQL interview preparation

    A method for SQL interview questions, the classic problem types from ranking to intervals, theory and output prediction, and timed practice.

    1. How to approach SQL interview questions Intermediate Free lesson, Pro answers: 25 minutes
    2. Org chart drills: self-joins and recursion Intermediate Free intro, Pro: 7 of 30 minutes free
    3. Interview drills: retention, growth, rolling Advanced Free intro, Pro: 8 of 35 minutes free
    4. Interview drills: 'all', 'none' and division Advanced Free intro, Pro: 6 of 30 minutes free
    5. Interview drills: intervals and as-of lookups Advanced Free intro, Pro: 8 of 35 minutes free
    6. Interview drills: reports, pivots, percentages Intermediate Free intro, Pro: 7 of 30 minutes free
    7. Interview drills: Oracle SQL syntax Intermediate Free intro, Pro: 8 of 35 minutes free
    8. SQL theory questions and output prediction Intermediate Free lesson, Pro answers: 30 minutes

Related tools

Official documentation

More in Programming languages

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.