SQL subquery practice: 6 exercises with answers
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Six exercises on queries inside queries: a value worked out by a subquery, IN with a list from a subquery, and subqueries that turn counts into percentages and retention figures.
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 subqueries 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)
- users (id, signed_up_on, country, source)
- plays (id, user_id, played_on, minutes, device)
- subscriptions (user_id, started_on, plan, cancelled_on)
- 1
Which menu items cost more than the average menu price? Show name and price. Work the average out with a subquery, don't type the number.
From the lesson Subqueries · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, price FROM menu_items WHERE price > (SELECT AVG(price) FROM menu_items);Expected result (both engines): 5 rows
name price Margherita 11 Pepperoni Rocket 13.5 Four Cheese Orbit 14 BBQ Meteor 15.5 Veggie Nebula 13 - 2
Show the names of customers who have at least one cancelled order (status 'cancelled').
From the lesson Subqueries · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders WHERE status = 'cancelled');Expected result (both engines): 2 rows
name Priya Nair Tomás Herrera - 3
Find the most expensive pizza using a subquery (no LIMIT). Show its name and price. Both the subquery and the outer query should only look at the pizza category.
From the lesson Subqueries · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, price FROM menu_items WHERE category = 'pizza' AND price = (SELECT MAX(price) FROM menu_items WHERE category = 'pizza');Expected result (both engines): 1 row
name price BBQ Meteor 15.5 - 4
What percentage of users have a subscription? Divide the number of subscriptions by the number of users. Return one number named pct_subscribed, rounded to 1 decimal.
From the lesson Funnels and conversion · Streamly
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT ROUND(100.0 * (SELECT COUNT(*) FROM subscriptions) / (SELECT COUNT(*) FROM users), 1) AS pct_subscribed;Expected result (both engines): 1 row
pct_subscribed 33.3 - 5
For users who signed up in February 2026, show plays (all their plays) and listeners (how many of them played at least once), using two CTEs.
From the lesson Cohorts and retention · Streamly
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
WITH feb AS (SELECT id FROM users WHERE signed_up_on >= '2026-02-01' AND signed_up_on < '2026-03-01'), feb_plays AS (SELECT p.user_id FROM plays p JOIN feb ON feb.id = p.user_id) SELECT COUNT(*) AS plays, COUNT(DISTINCT user_id) AS listeners FROM feb_plays;Expected result (both engines): 1 row
plays listeners 851 89 - 6
Of users who signed up in January, what percentage played in their second week (days 7 to 13 after signup)? One value, rounded to 1 decimal.
From the lesson Cohorts and retention · Streamly
Write your query here…Show the answerHide the answer
Answer in SQLite
SELECT ROUND(100.0 * COUNT(DISTINCT p.user_id) / (SELECT COUNT(*) FROM users WHERE signed_up_on < '2026-02-01'), 1) FROM users u JOIN plays p ON p.user_id = u.id WHERE u.signed_up_on < '2026-02-01' AND julianday(p.played_on) - julianday(u.signed_up_on) BETWEEN 7 AND 13;In PostgreSQL
SELECT ROUND(100.0 * COUNT(DISTINCT p.user_id) / (SELECT COUNT(*) FROM users WHERE signed_up_on < '2026-02-01'), 1) FROM users u JOIN plays p ON p.user_id = u.id WHERE u.signed_up_on < '2026-02-01' AND p.played_on - u.signed_up_on BETWEEN 7 AND 13;Expected result (SQLite): 1 row
ROUND(100.0 * COUNT(DISTINCT p.user_id) / (SELECT COUNT(*) FROM users WHERE signed_up_on < '2026-02-01'), 1) 63.8
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.
- Subqueries7 more exercises · sqlstreak Pro
- Funnels and conversion9 more exercises · sqlstreak Pro
- Cohorts and retention6 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.