SQL Koans
SQL is how you ask a database questions. You describe the rows you want, and the database works out how to find them, whether there are ten or ten million. Nearly every application keeps its data in a SQL database, so it’s useful to developers, analysts and anyone who works with data. These koans run on SQLite, a small database engine built into countless apps, and cover queries, grouping, joins, changing data, subqueries, window functions and a few classic puzzles.
72 koans in 9 groups, tested with sqllogictest-style expectations. Each is a small program whose tests fail: replace every ___ until they pass.
Awakening
How SQL koans check a query, and the first things to ask a table.
Expressions and functions
Numbers, strings, patterns, CASE, and dates.
- Numbers and types
- Strings
- LIKE and GLOB
- CASE
- Dates
- Type affinity
- Arithmetic functions
- COALESCE and NULLIF
- More string functions
- Date arithmetic
- Collation
- Math functions
Grouping
Aggregate functions, GROUP BY, HAVING, DISTINCT and group_concat.
Joins
Combining tables: inner and outer joins, joining a table to itself, and set operations.
- Inner joins
- Left joins
- Self joins
- Set operations
- Cross joins
- Joins and aggregates
- Semi- and anti-joins
- USING and NATURAL
- Many to many
Changing data
INSERT, UPDATE and DELETE, constraints, keys, upserts, and transactions.
- INSERT, UPDATE and DELETE
- Constraints
- Keys
- Upsert
- Transactions
- INSERT … SELECT and UPDATE … FROM
- RETURNING
- Defaults and generated columns
- Altering tables
- Indexes
Subqueries and CTEs
Queries inside queries, correlated subqueries, EXISTS, and WITH, recursion included.
- Subqueries
- Common table expressions
- Recursive CTEs
- Correlated subqueries
- EXISTS and IN
- Walking a hierarchy
Window functions
Calculations across rows related to the current one, without grouping them away.
SQLite features
JSON, views, triggers, and strict tables.
Puzzles
Classic problems that combine what came before.