cswork / libraryOpen practice workspace

Coding questions · Netflix

Write SQL for DAU and first-purchase conversion

This question evaluates a candidate's practical SQL and data manipulation skills, including aggregations, distinct counts, joins, date-range handling, and identification of first-time purchases for conversion metrics.

Today is 2025-09-01. Using the schema and sample data below, write a single ANSI-SQL query that returns one row per day for the last 7 days (2025-08-26 to 2025-09-01 inclusive) with columns: day (DATE), dau (distinct users with a 'session' event that day), new_buyers (users whose first-ever order occurs that day), and conv_rate (new_buyers/dau rounded to 2 decimals; return 0.00 when dau=0). Rules: count only event_type='session' for DAU; a user's 'first-ever' order is the minimum order_date across all their orders; include dates with zero activity. Schema: users(user_id INT PRIMARY KEY, signup_date DATE); events(user_id INT, event_date DATE, event_type VARCHAR); orders(order_id INT PRIMARY KEY, user_id INT, order_date DATE, amount DECIMAL(10,2)). Sample tables: users

+---------+-------------+
| user_id | signup_date |
+---------+-------------+
| 1 | 2025-08-15 |
| 2 | 2025-08-30 |
| 3 | 2025-09-01 |
| 4 | 2025-08-10 |
+---------+-------------+
events
+---------+------------+------------+
| user_id | event_date | event_type |
+---------+------------+------------+
| 1 | 2025-08-26 | session |
| 1 | 2025-08-27 | session |
| 1 | 2025-09-01 | session |
| 2 | 2025-08-31 | session |
| 2 | 2025-09-01 | session |
| 3 | 2025-09-01 | session |
| 4 | 2025-08-26 | session |
| 4 | 2025-08-26 | click |
| 4 | 2025-08-27 | session |
+---------+------------+------------+
orders
+----------+---------+------------+--------+
| order_id | user_id | order_date | amount |
+----------+---------+------------+--------+
| 101 | 1 | 2025-08-27 | 50.00 |
| 102 | 2 | 2025-09-01 | 20.00 |
| 103 | 4 | 2025-08-26 | 15.00 |
| 104 | 1 | 2025-09-01 | 25.00 |
+----------+---------+------------+--------+

DAU and first-purchase conversion rate by day (last 7 days)

Assume the current date is 2025-06-01. Write a single PostgreSQL query that returns one row per day for the last 7 days (2025-05-26 to 2025-06-01 inclusive) with the following columns:

  • day (DATE)
  • dau: distinct users who have an event with event_type = 'session' on that day
  • new_buyers: users whose first-ever order occurs on that day (i.e., their minimum order_date across all their orders equals that day)
  • conv_rate: new_buyers / dau rounded to 2 decimals; return 0.00 when dau = 0

Additional rules:

  • Count only event_type = 'session' for DAU (ignore other event types).
  • Include dates with zero activity (still return a row for every day in the range).

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
signup_dateDATENOT NULL

Sample data:

signup_dateuser_id
2025-05-151
2025-05-302
2025-06-013
2025-05-104

Table events

columntypeconstraints
user_idINTNOT NULL
event_dateDATENOT NULL
event_typeVARCHAR(20)NOT NULL

Sample data:

event_dateevent_typeuser_id
2025-05-26session1
2025-05-27session1
2025-06-01session1
2025-05-31session2
2025-06-01session2
2025-06-01session3
2025-05-26session4
2025-05-26click4
2025-05-27session4

Table orders

columntypeconstraints
order_idINTPRIMARY KEY, NOT NULL
user_idINTNOT NULL
order_dateDATENOT NULL
amountDECIMAL(10,2)NOT NULL

Sample data:

amountorder_dateorder_iduser_id
502025-05-271011
202025-06-011022
152025-05-261034
252025-06-011041

Expected output

7 rows (one per day from 2025-05-26 through 2025-06-01). Columns: day, dau, new_buyers, conv_rate.

conv_ratedaudaynew_buyers
0.522025-05-261
0.522025-05-271
002025-05-280
002025-05-290
002025-05-300
012025-05-310
0.3332025-06-011

Hints

  1. Build a date spine for 2025-05-26 through 2025-06-01 so you can return days with zero activity.
  2. Compute first-ever purchase date per user using MIN(order_date), then count how many users have first_order_date = day.
  3. Use COALESCE for missing counts and a CASE expression to avoid division by zero when dau = 0.