Skip to content
Codewilds

SQL cheat sheet

The essential SQL syntax on one page. Every snippet comes from a lesson of our interactive Learn SQL course — click "Practise" to write it yourself and get instant feedback.

Last updated on .

Querying Basics

SQL SELECT: Your First Query

Write your first SQL query with SELECT and FROM, read a table in a real SQLite database right in your browser, and see the result instantly.

SELECT * FROM heroes;
Practise: SQL SELECT: Your First Query

SQL WHERE: Filter Rows with Conditions

Filter rows in SQL with WHERE. Compare values with =, <>, < and >, combine conditions with AND, OR and NOT, and group them with parentheses.

SELECT name, level FROM heroes WHERE level > 18;
Practise: SQL WHERE: Filter Rows with Conditions

Aggregates & Grouping

SQL COUNT and DISTINCT: Count Rows and Unique Values

Count rows with SQL COUNT(*), see why COUNT(column) skips NULLs, remove duplicates with DISTINCT and count unique values with COUNT(DISTINCT).

SELECT COUNT(*) AS veterans FROM heroes WHERE level >= 15;
Practise: SQL COUNT and DISTINCT: Count Rows and Unique Values

SQL GROUP BY: Count, Sum and Average per Group

Summarise data per group with SQL GROUP BY. Count, sum and average for each category, group by several columns and sort the groups.

SELECT class, SUM(gold) AS total_gold
FROM heroes
GROUP BY class;
Practise: SQL GROUP BY: Count, Sum and Average per Group

SQL HAVING vs WHERE: Filter Groups After GROUP BY

Learn the difference between SQL WHERE and HAVING. Filter rows before grouping, and filter groups by their COUNT or SUM after GROUP BY.

SELECT region, COUNT(*) AS quests
FROM quests
GROUP BY region
HAVING COUNT(*) >= 2;
Practise: SQL HAVING vs WHERE: Filter Groups After GROUP BY

Joins

SQL INNER JOIN: Combine Rows from Two Tables

Learn why data is split into tables, how foreign keys link them, and how SQL JOIN … ON combines rows from two tables into one result.

SELECT heroes.name AS hero, guilds.name AS guild, guilds.city
FROM heroes
JOIN guilds ON heroes.guild_id = guilds.id;
Practise: SQL INNER JOIN: Combine Rows from Two Tables

SQL Multiple Joins and Table Aliases: Join Three Tables

Join three or more tables in SQL. Give tables short aliases, follow foreign keys through a link table, and combine heroes, quests and items.

SELECT h.name AS hero, g.name AS guild
FROM heroes AS h
JOIN guilds AS g ON h.guild_id = g.id;
Practise: SQL Multiple Joins and Table Aliases: Join Three Tables

SQL LEFT JOIN: Keep Unmatched Rows and Find Missing Matches

Use SQL LEFT JOIN to keep every row of the first table, get NULLs for missing matches, and find rows without a match with WHERE … IS NULL.

SELECT h.name AS hero, g.name AS guild
FROM heroes h
LEFT JOIN guilds g ON h.guild_id = g.id;
Practise: SQL LEFT JOIN: Keep Unmatched Rows and Find Missing Matches

Changing Data

SQL INSERT INTO: Add Rows to a Table

Add new rows with SQL INSERT INTO … VALUES, insert several rows in one statement, and let SQLite fill in default values and auto-numbered ids.

INSERT INTO guilds (name, city, founded)
VALUES ('Storm Riders', 'Saltcliff', 2025);
Practise: SQL INSERT INTO: Add Rows to a Table

SQL UPDATE: Change Existing Rows with SET and WHERE

Change data with SQL UPDATE … SET … WHERE, update several columns at once, use expressions like gold = gold + 100 and avoid the missing-WHERE disaster.

UPDATE heroes
SET level = 10
WHERE id = 4;
Practise: SQL UPDATE: Change Existing Rows with SET and WHERE

SQL DELETE: Remove Rows Safely with WHERE

