SQL exercises with solutions, topic by topic
Updated on
Here are 196 SQL exercises with solutions, taken from SpeedQL’s questions and sorted by the game’s 16 topics. Each exercise gives the task, the starting tables, a hint, the solution and the expected result, computed with SQLite. Try writing your query first, then open the solution.
The 16 SQL topics
- SQL exercises with solutions: SELECT, aliases and DISTINCT
4 exercises with solutions, from level 1 to level 1. SELECT picks the columns to show; AS renames a column, DISTINCT removes duplicates. - SQL exercises with solutions: WHERE and filters
15 exercises with solutions, from level 1 to level 4. WHERE keeps the rows that match a condition: =, <>, >, BETWEEN, IN, LIKE, AND, OR, NOT. - SQL exercises with solutions: ORDER BY and LIMIT
6 exercises with solutions, from level 1 to level 2. ORDER BY sorts (ASC ascending, DESC descending); LIMIT keeps the first n rows. - SQL exercises with solutions: COUNT, SUM, AVG, MIN, MAX
3 exercises with solutions, from level 1 to level 2. Aggregates sum up many rows into one value: COUNT, SUM, AVG, MIN, MAX. - SQL exercises with solutions: GROUP BY
15 exercises with solutions, from level 2 to level 3. GROUP BY computes one summary per group: one result row per value of the column. - SQL exercises with solutions: JOINs
15 exercises with solutions, from level 2 to level 8. JOIN links two tables through a shared column; LEFT JOIN also keeps the rows with no match. - SQL exercises with solutions: HAVING
12 exercises with solutions, from level 2 to level 3. HAVING filters groups after GROUP BY (WHERE filters rows before). - SQL exercises with solutions: subqueries
15 exercises with solutions, from level 2 to level 8. A subquery is a query inside a query: a value, a list (IN) or a table. - SQL exercises with solutions: CASE WHEN
15 exercises with solutions, from level 4 to level 7. CASE picks a value depending on conditions, row by row. - SQL exercises with solutions: NULL and COALESCE
14 exercises with solutions, from level 4 to level 8. NULL = missing value: test it with IS NULL / IS NOT NULL; COALESCE replaces it. - SQL exercises with solutions: UNION, INTERSECT, EXCEPT
11 exercises with solutions, from level 4 to level 4. UNION merges two results (no duplicates), INTERSECT keeps what they share, EXCEPT removes the second from the first. - SQL exercises with solutions: text and date functions
15 exercises with solutions, from level 2 to level 8. Text and dates: UPPER, LOWER, LENGTH, SUBSTR, ||, LIKE, strftime, julianday, date. - SQL exercises with solutions: CTEs (WITH)
15 exercises with solutions, from level 5 to level 8. WITH names an intermediate query, which you then reuse like a table. - SQL exercises with solutions: EXISTS and NOT EXISTS
15 exercises with solutions, from level 5 to level 8. EXISTS is true when the subquery returns at least one row; NOT EXISTS, when it returns none. - SQL exercises with solutions: window functions
15 exercises with solutions, from level 6 to level 8. Window functions compute over a set of rows without grouping them: ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, SUM() OVER… - SQL exercises with solutions: recursive CTEs
11 exercises with solutions, from level 7 to level 8. A recursive CTE calls itself to generate a sequence or walk a hierarchy. Always add a stop condition.
To go further: the SQL cheat sheet sums up each topic, and the custom exercises check your queries live, with every question in the game.