Mobile Event Counts
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
- Apple
Problem
Looking only at the events whose tag list includes 'mobile', count how many events were recorded under each event type, so that every event type appearing among those mobile events comes back with its own total.
Mobile engagement, device by device.
Tables
event_data
Columns: event_id INTEGER, event_type TEXT, user_id INTEGER, event_timestamp TEXT, 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"} |
Expected output
| event_type | event_count |
|---|---|
| button_click | 2 |
| checkout_start | 4 |
| crash | 6 |
| signup | 8 |
| video_play | 3 |
Worked solution and explanation
What this really is
This is a list-membership test dressed up as a mobile engagement report. tags is not one tag per row: every event stores a JSON array as text, like '["mobile", "ios", "premium"]'. Anyone can write GROUP BY event_type with COUNT(*). What separates candidates is reading tags before filtering it. Write tags = 'mobile' and the query runs clean and returns nothing at all, because no row's value is the bare word. Even a single-tag event is stored as a bracketed list.
SELECT event_type, COUNT(*) AS event_count
FROM event_data
WHERE tags = 'mobile'
GROUP BY event_typeCompares the whole tags string to 'mobile'; every row is a bracketed list, so the result is empty.
Getting there
Step 1: Read what tags actually holds
The sample rows show tags as a quoted, bracketed list. That makes the filter a search inside a list, not a comparison of 2 values. Saying that out loud before you type is the whole problem.
Step 2: Match the element, quotes included
tags LIKE '%"mobile"%' looks for the quoted element, so a future tag like 'mobile_web' would not slip in. Search tags, not properties: 'mobile' also sits there as a utm_source or referrer value, and matching that column counts the wrong events.
Step 3: Count every row per event_type
Filter in WHERE so the totals only see mobile events, then GROUP BY event_type with COUNT(*). It counts events, not people, so no COUNT(DISTINCT user_id). The question asks for totals, not standings, so no sort is needed.
The solution
SELECT event_type, COUNT(*) AS event_count
FROM event_data
WHERE tags LIKE '%"mobile"%'
GROUP BY event_type- Searching properties
properties LIKE '%mobile%'matches events whose traffic source or referrer happens to be 'mobile'. It returns a confident table counting the wrong events.- Searching the tag list
tags LIKE '%"mobile"%'reads only the list of tags, the one field that says whether an event is mobile.
Common follow-up questions
- If `tags` were a real array column, how would the filter change? (Tests element membership on a typed array (`contains` / `ANY`) versus string matching.)
- Count events per tag across every tag, not just 'mobile'. (Tests expanding the list into rows with `UNNEST` and grouping by the tag.)
- What if some events were tagged 'Mobile' with a capital M? (Tests normalizing case with `LOWER(tags)` before the match.)