Days with More Edited Than Unedited Messages

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
Google

Problem

A team chat product's admin view flags the days when message editing ran hot. From the message log, return every field of every message posted on days where more messages were edited than were left unedited.

Some days, more messages get edited than sent.

Tables

chat_msgs

Columns: msg_id INTEGER, channel TEXT, sender_id INTEGER, content TEXT, msg_type TEXT, sent_at TEXT, edited INTEGER, reply_to INTEGER

chat_msgs: 10 of 200 rows
msg_idchannelsender_idcontentmsg_typesent_ateditedreply_to
147#engineering197Anyone seeing increased latency?file2026-02-02 01:07:000NULL
194#incidents294PR #412 ready for reviewreaction2026-03-03 02:14:000NULL
241#random391Dashboard is downthread_reply2026-04-04 03:21:000NULL
288#deployments488Fixed the flaky testsystem2026-05-05 04:28:000NULL
335#data-team585Merged the migrationText2026-06-06 05:35:000NULL
382#general682NULLFILE2026-07-07 06:42:000NULL
429#engineering779LGTMtext2026-08-08 07:49:000NULL
476#incidents876👍file2026-09-09 08:56:0005
523#random973reaction2026-01-10 09:03:000NULL
570#deployments1070Can someone check the pipeline?thread_reply2026-02-11 10:10:000NULL

Expected output

First 5 of 13 rows
msg_idchannelsender_idcontentmsg_typesent_ateditedreply_to
1228#general2428Deploying v2.1 to prodthread_reply2026-07-25 00:48:00123
1792#general3592Deploying v2.1 to prodfile2026-01-09 12:12:00135
2356#general4756Deploying v2.1 to prodFILE2026-04-21 00:36:00147
2920#general1070Deploying v2.1 to prodsystem2026-07-05 12:00:00159
4048#general3398Deploying v2.1 to prodtext2026-04-01 12:48:00183

Worked solution and explanation

What this really tests

Beneath the chat-analytics framing this is a per-day conditional aggregation with a rejoin. The real question: can you compare two counts computed over the SAME group (edited versus unedited within one calendar date), then hand back the raw messages instead of the summary? Anyone can GROUP BY a date. The separator is expressing 'edited outnumbered unedited' as two conditional sums in HAVING, and then remembering the ask was for the original rows, so you must join the qualifying dates back to chat_msgs. Miss that rejoin and you return per-day counts nobody asked for; use >= and you leak the days where edits merely tied.


Break down the requirements

Step 1: Collapse to calendar day

sent_at is a timestamp, so collapse it to a calendar date with DATE(sent_at) and GROUP BY that. Grouping on the raw timestamp would make almost every message its own group.

Step 2: Compare the two counts per day

Within each day, count edited messages (edited = 1) and unedited messages (edited = 0) with two conditional sums, and keep the day in HAVING only when the edited count is strictly greater than the unedited count.

Step 3: Rejoin to recover the rows

The ask is for the raw messages, not the counts. Join the surviving dates back to chat_msgs to recover every column for every message on those days, then order by sent_at with msg_id as the tiebreaker for a deterministic result.


The solution

Qualifying date filter with full row retrieval
WITH qualifying_days AS (
    SELECT DATE(sent_at) AS msg_date
    FROM chat_msgs
    GROUP BY DATE(sent_at)
    HAVING SUM(CASE WHEN edited = 0 THEN 0 ELSE 1 END)
         > SUM(CASE WHEN edited = 0 THEN 1 ELSE 0 END)
)
SELECT cm.*
FROM chat_msgs cm
JOIN qualifying_days qd ON DATE(cm.sent_at) = qd.msg_date

Common follow-up questions