Fault Lines

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

Problem

The reliability team is building a daily failure report and treats each user's age_bucket as a stand-in for region. Looking only at open, error, and crash events, give them for every day and region the number of error and crash events as a share of the number of opens.

Errors by day and region. Some areas are worse than they appear.

Tables

event_data

Columns: event_id INTEGER, event_type TEXT, user_id INTEGER, event_timestamp TIMESTAMP, payload TEXT, tags TEXT, properties TEXT

event_data: 10 of 200 rows
event_idevent_typeuser_idevent_timestamppayloadtagsproperties
3059button_click1002026-02-02 01:07:00{"button_id": "cancel", "element_class": "btn-primary", "position": {"x": 13, "y": 7}}["desktop", "web", "returning"]{"os": "Edge", "language": "US", "screen_size": "en", "utm_source": "mobile"}
3118purchase1972026-03-03 02:14:00{"amount": 56.93, "currency": "GBP", "items": [{"sku": "SKU1002", "qty": 3}]}["ios", "premium", "new_user", "organic"]{"country": "Windows", "device_type": "FR", "referrer": "fr", "utm_medium": "1920x1080", "browser": "twitter"}
3177signup2942026-04-04 03:21:00{"signup_method": "apple", "referral_code": "REF3X", "accepted_terms": true}["android", "free", "campaign_a", "referral", "experiment_v1"]{"language": "iOS", "screen_size": "es", "utm_source": "desktop", "session_id": "bing", "os": "organic"}
3236login3912026-05-05 04:28:00{"login_method": "password", "remember_me": true, "mfa_used": false}["web", "returning"]{"device_type": "JP", "referrer": "ja", "utm_medium": "1440x900"}
3295logout4882026-06-06 05:35:00{"action": "logout", "value": 50}["premium", "new_user", "organic"]{"screen_size": "de", "utm_source": "tablet", "session_id": "amazon", "os": "Chrome"}
3354search5852026-07-07 06:42:00{"query": "phone", "results_count": 102, "filters": {"category": "electronics"}}["free", "campaign_a", "referral", "experiment_v1"]{"referrer": "mobile", "utm_medium": "375x812", "browser": "email", "country": "Edge", "device_type": "US"}
3413add_to_cartNULL2026-08-08 07:49:00{"product_id": 1007, "quantity": 2, "price": 58.49, "variant": "medium"}["returning", "campaign_b", "featured", "experiment_v2", "ios"]{"utm_source": "fr", "session_id": "twitter", "os": "Safari", "language": "Windows", "screen_size": "FR"}
3472open7792026-09-09 08:56:00{"action": "open", "value": 80}["new_user", "organic"]{"utm_medium": "bing", "browser": "organic", "country": "iOS"}
3531crash8762026-01-10 09:03:00{"action": "crash", "value": 90}["campaign_a", "referral", "experiment_v1"]{"session_id": "direct", "os": "Firefox", "language": "macOS", "screen_size": "JP"}
3590error9732026-02-11 10:10:00{"action": "error", "value": 100}["campaign_b", "featured", "experiment_v2", "ios"]{"browser": "Chrome", "country": "Android", "device_type": "DE", "referrer": "de", "utm_medium": "tablet"}

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

Expected output

First 5 of 37 rows
event_dayregionerror_rate
2022-03-2435-44NULL
2022-06-1145-54NULL
2022-12-0925-340
2023-01-1035-44NULL
2023-04-2545-54NULL

Worked solution and explanation

One output row is one day in one region, and its error_rate is failures per open: the number of 'error' and 'crash' events divided by the number of 'open' events in that group. Both counts come from the same grouped rows, so each is a SUM over a CASE that returns 1 only for the event types it wants. This is conditional aggregation, and it fits whenever a question asks for one slice of a group as a share of another slice.

From event log to failures per open

Step 1: Keep the 3 event types

The statement limits the report to 'open', 'error' and 'crash', so WHERE ed.event_type IN ('open', 'error', 'crash') comes first. It also decides which groups exist: a day and region with only 'login' or 'search' activity gets no row.

Step 2: Join to users for the region

Region is the user's age_bucket, so each event needs its users row. An inner join on user_id fits because an event without a user has no region to report under. Here user_id is NULL on 25 of the 200 events, and NULL never matches a join key, so those rows drop out.

Step 3: Group by day, count 2 things

event_timestamp is a datetime string, so substr(ed.event_timestamp, 1, 10) keeps the 'YYYY-MM-DD' part as event_day. Within each day and region, one SUM(CASE ...) counts the failures and another counts the opens, in a single pass over the rows.

Step 4: Divide as a decimal

Both sums are integers. CAST(... AS REAL) on the numerator makes the division fractional, so 1 failure over 2 opens comes out as 0.5 where integer division would return 0.

Per-day, per-region failure ratio
SELECT substr(ed.event_timestamp, 1, 10) AS event_day,
       u.age_bucket AS region,
       CAST(SUM(CASE WHEN ed.event_type IN ('error', 'crash') THEN 1 ELSE 0 END) AS REAL)
         / SUM(CASE WHEN ed.event_type = 'open' THEN 1 ELSE 0 END) AS error_rate
FROM event_data ed
JOIN users u ON ed.user_id = u.user_id
WHERE ed.event_type IN ('open', 'error', 'crash')
GROUP BY event_day, region

The WHERE filter and the opens-only denominator are the 2 choices that decide the result.

Only opens belong in the denominator

After the filter every row is an open, an error or a crash, so COUNT(*) looks like a natural denominator. It answers a different question: failures as a share of all 3 event types, with the failures counted on both sides of the division.

Failures over all filtered events
SELECT substr(ed.event_timestamp, 1, 10) AS event_day,
       u.age_bucket AS region,
       CAST(SUM(CASE WHEN ed.event_type IN ('error', 'crash') THEN 1 ELSE 0 END) AS REAL)
         / COUNT(*) AS error_rate
FROM event_data ed
JOIN users u ON ed.user_id = u.user_id
WHERE ed.event_type IN ('open', 'error', 'crash')
GROUP BY event_day, region

COUNT(*) counts the errors and crashes in the denominator too.

Hold that against the expected output. 4 of the 5 preview rows show an empty error_rate: those groups contain failures and zero opens, and the COUNT(*) version reports 1.0 for each of them. A group with 1 crash and 1 open reads 0.5 where the report wants 1.0. Only groups with no failures at all, like the preview row showing 0.0, come out the same.

What an empty error_rate tells the team

No `WHERE` filter
The 2 sums are unchanged, since each CASE already ignores other event types. But every day and region whose only activity was a 'login' or a 'search' now forms a group with 0 failures over 0 opens, and its empty error_rate looks exactly like a real failure day.
Filter to the 3 types first
A group exists only where an open, an error or a crash happened, which is what gives the 37 expected rows. An empty error_rate then has a single meaning.

Common follow-up questions