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_id | event_type | user_id | event_timestamp | payload | tags | properties |
|---|---|---|---|---|---|---|
| 3059 | button_click | 100 | 2026-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"} |
| 3118 | purchase | 197 | 2026-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"} |
| 3177 | signup | 294 | 2026-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"} |
| 3236 | login | 391 | 2026-05-05 04:28:00 | {"login_method": "password", "remember_me": true, "mfa_used": false} | ["web", "returning"] | {"device_type": "JP", "referrer": "ja", "utm_medium": "1440x900"} |
| 3295 | logout | 488 | 2026-06-06 05:35:00 | {"action": "logout", "value": 50} | ["premium", "new_user", "organic"] | {"screen_size": "de", "utm_source": "tablet", "session_id": "amazon", "os": "Chrome"} |
| 3354 | search | 585 | 2026-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"} |
| 3413 | add_to_cart | NULL | 2026-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"} |
| 3472 | open | 779 | 2026-09-09 08:56:00 | {"action": "open", "value": 80} | ["new_user", "organic"] | {"utm_medium": "bing", "browser": "organic", "country": "iOS"} |
| 3531 | crash | 876 | 2026-01-10 09:03:00 | {"action": "crash", "value": 90} | ["campaign_a", "referral", "experiment_v1"] | {"session_id": "direct", "os": "Firefox", "language": "macOS", "screen_size": "JP"} |
| 3590 | error | 973 | 2026-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
| user_id | username | signup_date | account_status | age_bucket | |
|---|---|---|---|---|---|
| 100 | alice | alice@example.com | 2025-02-02 | inactive | 25-34 |
| 197 | aaron42 | aaron42@example.com | 2026-03-03 | suspended | 35-44 |
| 294 | amelia | amelia@example.com | 2024-04-04 | pending_verification | 45-54 |
| 391 | arjun | arjun@example.com | 2025-05-05 | active | 55-64 |
| 488 | ava99 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 585 | andrew_k | andrew_k@example.com | 2024-07-07 | suspended | NULL |
| 682 | anika | anika@example.com | 2025-08-08 16:34:00 | pending_verification | 18-24 |
| 779 | aiden | aiden@example.com | 2026-09-09 14:53:00 | active | 25-34 |
| 876 | aria_b | aria_b@example.com | 2024-10-10 | inactive | 35-44 |
| 973 | brian | brian@example.com | 2025-11-11 | suspended | 45-54 |
Expected output
| event_day | region | error_rate |
|---|---|---|
| 2022-03-24 | 35-44 | NULL |
| 2022-06-11 | 45-54 | NULL |
| 2022-12-09 | 25-34 | 0 |
| 2023-01-10 | 35-44 | NULL |
| 2023-04-25 | 45-54 | NULL |
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.
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, regionThe 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.
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, regionCOUNT(*) 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
CASEalready 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 emptyerror_ratelooks 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_ratethen has a single meaning.
Common follow-up questions
- Your join is an inner join. Which events never reach this report?
Any event whose
user_idis NULL (25 of the 200 here) or has no row inusers, because a NULL key never equals anything. Ask whether those system events include crashes, since a failure with no user attached is still a failure the team may want counted somewhere. - What happens to users who have no
age_bucket?GROUP BYkeeps NULL as a group, so their events are reported under an empty region for each day;age_bucketis NULL on 28 of the 200usersrows. In a real report you would agree with its owner whether that bucket gets a label or is left out. - Can
error_ratebe greater than 1?Yes: it's failures per open, so 1 open followed by 3 errors gives 3.0. Anyone charting it as a percentage of sessions will misread it, which is why the definition belongs next to the column.