SQL QuestSQL Interview Questions › Subqueries & CTEs

Cumulative Distinct Customers Over Time

HardFreeQuerying BasicsSubqueries & CTEsWindow FunctionsAggregation & Grouping

Calculate the cumulative number of unique customers over time. For each distinct order_date, show order_date, new_customers (customers ordering for the first time on that date), and cumulative_customers (running total of unique customers up to that date). Sort by order_date. Cumulative distinct counting is a core product analytics question at Meta and Spotify because COUNT(DISTINCT) doesn't work as a window function.

Solve it in the browser editor →

Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".

Schema

orders

order_idcustomer_idproductcategoryquantitypricetotalorder_datecountrystatus
11Laptop ProElectronics11299.991299.992024-01-15USAcompleted
22Wireless MouseElectronics249.9999.982024-01-16Canadacompleted
33Office ChairFurniture1349.99349.992024-01-17USAcompleted

Expected output: Jan-1: new=2, cumulative=2. Jan-2: new=1, cumulative=3

Hint

Step 1: Find each customer's MIN(order_date) as first_order_date. Step 2: COUNT customers per first_order_date. Step 3: SUM() OVER (ORDER BY date) for the running total.

Concepts

SELECT CTE Window Functions GROUP BY Window Functions + CTE

Practise the topic: SQL practice questions · CTE practice · Window function practice · GROUP BY exercises · Advanced SQL interview questions

In these company practice sets

Airbnb · Anthropic · Meta · Netflix · OpenAI · Ramp · Revolut · Snowflake · Spotify · Stripe

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Running Total RevenueHard · FreeYear-over-Year GrowthHard · Free7-Day Rolling Revenue AverageHard · FreeMulti-CTE Revenue PipelineHard · FreeWealthy Survivor ProfileHard · ProOrder Sessionization by CustomerHard · Pro

Where would this cost you points in an interview?

Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.

Take the readiness test