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

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

ad_impressions

Columns: impression_id INTEGER, user_id INTEGER, ad_campaign TEXT, impression_time TIMESTAMP, clicked BOOLEAN, revenue REAL

ad_impressions: 10 of 200 rows
impression_iduser_idad_campaignimpression_timeclickedrevenue
253100BRAND_AWARENESS_Q12026-02-02 01:07:000NULL
306197PRODUCT_LAUNCH_X2026-03-03 02:14:000NULL
359294RETARGETING_CART2026-04-04 03:21:000NULL
412391HOLIDAY_PROMO2026-05-05 04:28:000NULL
465488NEW_USER_ACQU2026-06-06 05:35:0010.2
518NULLLOYALTY_PROGRAM2026-07-07 06:42:000NULL
571682FLASH_SALE_48H2026-08-08 07:49:000NULL
624779SUMMER_SALE_20242026-09-09 08:56:000NULL
677876BRAND_AWARENESS_Q12026-01-10 09:03:000NULL
730973PRODUCT_LAUNCH_X2026-02-11 10:10:0010.35

Expected output

First 5 of 16 rows
usernametotal_revenue
emma25.1
dora_7723.55
dahlia22.5
dimitri22
devon21

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.

Filter clicks, join for the label, total, 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 users to every impression and sum: each clicker's total is unchanged because unclicked revenue is NULL, but 9 users who only saw ads come back with a NULL total_revenue. Depending on how the engine sorts NULLs under DESC, 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.
The version that looks right and is not
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 DESC

Returns 25 rows: the 16 clickers plus 9 non-clickers with a NULL total_revenue.

Common follow-up questions