Remove rows with SQL DELETE FROM … WHERE, check the rows first with SELECT, and delete related rows in the right order so no orphans are left behind.

DELETE FROM items
WHERE type = 'scroll' AND price < 50;
Practise: SQL DELETE: Remove Rows Safely with WHERE

Designing Tables

SQL CREATE TABLE: Columns, Data Types and Primary Keys

Create your own SQL tables with CREATE TABLE, choose SQLite data types (INTEGER, TEXT, REAL) and give every row an id with INTEGER PRIMARY KEY.

CREATE TABLE taverns (
  id INTEGER PRIMARY KEY,
  name TEXT,
  city TEXT,
  rooms INTEGER,
  ale_price REAL
);
Practise: SQL CREATE TABLE: Columns, Data Types and Primary Keys

SQL Constraints: NOT NULL, UNIQUE, DEFAULT and CHECK

Protect your data with SQL constraints: NOT NULL, UNIQUE, DEFAULT and CHECK in CREATE TABLE, the errors they raise, and a table that rejects bad rows.

CREATE TABLE bounties (
  id INTEGER PRIMARY KEY,
  target TEXT NOT NULL UNIQUE,
  reward INTEGER NOT NULL CHECK (reward > 0),
  status TEXT NOT NULL DEFAULT 'open'
);
Practise: SQL Constraints: NOT NULL, UNIQUE, DEFAULT and CHECK

SQL Foreign Keys: REFERENCES, ALTER TABLE and DROP TABLE

Link SQL tables with foreign keys (REFERENCES), switch on enforcement in SQLite, and change a schema with ALTER TABLE ADD COLUMN and DROP TABLE.

CREATE TABLE purchases (
  id INTEGER PRIMARY KEY,
  hero_id INTEGER NOT NULL REFERENCES heroes(id),
  item_id INTEGER NOT NULL REFERENCES items(id),
  quantity INTEGER NOT NULL DEFAULT 1,
  bought_on TEXT NOT NULL
);
Practise: SQL Foreign Keys: REFERENCES, ALTER TABLE and DROP TABLE

Advanced Queries

SQL Subqueries: IN, EXISTS and Scalar Subqueries

Put a query inside a query: SQL subqueries with IN, NOT IN, EXISTS and scalar subqueries like gold > (SELECT AVG(gold) …), with an interactive exercise.

SELECT name, gold
FROM heroes
WHERE gold > (SELECT AVG(gold) FROM heroes);
Practise: SQL Subqueries: IN, EXISTS and Scalar Subqueries

SQL CASE WHEN: If-Then-Else Logic in Your Queries

Add if-then-else logic to SQL with CASE WHEN … THEN … ELSE … END. Label rows, sort in a custom order and count conditionally inside aggregates.

SELECT name, level,
       CASE
         WHEN level >= 20 THEN 'veteran'
         WHEN level >= 10 THEN 'adept'
         ELSE 'novice'
       END AS tier
FROM heroes;
Practise: SQL CASE WHEN: If-Then-Else Logic in Your Queries

SQL WITH Clause: Common Table Expressions (CTEs)

Break a big SQL query into named steps with the WITH clause. Learn common table expressions (CTEs), chain several CTEs and see why they beat nesting.

WITH guild_gold AS (
  SELECT guild_id, SUM(gold) AS total
  FROM heroes
  WHERE guild_id IS NOT NULL
  GROUP BY guild_id
)
SELECT g.name, gg.total
FROM guild_gold gg
JOIN guilds g ON g.id = gg.guild_id
WHERE gg.total > 1500
ORDER BY gg.total DESC;
Practise: SQL WITH Clause: Common Table Expressions (CTEs)

SQL Window Functions: ROW_NUMBER, RANK and Running Totals

Learn SQL window functions: OVER, PARTITION BY, ROW_NUMBER, RANK and running totals with SUM() OVER, to rank and compare rows without losing them.

SELECT name, class, gold,
       MAX(gold) OVER (PARTITION BY class) AS class_best
FROM heroes;
Practise: SQL Window Functions: ROW_NUMBER, RANK and Running Totals