Tracks Above Average Price
The pricing team wants to identify premium-priced Track. Find all tracks that are priced above the average track price...
Nest one query inside another when you need its answer first.
A subquery is a SELECT nested inside another statement, used when one query needs the answer to a smaller query first. A scalar subquery returns a single value and can sit in the SELECT list or a comparison, while a correlated subquery references the outer row and is re-evaluated for each of them. These questions build the judgement for when a subquery is the clearest tool and when a join or CTE would read better and run faster.
The pricing team wants to identify premium-priced Track. Find all tracks that are priced above the average track price...
A correlated subquery references the outer query. Find tracks that are longer than the average length of tracks in...
Use EXISTS to find customers who have made at least one purchase over $10. EXISTS is efficient for checking if related...
Use NOT EXISTS to find artists who don't have any albums in the catalog. This is a common pattern for finding orphan or...
Use a scalar subquery in the SELECT clause to add computed values. Show each artist alongside the total number of...
Calculate the average customer lifetime value (average of each customer's total spending). This requires aggregating...
Calculate each genre's share of total tracks as a percentage. This requires comparing individual counts to the overall...
Generate a single-row "catalog overview" for the platform's daily dashboard. Schema: Track ColumnType TrackIdINTEGER...
Find customers who have purchased across multiple calendar years — a proxy for loyalty. Schema: Customer ColumnType...
For each country with customers, identify the top 3 spenders using a window function. Schema: Customer ColumnType...
Find genres whose average track price exceeds the overall catalog average. Schema: Track ColumnType TrackIdINTEGER (PK)...
Show how many customers made their first-ever purchase each month — the new-customer cohort series. Schema: Invoice...
The finance team wants to spot unusually large orders. Find every invoice whose Total is greater than the average Total...
The catalog team wants to find tracks that have never appeared in any customer purchase, as candidates for promotion or...
Find every track whose UnitPrice is greater than the average UnitPrice across the entire catalog, using a scalar...
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
Both can often give the same result. Choose by readability and by which columns you need.
-- customers with at least one order
SELECT c.name
FROM customers c
WHERE EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.id
);A correlated subquery references a column from the outer query, so it is logically re-evaluated for every outer row.
SELECT e.name, e.salary
FROM employees e
WHERE e.salary > (
SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id
);If the subquery returns even one NULL, NOT IN returns no rows at all.
SELECT c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.id
);Track your streak & earn XP
Sign up free to unlock your progress dashboard, daily streaks, and leaderboard ranking.
10 role-based tests for Data Analysts, SQL Developers & Data Engineers with instant scorecards.