Pareto Analysis: Top 20% Customers Revenue Share
Apply the Pareto principle (80/20 rule) to customer revenue. Find the top 20% of customers and show what percentage of...
Apply the Pareto principle (80/20 rule) to customer revenue. Find the top 20% of customers and show what percentage of...
For each country with customers, identify the top 3 spenders using a window function. Schema: Customer ColumnType...
Compute cohort retention: for each customer signup-month (first invoice), how many of that cohort were active in month...
Produce a monthly report combining MoM revenue growth with the top-spending customer in that month. Schema: Customer...
Segment customers into quartiles by lifetime value and compute days since their most recent purchase. Schema: Customer...
For genres present in every calendar year of available data, compute year-over-year revenue growth. Schema: InvoiceLine...
For each genre, find the top 3 tracks by units sold. Schema: InvoiceLine ColumnType InvoiceLineIdINTEGER (PK)...
For each customer, compute their shortest gap (in days) between any two consecutive purchases. This surfaces the most...
Apply the 80/20 rule: identify the smallest set of customers that together generate at least 80% of total revenue....
Classic RFM segmentation: score every customer on Recency (R), Frequency (F), and Monetary (M), then assign a segment...
Marketing wants to segment customers into four equal-sized tiers by lifetime spend, to target the top tier with a...
For every invoice, assign a sequential number within its customer, ordered by InvoiceDate -- so each customer's first...
Compute each artist's total track count (across all their albums) and rank artists by that count, highest first, using...
Compute each customer's total number of invoices, then rank customers by that count (most invoices first) using...
For each of a customer's invoices, show the Total of their next invoice (chronologically) and the change between the...
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;