Push Opens by Platform and Campaign

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
L3
Asked in
ESPN

Problem

Every notification sent is a row in push_notifs, carrying the platform it went to and an opened flag. Find how many unique users opened a notification on each platform, listing the platform with the most openers first.

Opens by platform and campaign.

Tables

push_notifs

Columns: notif_id INTEGER, user_id INTEGER, title TEXT, platform TEXT, status TEXT, sent_at TEXT, opened INTEGER, campaign TEXT

push_notifs: 10 of 200 rows
notif_iduser_idtitleplatformstatussent_atopenedcampaign
1053100New message from teamandroidpending2026-02-02 01:07:000winback_feb
1106197Weekly digest readywebfailed2026-03-03 02:14:000promo_summer
1159294Price drop alertiOSbounced2026-04-04 03:21:001onboard_v2
1212391Security alertAndroidDelivered2026-05-05 04:28:000NULL
1265488Payment receivedWEBFAILED2026-06-06 05:35:00NULLloyalty_2024
1318585Complete your profilebasicdelivered2026-07-07 06:42:001NULL
1371682New feature availablepremiumpending2026-08-08 07:49:000reengagement
1424779Subscription renewaliosfailed2026-09-09 08:56:000NULL
1477876Flash sale: 50% offandroidbounced2026-01-10 09:03:001flash_sale
1530973Your order shipped!webDelivered2026-02-11 10:10:00NULLretention_q1

Expected output

First 5 of 8 rows
platformunique_openers
ios5
iOS4
basic4
web3
premium3

Worked solution and explanation

This is a count of people dressed up as a count of events. push_notifs holds one row per notification, so a user who opens two notifications on the same platform appears twice. The question asks how many people opened, not how many opens happened. Everyone writes the WHERE opened = 1 and the GROUP BY platform. What separates candidates is whether the aggregate counts users or rows. Write COUNT(*) and nearly every platform doubles on this data: 'ios' reads 9 instead of 5. The comparison the engagement team wanted then measures how often repeat openers come back, not how many users respond.

Walking to the query

Step 1: Keep only real opens

opened is 1, 0 or NULL. A NULL flag means no open was ever recorded, so it is not an open. opened = 1 drops the zeros and the NULLs in one go, because NULL = 1 is never true. That is exactly the behavior you want, so no extra guard is needed.

Step 2: Group on platform as it is stored

The column carries 'ios' and 'iOS' as separate labels, plus odd values like 'basic' and 'premium'. The expected output keeps each stored label on its own row, so the grain is the label in the table. Merging spellings would be a different answer.

Step 3: Count people, not rows

COUNT(DISTINCT user_id) collapses a user's repeat opens on one platform into a single opener. This is the line the whole problem turns on.

Step 4: Sort by the count you just named

ORDER BY unique_openers DESC reuses the alias. Four platforms tie at 3 and three at 4. The question only asks for most openers first, so tied platforms may come in any order.

Unique openers per `platform`
SELECT platform, COUNT(DISTINCT user_id) AS unique_openers
FROM push_notifs
WHERE opened = 1
GROUP BY platform
ORDER BY unique_openers DESC

One pass over push_notifs: filter to opens, collapse users per label, sort by the count.

Counting rows
COUNT(*) returns 'ios' 9, 'iOS' 8, 'android' 8, 'basic' 7. Each opener's second row is counted again, so the numbers reward repeat behavior.
Counting users
COUNT(DISTINCT user_id) returns 'ios' 5, then 'android', 'basic' and 'iOS' level at 4. One user is one opener, which is what 'how many users' means.

Common follow-up questions