Seen or Ignored

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L3
Asked in
Meta

Problem

Every send our push notification system makes lands in push_notifs, along with whether the recipient opened it. The engagement team wants each platform's fraction of sent notifications that were opened, strongest open rate first.

A send is only half the story. Find where the taps actually land.

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
platformopen_ratio
ios0.375
iOS0.3076923076923077
basic0.2916666666666667
premium0.25
web0.23076923076923078

Worked solution and explanation

What this really tests

This is a denominator-definition problem wearing a push-notification costume. The metric looks trivial: opens over sends, split by platform. The question the interviewer is really probing: when some sends never report back an open signal (opened is NULL), do those rows belong in your denominator? The ask is the fraction of ALL sent notifications, so they do. Candidates who miss this quietly divide by the wrong number, and every platform looks better than it is.


Building it

Step 1: Count the confirmed opens

opened is a 0/1 flag, so SUM(opened) is the count of confirmed opens. A NULL contributes nothing to it, which is exactly right: an untracked send was not confirmed opened.

Step 2: Divide by every send

Divide by COUNT(*), not COUNT(opened). COUNT(*) counts every send for the platform, including the NULL-opened rows, which gives the fraction of all sent notifications. The * 1.0 forces real division, so the result is a decimal instead of a truncated integer 0.

Step 3: Split by platform and sort

Break the calculation out per platform value as stored, and order by open_ratio descending so the strongest platforms surface first. Several platforms land on exactly the same ratio on this data, and the ask says nothing about them, so tied rows may come out in either order. Do not invent a tiebreaker.


The solution

Per-platform open ratio in one pass
SELECT platform,
       SUM(opened) * 1.0 / COUNT(*) AS open_ratio
FROM push_notifs
GROUP BY platform
ORDER BY open_ratio DESC
`AVG(opened)` (wrong)
Divides by COUNT(opened), which drops every NULL-opened send. Platforms with many untracked sends get an inflated open rate that has no relationship to how many notifications you actually pushed.
`SUM(opened) * 1.0 / COUNT(*)` (right)
Divides by COUNT(*), so untracked sends stay in the denominator and count as not-opened. This is the true fraction of everything you sent.

Common follow-up questions