Proof of Presence

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
L4
Asked in
Meta

Problem

The security team is measuring how reliably users complete two-factor authentication, counting only the pushes that were actually delivered. For each client platform, find the confirmation rate, the share of delivered pushes the user opened, and rank the platforms from the most reliable down to the weakest.

Two-factor sent. How many confirmed?

Tables

push_notifs_2fa

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

push_notifs_2fa: 10 of 200 rows
notif_iduser_idtitleplatformstatussent_atopenedcampaign
1001101Your 2FA codeandroiddelivered2026-02-02 01:07:001login_feb
1002102Your 2FA codeandroiddelivered2026-02-03 02:10:001login_feb
1003103Your 2FA codeandroiddelivered2026-02-04 03:15:000login_feb
1004104Your 2FA codeiosdelivered2026-02-05 04:20:001login_feb
1005105Your 2FA codeiosdelivered2026-02-06 05:25:000login_feb
1006106Your 2FA codewebdelivered2026-02-07 06:30:001login_mar
1007107Your 2FA codewebdelivered2026-02-08 07:35:000login_mar
1008108Your 2FA codewebdelivered2026-02-09 08:40:00NULLlogin_mar
1009109Your 2FA codeiosfailed2026-02-10 09:45:000NULL
1010110Your 2FA codeandroidbounced2026-02-11 10:50:001login_mar

Expected output

First 3 of 3 rows
platformconfirmation_rate
android0.67
ios0.5
web0.33

Worked solution and explanation

What this problem really is

Beneath the security dashboard, this is a per-platform ratio where the numerator and denominator disagree about what NULL means. Anyone can type SUM(CASE ...) / COUNT(*) and group by platform. Two quiet things separate people. First, SUM(...) / COUNT(*) is integer over integer, so a 2-of-3 rate floors to 0 instead of 0.67. Second, a delivered push with opened IS NULL still belongs in the denominator but must never count as confirmed. Miss either one and you hand the security team confirmation rates that are confidently, invisibly wrong.


Break down the requirements

Step 1: Filter to qualifying rows

Apply the WHERE clause to isolate the delivered subset before computing the ratio, so failed and bounced pushes never touch the math.

Step 2: Group by platform

GROUP BY platform produces one output row per distinct client platform.

Step 3: Compute the ratio with CASE and NULLIF

The numerator is SUM(CASE WHEN opened = 1 THEN 1 ELSE 0 END); NULL fails the = 1 test and adds 0. Wrapping the denominator in NULLIF(COUNT(*), 0) prevents a divide-by-zero on any empty group.

Step 4: Round and order

Use ROUND(..., 2) for clean decimal output and sort by confirmation_rate descending.


The solution

Conditional aggregation for rate computation
SELECT
    platform,
    ROUND(
        1.0 * SUM(CASE WHEN opened = 1 THEN 1 ELSE 0 END)
        / NULLIF(COUNT(*), 0),
        2
    ) AS confirmation_rate
FROM push_notifs_2fa
WHERE status = 'delivered'
GROUP BY platform
ORDER BY confirmation_rate DESC

Common follow-up questions