Free tutorial · 36 lessons

SQL Tutorial

Learn SQL step by step on a real database in your browser: SELECT, WHERE, ORDER BY, GROUP BY, joins, subqueries, CTEs, window functions, INSERT, UPDATE, DELETE, indexes, views and transactions.

Start learning →

SQL Basics

  1. SQL Introduction — What SQL is, what relational databases are and how this tutorial's examples work.
  2. The Sample Database — The tables used throughout this tutorial: customers, products, orders and employees.
  3. SQL SELECT — Choose columns, use * and remove duplicates with DISTINCT.
  4. SQL WHERE — Filter rows with WHERE and comparison operators.
  5. SQL AND, OR, NOT — Combine conditions and control precedence with parentheses.
  6. SQL ORDER BY — Sort results ascending or descending, by one or more columns.
  7. SQL LIMIT — Return only the first N rows, and paginate with OFFSET.
  8. SQL LIKE and Wildcards — Match text patterns with LIKE, % and _.
  9. SQL IN and BETWEEN — Match a list of values with IN and a range with BETWEEN.
  10. SQL NULL Values — What NULL means, IS NULL, COALESCE and three-valued logic.
  11. SQL Aliases — Rename columns and tables with AS for readable queries.
  12. SQL History & Versions — How the relational model and SQL were invented at IBM, every SQL standard from SQL-86 to SQL:2023, the history and current versions of SQLite, PostgreSQL, MySQL and SQL Server, and how their SQL dialects differ.

Aggregation and Functions

  1. SQL Aggregate Functions — COUNT, SUM, AVG, MIN and MAX.
  2. SQL GROUP BY — Group rows and aggregate each group.
  3. SQL HAVING — Filter groups after aggregation with HAVING.
  4. SQL String Functions — UPPER, LOWER, LENGTH, SUBSTR, REPLACE, TRIM and concatenation.
  5. SQL Date Functions — Extract parts of dates, add intervals and group by month.
  6. SQL CASE — Conditional logic inside queries with CASE WHEN.

Joins and Subqueries

  1. SQL INNER JOIN — Combine rows from two tables that match on a key.
  2. SQL LEFT JOIN — Keep every row from the left table, matched or not.
  3. SQL RIGHT, FULL and CROSS JOIN — The remaining join types and when you'd use them.
  4. SQL Self Join — Join a table to itself — for example employees and their managers.
  5. SQL UNION — Stack the results of two queries with UNION and UNION ALL.
  6. SQL Subqueries — Use a query inside another query — in WHERE, FROM and SELECT.
  7. SQL EXISTS — Test whether related rows exist with EXISTS and NOT EXISTS.
  8. SQL WITH (CTEs) — Name subqueries with WITH, and walk hierarchies with recursive CTEs.

Changing Data and Tables

  1. SQL INSERT — Add rows to a table with INSERT INTO.
  2. SQL UPDATE — Change existing rows — and always use WHERE.
  3. SQL DELETE — Remove rows safely, and understand TRUNCATE and soft deletes.
  4. SQL CREATE TABLE — Define tables, data types and constraints.
  5. SQL ALTER and DROP — Change a table's structure and remove tables safely.
  6. SQL Indexes — Speed up lookups with indexes, and see how the database uses them.
  7. SQL Views — Save a query as a virtual table with CREATE VIEW.
  8. SQL Transactions — Group statements into all-or-nothing units with BEGIN, COMMIT and ROLLBACK.

Advanced SQL

  1. SQL Window Functions — ROW_NUMBER, RANK, running totals and LAG with OVER (PARTITION BY ...).
  2. SQL Next Steps — Where to go next: database design, performance and interview practice.