Ad Clickers
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L4
Problem
Which users clicked at least one ad in the ad_impressions log, and how much revenue did those clicks bring in? Show each clicker's username from users alongside their total click revenue, highest contributor first.
Who clicked? What did they spend?
Tables
users
Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT
| user_id | username | signup_date | account_status | age_bucket | |
|---|---|---|---|---|---|
| 100 | alice | alice@example.com | 2025-02-02 | inactive | 25-34 |
| 197 | aaron42 | aaron42@example.com | 2026-03-03 | suspended | 35-44 |
| 294 | amelia | amelia@example.com | 2024-04-04 | pending_verification | 45-54 |
| 391 | arjun | arjun@example.com | 2025-05-05 | active | 55-64 |
| 488 | ava99 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 585 | andrew_k | andrew_k@example.com | 2024-07-07 | suspended | NULL |
| 682 | anika | anika@example.com | 2025-08-08 16:34:00 | pending_verification | 18-24 |
| 779 | aiden | aiden@example.com | 2026-09-09 14:53:00 | active | 25-34 |
| 876 | aria_b | aria_b@example.com | 2024-10-10 | inactive | 35-44 |
| 973 | brian | brian@example.com | 2025-11-11 | suspended | 45-54 |
ad_impressions
Columns: impression_id INTEGER, user_id INTEGER, ad_campaign TEXT, impression_time TIMESTAMP, clicked BOOLEAN, revenue REAL
| impression_id | user_id | ad_campaign | impression_time | clicked | revenue |
|---|---|---|---|---|---|
| 253 | 100 | BRAND_AWARENESS_Q1 | 2026-02-02 01:07:00 | 0 | NULL |
| 306 | 197 | PRODUCT_LAUNCH_X | 2026-03-03 02:14:00 | 0 | NULL |
| 359 | 294 | RETARGETING_CART | 2026-04-04 03:21:00 | 0 | NULL |
| 412 | 391 | HOLIDAY_PROMO | 2026-05-05 04:28:00 | 0 | NULL |
| 465 | 488 | NEW_USER_ACQU | 2026-06-06 05:35:00 | 1 | 0.2 |
| 518 | NULL | LOYALTY_PROGRAM | 2026-07-07 06:42:00 | 0 | NULL |
| 571 | 682 | FLASH_SALE_48H | 2026-08-08 07:49:00 | 0 | NULL |
| 624 | 779 | SUMMER_SALE_2024 | 2026-09-09 08:56:00 | 0 | NULL |
| 677 | 876 | BRAND_AWARENESS_Q1 | 2026-01-10 09:03:00 | 0 | NULL |
| 730 | 973 | PRODUCT_LAUNCH_X | 2026-02-11 10:10:00 | 1 | 0.35 |
Expected output
| username | total_revenue |
|---|---|
| emma | 25.1 |
| dora_77 | 23.55 |
| dahlia | 22.5 |
| dimitri | 22 |
| devon | 21 |
Worked solution and explanation
What this really is
This is a filtered fact rolled up to a dimension label, and the whole question hangs on one predicate. clicked = 1 decides two things at once: which users appear and which rows feed the sum. Every unclicked row in ad_impressions has a NULL revenue, so a query that drops the filter leaves each clicker's total exactly right and quietly adds 9 users who only ever saw ads, each with a NULL total_revenue. The numbers you spot-check never show the bug; the row count does, 25 instead of 16. The join is there only because username lives on users: the money never leaves ad_impressions.
Building it
Step 1: Filter ad_impressions to clicks
WHERE imp.clicked = 1 drops every unclicked impression before the rows are grouped. A user with no clicks has no rows left, so no group ever forms for them, and 'clicked at least one ad' holds with no extra check. The flag holds 0 and 1, so compare it to 1.
Step 2: Join to users for the label
An inner JOIN on user_id brings in username. Impressions whose user_id is NULL match nothing and fall away; 3 clicked impressions on the seed are that case, and their revenue belongs to nobody the report can name.
Step 3: Total per username
GROUP BY u.username with SUM(imp.revenue) AS total_revenue. Usernames are unique in users, so this grain is the same as grouping by user_id. The alias is the output contract: the grader reads a column named total_revenue.
Step 4: Sort by the total
ORDER BY total_revenue DESC puts the biggest contributor first. No two clickers share a total on the seed, and tied rows may come back in any order, so one key is the whole sort.
SELECT
u.username,
SUM(imp.revenue) AS total_revenue
FROM users u
JOIN ad_impressions imp ON imp.user_id = u.user_id
WHERE imp.clicked = 1
GROUP BY u.username
ORDER BY total_revenue DESC- Click filter dropped
- Join
usersto every impression and sum: each clicker's total is unchanged because unclickedrevenueis NULL, but 9 users who only saw ads come back with a NULLtotal_revenue. Depending on how the engine sorts NULLs underDESC, they land at the top or hide at the bottom. - Click filter first
- Filter to
clicked = 1, then join and sum: 16 users, each with real click revenue, and nobody who never clicked.
SELECT
u.username,
SUM(imp.revenue) AS total_revenue
FROM users u
JOIN ad_impressions imp ON imp.user_id = u.user_id
GROUP BY u.username
ORDER BY total_revenue DESCReturns 25 rows: the 16 clickers plus 9 non-clickers with a NULL total_revenue.
Common follow-up questions
- How would you also return each user's click count? (Add `COUNT(*) AS click_count`; after the `WHERE`, every row counted is a click.)
- What changes if you only need the top 5 clickers? (Add `LIMIT 5`. If fifth place ties, the cut needs a tie rule, so the question names one and the `ORDER BY` gains that key, such as `username`, before the limit.)
- What if a clicked impression could have a NULL `revenue`? (`SUM` skips NULLs, so a user whose clicks all lack revenue shows a NULL total; decide whether that user belongs in a revenue report.)