Total Track Count
The product manager wants to know the total size of the music catalog. How many tracks are available in the store?...
The product manager wants to know the total size of the music catalog. How many tracks are available in the store?...
The content team wants to understand the distribution of tracks across different Genre. Which genres have the most...
The data science team is analyzing listening patterns. They want to know the average track length for each genre. Some...
The executive team wants to know which countries generate the most revenue. Create a report showing total sales by...
The pricing team wants to identify premium-priced Track. Find all tracks that are priced above the average track price...
The editorial team is writing a feature about the most prolific artists in the catalog. Find artists who have released...
Create a leaderboard of artists ranked by how many albums they've released. Artists with the same number of albums...
Calculate a running total (cumulative sum) of invoice amounts for each customer, showing how their total spending...
The marketing team wants to segment customers into 4 groups (quartiles) based on their total spending. This will help...
Common Table Expressions (CTEs) let you define named subqueries that make complex queries more readable. Let's start...
Use a CTE to find the longest track in each genre. CTEs are especially useful when you need to reference aggregated...
A correlated subquery references the outer query. Find tracks that are longer than the average length of tracks in...
Use CASE within aggregation functions to count items by category in a single query. Count tracks by price tier without...
Calculate multiple statistics about tracks in a single query: count, average length, min price, max price, and total...
Group data by multiple columns to get more granular insights. Calculate the number of invoices and total revenue by...
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.
Collapse many rows into one summary row per group.
Aggregation collapses many rows into one summary row per group: GROUP BY names the grouping columns, and SUM, COUNT, AVG, MIN and MAX describe each group. HAVING then filters the groups themselves, which is why it can reference an aggregate while WHERE cannot. This is the largest topic in the question bank because almost every analyst task, from revenue by month to active users per plan, is an aggregation with the right grouping key.
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
WHERE filters rows before grouping. HAVING filters groups after aggregation.
SELECT region, SUM(amount) AS total
FROM orders
WHERE status = 'paid' -- row filter, before grouping
GROUP BY region
HAVING SUM(amount) > 1000; -- group filter, after groupingThey count different things: all rows, non-NULL values, or unique non-NULL values.
Every selected column must be in GROUP BY or inside an aggregate, because the database cannot pick one value for the whole group.
SELECT COUNT(*) AS order_rows,
COUNT(coupon_code) AS orders_with_coupon,
COUNT(DISTINCT user_id) AS unique_customers
FROM orders;-- error: name is neither grouped nor aggregated
SELECT customer_id, name, SUM(amount) FROM sales GROUP BY customer_id;
-- fix: group by it as well
SELECT customer_id, name, SUM(amount) FROM sales GROUP BY customer_id, name;