Code Room
Code reviewHardcr-g207
Subject DeadlockLevel Senior–Staff~32 minCommon in Databases & SQL · Concurrency interviewsIndustries Software development

Question

Review this SQL stored-procedure body (MySQL/InnoDB, REPEATABLE READ) that upserts a daily counter.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewsql
START TRANSACTION; SELECT count FROM daily_counters  WHERE day = CURDATE() AND metric = p_metric  FOR UPDATE; -- if no row, insert itINSERT INTO daily_counters (day, metric, count)  VALUES (CURDATE(), p_metric, 1); UPDATE daily_counters SET count = count + 1  WHERE day = CURDATE() AND metric = p_metric; COMMIT;
Run or narrate your approach, then ask the coach.