HAVING with Multiple Conditions
Find genres that are both popular (many tracks) AND have high-quality tracks (high average file size). Use HAVING with...
Find genres that are both popular (many tracks) AND have high-quality tracks (high average file size). Use HAVING with...
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...
An INNER JOIN only returns matching rows. Use LEFT JOIN to include ALL artists, even those without any albums. Write a...
Count how many unique composers have created Rock music. Use COUNT with DISTINCT to count unique values. Write a query...
For each album, find the shortest and longest track durations. This helps identify albums with consistent vs. varied...
Understand the difference between WHERE (filters rows before grouping) and HAVING (filters groups after aggregation)....
The executive team wants to understand revenue distribution across music genres. For each genre, calculate the total...
The marketing team wants to identify above-average spenders within each country to target premium campaigns. Compare...
The CRM team wants to segment customers into value tiers based on their lifetime spending. Classify each customer as...
Management wants to compare the sales performance of support representatives. Calculate each rep's customer count,...
The product team wants to identify customers with the most diverse music taste — those who listen across many genres....
The data team discovered that some artists have tracks in multiple genres. Find these cross-genre artists and show...
The retention team wants to understand purchase frequency patterns. Calculate the average, minimum, and maximum gap (in...
Apply the Pareto principle (80/20 rule) to customer revenue. Find the top 20% of customers and show what percentage of...
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;