SQL Tutorial
Learn SQL step by step, from your first SELECT to JOINs, CTEs and window functions. Every example runs on small sample databases, and you can see the exact result each query returns.
1. SELECT basics
- SQL SELECT StatementLearn the SQL SELECT statement with runnable examples: choose columns, select every column with *, and calculate new values. Includes common mistakes.
- SQL LIMIT and OFFSETUse SQL LIMIT to return the first N rows, combine it with ORDER BY for top-N queries, and page through results with OFFSET. Examples and equivalents.
- SQL DISTINCTSQL SELECT DISTINCT removes duplicate rows from results. See how it treats multiple columns and NULL, and how COUNT(DISTINCT) works, with examples.
- SQL Aliases (AS)Learn SQL aliases with AS: rename output columns, label calculated values, and shorten table names in joins. Includes rules for where aliases work.
2. Filter with WHERE
- SQL WHERE ClauseFilter rows with the SQL WHERE clause. Runnable examples of comparison operators, text and date filters, plus the most common WHERE mistakes.
- SQL AND, OR and NOTCombine conditions in SQL WHERE with AND, OR and NOT. Learn operator precedence and when you need parentheses, with runnable examples.
- SQL IN OperatorUse the SQL IN operator to match any value in a list or subquery, and NOT IN to exclude them. Includes the NOT IN and NULL trap, with examples.
- SQL BETWEEN OperatorSQL BETWEEN filters values within a range, including both ends. Examples with numbers and dates, NOT BETWEEN, and the date-time boundary trap.
- SQL LIKE OperatorSearch text with SQL LIKE: % matches any characters and _ matches one. Examples for starts with, ends with and contains, plus case sensitivity.
- SQL NULL ValuesUnderstand NULL in SQL: why = NULL never matches, how to use IS NULL and IS NOT NULL, replace NULLs with COALESCE, and how NULL affects COUNT.
3. Sort with ORDER BY
4. Count and group
- SQL Aggregate FunctionsSummarise data with SQL aggregate functions COUNT, SUM, AVG, MIN and MAX. Runnable examples, how they treat NULL, and how to round averages.
- SQL GROUP BYLearn SQL GROUP BY with runnable examples: count and average per group, group by several columns, and the rule for which columns you can select.
- SQL HAVING ClauseUse SQL HAVING to filter groups after GROUP BY, like categories with more than 3 items. Learn HAVING vs WHERE and how to use both in one query.
5. Join tables
- SQL JOIN (INNER JOIN)Learn SQL JOINs with runnable examples: combine tables with INNER JOIN, join three tables, and see how inner, left, right and full joins differ.
- SQL LEFT JOINSQL LEFT JOIN keeps every row from the left table, even without a match. Count with zeros, find rows with no match, and avoid the WHERE trap.
6. Subqueries and CTEs
- SQL SubqueriesLearn SQL subqueries with runnable examples: compare against an average, filter with IN, and query a derived table in FROM. Plus when to use a CTE.
- SQL CTE (WITH Clause)Write readable SQL with CTEs (common table expressions). Learn the WITH clause syntax, chain several CTEs, and see when to use a CTE over a subquery.
7. CASE, text and dates
- SQL CASE WHENAdd if-then logic to SQL with CASE WHEN. Label rows into buckets, count conditionally with SUM(CASE ...), and sort in a custom order. Runnable examples.
- SQL String FunctionsClean and reshape text in SQL: UPPER, LOWER, LENGTH, SUBSTR, INSTR, REPLACE, TRIM and concatenation with ||. Runnable examples and dialect notes.
- SQL Date FunctionsWork with dates in SQL: group by month, add days and find the difference between dates. SQLite examples with PostgreSQL, MySQL and SQL Server equivalents.
8. Window functions
- SQL Window FunctionsLearn SQL window functions with runnable examples: compare rows to their group average with OVER (PARTITION BY), and build running totals.
- SQL ROW_NUMBER, RANK and DENSE_RANKNumber and rank rows in SQL with ROW_NUMBER, RANK and DENSE_RANK. See how each handles ties, and get the top row per group with a CTE.
- SQL LAG and LEADCompare a row with the previous or next row using SQL LAG and LEAD window functions. Calculate day-over-day changes and gaps between events.
9. Change data
- SQL INSERT INTOAdd data with SQL INSERT INTO: insert one row, several rows at once, or rows from a SELECT. Runnable examples and the most common INSERT mistakes.
- SQL UPDATEChange existing data with SQL UPDATE: set one or several columns, calculate new values, and always use WHERE. Runnable examples and safety tips.
- SQL DELETERemove rows with SQL DELETE: delete matching rows, delete with a subquery, and learn how DELETE differs from TRUNCATE and DROP. Runnable examples.
- SQL CREATE TABLECreate tables in SQL with CREATE TABLE: choose column types, add a primary key, NOT NULL, DEFAULT, UNIQUE and CHECK constraints. Runnable example.
Learn by writing SQL, not just reading it
Each tutorial has a matching interactive lesson. Write the query yourself, run it on real data and get instant feedback.
Start the free SQL course