Skip to the levels
ROOTSPIRE
DiscsFor teachers
▶ Play freeLibrary 16 discsDiscsFor teachersLog in

Every level, in the open

Databases

3 worlds · 29 levels · 2 bosses

SOLUTION HIDDENTeachers see answer keys in the classroom.

▶ Play this discAll discs
All discsLinux: Zero to RootBash ScriptingGitDatabasesPython for OpsLinux Sysadmin ProNetworkingObservabilityCloud & IaCDocker & ContainersCTF ArenaSOC FieldWindows for Linux peopleKubernetesAnsibleThe Guildhall: First Week

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 1 The Reading RoomWorld 2 The Ledger HallWorld 3 The Crossroads

World 1The Reading Room

4 levels

Before the big book, a reading lesson: SQL one word at a time, each one shown, tried and bent once.

  1. 1

    Level 1: Ask for columns

    GuidedEasy

    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 taughtsqlite3sql-tablessql-select

    ▶ Play: Ask for columnsSOLUTION HIDDEN10 XP
  2. 2

    Level 2: Pick the rows

    GuidedEasy

    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 taughtsql-where

    ▶ Play: Pick the rowsSOLUTION HIDDEN10 XP
  3. 3

    Level 3: Sort and cut

    GuidedEasy

    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 taughtsql-order-limit

    ▶ Play: Sort and cutSOLUTION HIDDEN10 XP
  4. 4

    Level 4: Write in pencil

    GuidedEasy

    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 taughtsql-insertsql-updatesql-deletesql-create-table

    ▶ Play: Write in pencilSOLUTION HIDDEN15 XP

World 2The Ledger Hall

11 levels

Acme Parcel Co. keeps everything in one book: customers, orders, products, staff. Learn to read it, ask it questions and change it without tearing pages.

  1. 5

    Level 5: Open the book

    GuidedEasy

    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

    ▶ Play: Open the bookSOLUTION HIDDEN15 XP
  2. 6

    Level 6: The shape of a page

    GuidedEasy

    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 taughtsql-schema

    ▶ Play: The shape of a pageSOLUTION HIDDEN15 XP
  3. 7

    Level 7: The first question

    GuidedEasy

    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.

    ▶ Play: The first questionSOLUTION HIDDEN20 XP
  4. 8

    Level 8: Narrow it down

    GuidedEasy

    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.

    ▶ Play: Narrow it downSOLUTION HIDDEN20 XP
  5. 9

    Level 9: Top of the list

    GuidedEasy

    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.

    ▶ Play: Top of the listSOLUTION HIDDEN20 XP
  6. 10

    Level 10: A new face

    GuidedEasy

    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.

    ▶ Play: A new faceSOLUTION HIDDEN20 XP
  7. 11

    Level 11: One price, not forty

    AloneMedium

    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.

    ▶ Play: One price, not fortySOLUTION HIDDEN30 XP
  8. 12

    Level 12: Sweep the test rows

    AloneMedium

    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.

    ▶ Play: Sweep the test rowsSOLUTION HIDDEN30 XP
  9. 13

    Level 13: A new shelf

    GuidedEasy

    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.

    ▶ Play: A new shelfSOLUTION HIDDEN25 XP
  10. 14

    Level 14: Restock, now

    Alone⏱ TimedHard

    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.

    ▶ Play: Restock, nowSOLUTION HIDDEN45 XP
  11. 15

    Level 15: Closing night

    Boss⏱ TimedHard

    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.

    ▶ Play: Closing nightSOLUTION HIDDEN100 XP

World 3The Crossroads

14 levels

Tables meet here: orders point at customers, items at products, employees at their managers. Join them, total them, speed them up, keep a copy safe.

  1. 16

    Level 16: Where tables meet

    GuidedEasy

    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 taughtsql-joinsql-group-bysql-aggregatessql-left-joinsql-having

    ▶ Play: Where tables meetSOLUTION HIDDEN10 XP
  2. 17

    Level 17: Who ordered?

    GuidedEasy

    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.

    ▶ Play: Who ordered?SOLUTION HIDDEN20 XP
  3. 18

    Level 18: Count by kind

    GuidedEasy

    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.

    ▶ Play: Count by kindSOLUTION HIDDEN20 XP
  4. 19

    Level 19: Follow the money

    GuidedEasy

    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.

    ▶ Play: Follow the moneySOLUTION HIDDEN25 XP
  5. 20

    Level 20: The silent ones

    GuidedEasy

    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.)

    ▶ Play: The silent onesSOLUTION HIDDEN25 XP
  6. 21

    Level 21: The regulars

    AloneMedium

    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.

    ▶ Play: The regularsSOLUTION HIDDEN30 XP
  7. 22

    Level 22: Who's the boss?

    AloneMedium

    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 taughtsql-self-join

    ▶ Play: Who's the boss?SOLUTION HIDDEN30 XP
  8. 23

    Level 23: The DBA's toolbelt

    GuidedEasy

    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 taughtsql-indexsql-transactionssql-backup

    ▶ Play: The DBA's toolbeltSOLUTION HIDDEN10 XP
  9. 24

    Level 24: Stop reading every page

    GuidedEasy

    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);

    ▶ Play: Stop reading every pageSOLUTION HIDDEN30 XP
  10. 25

    Level 25: All or nothing

    GuidedEasy

    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.

    ▶ Play: All or nothingSOLUTION HIDDEN30 XP
  11. 26

    Level 26: Two copies

    GuidedEasy

    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.

    ▶ Play: Two copiesSOLUTION HIDDEN30 XP
  12. 27

    Level 27: Just the one table

    AloneMedium

    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 taughtsql-restore-table

    ▶ Play: Just the one tableSOLUTION HIDDEN35 XP
  13. 28

    Level 28: Monday numbers

    Alone⏱ TimedHard

    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.

    ▶ Play: Monday numbersSOLUTION HIDDEN50 XP
  14. 29

    Level 29: The slow ledger

    Boss⏱ TimedHard

    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.

    ▶ Play: The slow ledgerSOLUTION HIDDEN110 XP

For teachers

Use these levels with a class

SOLUTION HIDDEN Teachers see answer keys in the classroom.

For teachers
ROOTSPIRE
DiscsFor teachers
▶ Play freeLibrary 16 discsDiscsFor teachersLog in