Free SQL tutorial
SQL guides with examples you can run
Every guide explains one SQL concept in plain English, shows the syntax, and gives you examples to run on a real PostgreSQL database right in the page. Then a short challenge checks that you've got it.
Basics
SQL SELECTLearn the SQL SELECT statement with runnable examples on a real PostgreSQL database. Pick colum…SQL aliases (AS)Use SQL aliases with AS to rename columns and shorten table names in joins. Clear examples and …SQL DISTINCTReturn unique values with SQL SELECT DISTINCT and COUNT(DISTINCT). Examples, gotchas and an int…
Filtering
SQL WHEREHow to filter rows with the SQL WHERE clause: comparison operators, text, numbers and dates. Ru…SQL AND, OR and NOTCombine conditions in SQL with AND, OR and NOT. Learn operator precedence, parentheses and comm…SQL INUse the SQL IN operator to match any value in a list or subquery. Examples, NOT IN pitfalls wit…SQL BETWEENSQL BETWEEN explained: inclusive ranges for numbers and dates, the timestamp trap, and a live e…SQL LIKESearch text in SQL with LIKE, ILIKE and the % and _ wildcards. Examples you can run instantly p…SQL IS NULLWhat NULL means in SQL, why = NULL never works, and how to use IS NULL, IS NOT NULL and COALESC…
Sorting
Aggregation
SQL COUNTLearn SQL COUNT(*), COUNT(column) and COUNT(DISTINCT). Understand how NULLs are counted and pra…SQL SUMTotal values with the SQL SUM function: whole tables, per group, and calculated revenue. Runnab…SQL AVGCalculate averages in SQL with AVG and ROUND. See how NULLs affect averages and practise on a l…SQL MIN and MAXFind the lowest and highest values with SQL MIN and MAX on numbers, dates and text. Live exampl…SQL GROUP BYMaster SQL GROUP BY: aggregate per group, group by several columns and avoid the classic errors…SQL HAVINGLearn when to use SQL HAVING instead of WHERE, with runnable examples on aggregates and an inte…
Joins
SQL JOINLearn SQL INNER JOIN: combine tables with ON, join three tables and avoid duplicate rows. Pract…SQL LEFT JOINUnderstand SQL LEFT JOIN, find rows without a match (anti-join) and avoid the WHERE trap. Inter…SQL self joinHow a SQL self join works, with an employee-manager example you can run live, plus a practice e…
Advanced queries
SQL subqueryLearn SQL subqueries step by step: in WHERE, in FROM and correlated subqueries, with live Postg…SQL WITH clause (CTE)Use Common Table Expressions (WITH) to break complex SQL into steps. Runnable examples on Postg…SQL EXISTSCheck for related rows with SQL EXISTS and NOT EXISTS. Why NOT EXISTS beats NOT IN, with live e…
Functions
SQL CASE WHENAdd conditional logic to SQL with CASE WHEN: categorise values, group by buckets and pivot with…SQL COALESCEReplace NULLs with defaults using SQL COALESCE, and learn NULLIF to avoid division by zero. Run…SQL string functionsThe SQL string functions you need: concatenation, case changes, length, substring and replace. …SQL date functionsWork with dates in SQL: EXTRACT year and month, DATE_TRUNC for monthly reports, intervals and d…
Window functions
SQL window functionsA clear introduction to SQL window functions: OVER, PARTITION BY, ORDER BY and frames. Runnable…SQL ROW_NUMBER, RANK and DENSE_RANKRank rows in SQL with ROW_NUMBER, RANK and DENSE_RANK, and solve the classic top-N-per-group in…SQL LAG and LEADUse SQL LAG and LEAD to compare a row with the previous or next one: month-over-month growth an…SQL running totalCalculate running totals and cumulative sums in SQL with SUM() OVER (ORDER BY). Step-by-step ex…
Changing data
SQL INSERT INTOAdd data with SQL INSERT INTO: single rows, multiple rows and INSERT ... SELECT. Practise safel…SQL UPDATEChange data with SQL UPDATE and SET, update with calculations, and avoid the missing-WHERE disa…SQL DELETERemove data with SQL DELETE FROM and WHERE, the difference from TRUNCATE and DROP, and safe liv…SQL CREATE TABLECreate tables with SQL CREATE TABLE: choose data types, add primary keys, NOT NULL and foreign …