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.
SQL Basics
- SQL Introduction — What SQL is, what relational databases are and how this tutorial's examples work.
- The Sample Database — The tables used throughout this tutorial: customers, products, orders and employees.
- SQL SELECT — Choose columns, use * and remove duplicates with DISTINCT.
- SQL WHERE — Filter rows with WHERE and comparison operators.
- SQL AND, OR, NOT — Combine conditions and control precedence with parentheses.
- SQL ORDER BY — Sort results ascending or descending, by one or more columns.
- SQL LIMIT — Return only the first N rows, and paginate with OFFSET.
- SQL LIKE and Wildcards — Match text patterns with LIKE, % and _.
- SQL IN and BETWEEN — Match a list of values with IN and a range with BETWEEN.
- SQL NULL Values — What NULL means, IS NULL, COALESCE and three-valued logic.
- SQL Aliases — Rename columns and tables with AS for readable queries.
- 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
- SQL Aggregate Functions — COUNT, SUM, AVG, MIN and MAX.
- SQL GROUP BY — Group rows and aggregate each group.
- SQL HAVING — Filter groups after aggregation with HAVING.
- SQL String Functions — UPPER, LOWER, LENGTH, SUBSTR, REPLACE, TRIM and concatenation.
- SQL Date Functions — Extract parts of dates, add intervals and group by month.
- SQL CASE — Conditional logic inside queries with CASE WHEN.
Joins and Subqueries
- SQL INNER JOIN — Combine rows from two tables that match on a key.
- SQL LEFT JOIN — Keep every row from the left table, matched or not.
- SQL RIGHT, FULL and CROSS JOIN — The remaining join types and when you'd use them.
- SQL Self Join — Join a table to itself — for example employees and their managers.
- SQL UNION — Stack the results of two queries with UNION and UNION ALL.
- SQL Subqueries — Use a query inside another query — in WHERE, FROM and SELECT.
- SQL EXISTS — Test whether related rows exist with EXISTS and NOT EXISTS.
- SQL WITH (CTEs) — Name subqueries with WITH, and walk hierarchies with recursive CTEs.
Changing Data and Tables
- SQL INSERT — Add rows to a table with INSERT INTO.
- SQL UPDATE — Change existing rows — and always use WHERE.
- SQL DELETE — Remove rows safely, and understand TRUNCATE and soft deletes.
- SQL CREATE TABLE — Define tables, data types and constraints.
- SQL ALTER and DROP — Change a table's structure and remove tables safely.
- SQL Indexes — Speed up lookups with indexes, and see how the database uses them.
- SQL Views — Save a query as a virtual table with CREATE VIEW.
- SQL Transactions — Group statements into all-or-nothing units with BEGIN, COMMIT and ROLLBACK.
Advanced SQL
- SQL Window Functions — ROW_NUMBER, RANK, running totals and LAG with OVER (PARTITION BY ...).
- SQL Next Steps — Where to go next: database design, performance and interview practice.