SQL CTE practice: WITH and WITH RECURSIVE exercises
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Six exercises on naming steps with WITH: per-customer counts, order totals, spending filters and a ranked step, then two recursive CTEs that generate numbers and a week of dates.
Write each query, press Check answer and get told right away whether your result matches. Stuck? Take a hint, or open the answer. New to the topic? Read the SQL CTEs: the WITH clause guide first.
The tables
- customers (id, name, city, joined_on)
- menu_items (id, name, category, price, vegetarian)
- orders (id, customer_id, ordered_on, status)
- order_items (order_id, item_id, quantity)
- staff (id, name, role, city, manager_id, hired_on)
- 1
Write a CTE named
order_countsthat counts orders percustomer_idas order_count. Then return the customer_id and order_count of customers with 3 or more orders.From the lesson WITH: name your steps · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
WITH order_counts AS (SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id) SELECT customer_id, order_count FROM order_counts WHERE order_count >= 3;Expected result (SQLite): 2 rows
customer_id order_count 1 4 2 3 - 2
An order's total is the sum of quantity × price over its items. Using a CTE of each delivered order's total (join orders, order_items and menu_items), find the average delivered order total.
From the lesson WITH: name your steps · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
WITH order_totals AS (SELECT o.id, SUM(oi.quantity * m.price) AS total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN menu_items m ON m.id = oi.item_id WHERE o.status = 'delivered' GROUP BY o.id) SELECT AVG(total) FROM order_totals;Expected result (SQLite): 1 row
AVG(total) 27.153846153846153 - 3
Find customers whose delivered spending is over $50. Make a CTE
spendingwith each customer_id's total spent on delivered orders (sum of quantity × price) as spent, then join it to customers. Show name and spent.From the lesson WITH: name your steps · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
WITH spending AS (SELECT o.customer_id, SUM(oi.quantity * m.price) AS spent FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN menu_items m ON m.id = oi.item_id WHERE o.status = 'delivered' GROUP BY o.customer_id) SELECT c.name, s.spent FROM spending s JOIN customers c ON c.id = s.customer_id WHERE s.spent > 50;Expected result (both engines): 4 rows
name spent Maya Ortiz 66 Jonah Reed 78 Ellie Park 53 Lena Fischer 59 - 4
Now with a CTE named
ranked: find the most expensive item in each category, keeping ties. Rank with RANK() as price_rank, then show category, name and price where price_rank is 1.From the lesson Ranking with OVER · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
WITH ranked AS (SELECT category, name, price, RANK() OVER (PARTITION BY category ORDER BY price DESC) AS price_rank FROM menu_items) SELECT category, name, price FROM ranked WHERE price_rank = 1;Expected result (both engines): 4 rows
category name price dessert Tiramisu 6.5 drink Lemon Soda 3 pizza BBQ Meteor 15.5 side Wings 8.5 - 5
Now count up: generate the numbers 1 to 10 in a column called n.
From the lesson WITH RECURSIVE · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
WITH RECURSIVE nums(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM nums WHERE n < 10) SELECT n FROM nums;Expected result (both engines): first 8 of 10 rows
n 1 2 3 4 5 6 7 8 - 6
Generate every day from 2026-03-01 to 2026-03-07 in a column called day.
From the lesson WITH RECURSIVE · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer in SQLite
WITH RECURSIVE days(day) AS (SELECT '2026-03-01' UNION ALL SELECT date(day, '+1 day') FROM days WHERE day < '2026-03-07') SELECT day FROM days;In PostgreSQL
WITH RECURSIVE days(day) AS (SELECT DATE '2026-03-01' UNION ALL SELECT day + 1 FROM days WHERE day < DATE '2026-03-07') SELECT day FROM days;Expected result (both engines): 7 rows
day 2026-03-01 2026-03-02 2026-03-03 2026-03-04 2026-03-05 2026-03-06 2026-03-07
The lessons have more like these
Each lesson teaches the idea step by step first, then gives you exercises with instant feedback and a daily streak.
- WITH: name your steps5 more exercises · sqlstreak Pro
- Ranking with OVER8 more exercises · sqlstreak Pro
- WITH RECURSIVE4 more exercises · sqlstreak Pro
More practice sets
Check your level
The free SQL test takes about 10 minutes and tells you which lesson to start with.