Number Tracks Within Albums
You need to assign a sequential number to each track within its album, ordered by the track's position (TrackId). This...
You need to assign a sequential number to each track within its album, ordered by the track's position (TrackId). This...
Create a leaderboard of artists ranked by how many albums they've released. Artists with the same number of albums...
Create a pricing tier report where tracks are ranked by their unit price. Use DENSE_RANK so there are no gaps in...
For each customer's invoice, show the previous and next invoice dates. This helps analyze purchase frequency patterns....
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...
The executive team wants to understand revenue distribution across music genres. For each genre, calculate the total...
Finance needs a revenue growth analysis showing monthly revenue alongside the previous month and the percentage growth...
The international marketing team needs to know the most popular music genre in each country by purchase count. This...
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 finance team needs a cumulative revenue growth chart for key markets. Show monthly revenue alongside the running...
The product team wants to identify customers with the most diverse music taste — those who listen across many genres....
The retention team wants to understand purchase frequency patterns. Calculate the average, minimum, and maximum gap (in...
The analytics team wants to track how genre popularity changes over time. Calculate the year-over-year revenue change...
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.
Calculate across related rows without collapsing them: running totals, rankings and month-over-month changes in one query.
A window function computes a value across a set of rows related to the current row without collapsing those rows into one, which is the difference between SUM(amount) OVER (PARTITION BY user_id) and a GROUP BY. That is what makes running totals, per-customer rankings and month-over-month deltas possible in a single pass over the table. Every question below runs against a real database in the browser, so you write the OVER clause and see the result set immediately.
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
They differ only in how they treat ties.
SELECT name, score,
ROW_NUMBER() OVER (ORDER BY score DESC) AS rn,
RANK() OVER (ORDER BY score DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY score DESC) AS dense
FROM results;
-- scores 90, 85, 85, 70
-- rn: 1,2,3,4 rnk: 1,2,2,4 dense: 1,2,2,3WHERE runs before window functions are calculated, so it cannot see their results.
WITH ranked AS (
SELECT customer_id, amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id ORDER BY amount DESC
) AS rn
FROM orders
)
SELECT * FROM ranked WHERE rn = 1;GROUP BY collapses each group to one row. PARTITION BY keeps every row and adds the group's calculation beside it.
SELECT order_id, amount,
SUM(amount) OVER (PARTITION BY customer_id) AS customer_total,
ROUND(100.0 * amount /
SUM(amount) OVER (PARTITION BY customer_id), 1) AS pct
FROM orders;