Every level, in the open
Databases
3 worlds · 29 levels · 2 bosses
SOLUTION HIDDENTeachers see answer keys in the classroom.
SQL from the first SELECT to joins, GROUP BY, indexes, transactions and backups. Honest note: it runs sqlite3 in your VM (the same SQL as PostgreSQL and MariaDB, without a server).
Guided levels show the command first. Alone levels give the goal only. Bosses and exams are timed.
Jump to a world
World 1The Reading Room
4 levelsBefore the big book, a reading lesson: SQL one word at a time, each one shown, tried and bent once.
Level 1: Ask for columns
Objective: Ask the departments and products tables for their columns, one change at a time.
SQL asks a database for rows. SELECT names the columns you want, FROM names the table, and a semicolon ends the question. From the shell: sqlite3 FILE "SQL;"
Commands taught
sqlite3sql-tablessql-selectLevel 2: Pick the rows
Objective: Filter rows with WHERE: a number, a text, a comparison, two conditions, then a count.
WHERE keeps only the rows that match a condition: = equal, <> not equal, < > <= >= compare. Text goes in single quotes, numbers don't. AND joins two conditions; count(*) counts rows instead of printing them.
Commands taught
sql-whereLevel 3: Sort and cut
Objective: Sort products by price, flip the order, keep the top 3, then print them neatly and as CSV.
ORDER BY column sorts the rows, smallest first; add DESC for biggest first. LIMIT n keeps the first n. sqlite3 -header -column prints neat columns with names; -csv -header prints CSV for files.
Commands taught
sql-order-limitLevel 4: Write in pencil
Objective: In practice.db: insert a note, mark it done, delete done notes, search with LIKE, then create a table.
Four writing words, practised on a scratch file (~/hall/practice.db, table notes): INSERT adds a row, UPDATE … SET changes rows, DELETE removes rows, CREATE TABLE makes a table. WHERE picks the rows for UPDATE and DELETE.
Commands taught
sql-insertsql-updatesql-deletesql-create-table
World 2The Ledger Hall
11 levelsAcme Parcel Co. keeps everything in one book: customers, orders, products, staff. Learn to read it, ask it questions and change it without tearing pages.
Level 5: Open the book
Objective: List the tables in ~/hall/acme.db and tell Kern how many there are.
A database keeps data in tables of rows and named columns: in one file (sqlite) or on a server (PostgreSQL, MariaDB). sqlite3 FILE opens one; dot-commands like .tables talk to the tool itself, and work as an argument too: hidden
Level 6: The shape of a page
Objective: Find the column of the orders table that holds when the order was placed, and tell Kern its name.
Every table has a fixed set of columns, each with a name and a type (INT, VARCHAR, DATE, DECIMAL…). .schema TABLE prints the CREATE TABLE statement that made it: the fastest way to learn what's inside before you ask anything.
Commands taught
sql-schemaLevel 7: The first question
Objective: Answer the HR ticket in ~/hall/TICKET: tell Kern the employee's email address.
SELECT asks for columns, FROM names the table, WHERE picks rows: SELECT email FROM employees WHERE id = 12; Put the SQL in double quotes after the file name, and end it with a semicolon. Text values go in single quotes; numbers don't.
Level 8: Narrow it down
Objective: Answer the marketing question in ~/hall/TICKET: tell Kern how many customers match.
WHERE can combine conditions with AND / OR, and compare with = <> < > <= >=. Dates stored as text like 2024-05-17 sort correctly, so a year is created_at >= '2024-01-01' AND created_at < '2025-01-01'. count(*) counts the rows that match instead of printing them.
Level 9: Top of the list
Objective: Answer the sales question in ~/hall/TICKET: tell Kern the product's name.
ORDER BY column sorts the rows (smallest first; add DESC for biggest first), LIMIT n keeps only the first n. For eyes, sqlite3 -header -column prints neat columns with their names; for files, -csv -header prints CSV.
Level 10: A new face
Objective: Add the new customer described in ~/hall/TICKET to the customers table.
INSERT INTO table (col1, col2, …) VALUES (v1, v2, …); adds a row. Naming the columns makes it clear which value goes where. Text in single quotes, numbers bare.
Level 11: One price, not forty
Objective: Apply the price change in ~/hall/TICKET to that one product.
UPDATE table SET column = value WHERE …; changes the rows WHERE picks, and only those. Without WHERE it changes EVERY row. A safe habit: run SELECT with the same WHERE first, see the rows, then turn it into the UPDATE.
Level 12: Sweep the test rows
Objective: Do what ~/hall/TICKET asks: delete the test accounts and nothing else.
DELETE FROM table WHERE …; removes the rows WHERE picks (no WHERE = all of them). LIKE matches text patterns: % is any run of characters, _ exactly one. '%@test.invalid' means 'ends with @test.invalid'; '%test%' means 'contains test' anywhere.
Level 13: A new shelf
Objective: Create the suppliers table from ~/hall/TICKET and add its two rows.
CREATE TABLE name (column TYPE constraints, …); makes a table. INTEGER PRIMARY KEY = a unique id per row; NOT NULL = the value is required; UNIQUE = no two rows share it. Constraints are rules the database enforces on every insert, forever.
Level 14: Restock, now
Objective: Produce the CSV the warehouse asks for in ~/hall/TICKET, at ~/hall/restock.csv.
Other teams want spreadsheets, not terminals. sqlite3 -csv -header FILE "SELECT …;" > report.csv writes the result as CSV with the column names on the first line.
Level 15: Closing night
Objective: Work through the four jobs in ~/hall/TICKET, in order.
Everything at once, on the real book: count with two conditions, insert new rows, change one category's prices without touching the others, and hand over a CSV.
World 3The Crossroads
14 levelsTables meet here: orders point at customers, items at products, employees at their managers. Join them, total them, speed them up, keep a copy safe.
Level 16: Where tables meet
Objective: Try JOIN, GROUP BY, sum() AS, LEFT JOIN and HAVING once each on acme.db.
Five new SQL words before the Crossroads asks for them: JOIN … ON, GROUP BY, sum() with AS, LEFT JOIN … IS NULL and HAVING. Same ladder as before: see it, copy it, read what it printed.
Commands taught
sql-joinsql-group-bysql-aggregatessql-left-joinsql-havingLevel 17: Who ordered?
Objective: Answer the courier ticket in ~/hall/TICKET: tell Kern the customer's name.
orders has customer_id, a pointer to customers.id. JOIN glues the two tables row by row where the ON condition holds: FROM orders o JOIN customers c ON c.id = o.customer_id. o and c are short names (aliases) so you can say o.id and c.name.
Level 18: Count by kind
Objective: Write the re-usable query ~/hall/TICKET asks for into ~/hall/status.sql.
GROUP BY col makes one row per distinct value; count(*), sum(x), avg(x), min(x), max(x) then work per group. SELECT status, count(*) FROM orders GROUP BY status; A query you re-run belongs in a file: hidden runs it.
Level 19: Follow the money
Objective: Answer the finance question in ~/hall/TICKET: tell Kern the amount.
Joins chain: order_items → orders (for the date) and order_items → products (for the category). sum(i.qty * i.unit_price) adds up the money of every row left after WHERE. round(x, 2) keeps two decimals.
Level 20: The silent ones
Objective: Answer the marketing question in ~/hall/TICKET: tell Kern how many.
LEFT JOIN keeps every row of the left table, even with no match on the right; the right side's columns are then NULL. So 'customers with no orders' = customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL. (= NULL never matches: use IS NULL.)
Level 21: The regulars
Objective: Write the loyalty query ~/hall/TICKET asks for into ~/hall/loyal.sql.
WHERE filters rows before grouping; HAVING filters groups after: … GROUP BY c.id HAVING count(*) >= 8. Group by the id, not the name: two customers can share a name.
Level 22: Who's the boss?
Objective: Answer the HR question in ~/hall/TICKET: tell Kern the manager's full name.
employees.manager_id points at another row of employees. Join the table to itself with two aliases: employees e JOIN employees m ON m.id = e.manager_id. e is the employee, m is the manager.
Commands taught
sql-self-joinLevel 23: The DBA's toolbelt
Objective: Try each tool once: a plan, an index, a cancelled transaction, a dump, a backup and a sorted log.
The admin tools of the next levels, tried once each: EXPLAIN QUERY PLAN, CREATE INDEX, BEGIN … ROLLBACK, .dump, .backup, and sort -t -k to read a log of timings.
Commands taught
sql-indexsql-transactionssql-backupLevel 24: Stop reading every page
Objective: Make the query in ~/hall/TICKET use an index, and prove it with EXPLAIN QUERY PLAN.
Without an index, WHERE ordered_at = … reads every row (SCAN). An index is a sorted copy of a column pointing back at the rows, so the database jumps to the matches (SEARCH … USING INDEX); EXPLAIN QUERY PLAN shows which. CREATE INDEX idx ON table (column);
Level 25: All or nothing
Objective: Record the phone order in ~/hall/TICKET: three changes, one transaction.
A transaction groups statements: BEGIN; … COMMIT; Either all of them happen or none do. If anything fails (or the machine dies) before COMMIT, the database rolls back to before BEGIN. ROLLBACK; cancels on purpose.
Level 26: Two copies
Objective: Make the two backups ~/hall/TICKET asks for in ~/backups/.
.dump prints the whole database as SQL (CREATE TABLE + INSERT lines): readable, diffable, portable to other engines with some edits. .backup FILE makes a binary copy that's safe even while others write. Restore a dump with sqlite3 new.db < dump.sql.
Level 27: Just the one table
Objective: Bring back the dropped table described in ~/hall/TICKET without losing this morning's order.
Restoring a whole dump rolls EVERYTHING back to the backup's time, wiping today's work. Often you need one table: take only its CREATE TABLE and INSERT lines from the dump (grep), or load the dump into a scratch database and dump just that table from there.
Commands taught
sql-restore-tableLevel 28: Monday numbers
Objective: Write the report ~/hall/TICKET asks for into ~/hall/top5.sql.
A real report: three tables, a filter, a group, a total, a sort and a limit, in one query the CEO can re-run.
Level 29: The slow ledger
Objective: Work the slow-page ticket in ~/hall/TICKET: name the table, back up, index, answer.
A slow page, on the live book: find the slow query in the log, take a backup before you touch anything, add the index that fixes it, then answer the question it was asking.
For teachers
Use these levels with a class
SOLUTION HIDDEN Teachers see answer keys in the classroom.