Most Common Monday Outcome

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

Problem

Experiments we create on a Monday seem to end with no recorded outcome more often than with any real result. Across the Monday-created rows of experiments, show how often each outcome value occurs, most common first.

Mondays have a pattern.

Tables

experiments

Columns: exp_id INTEGER, exp_name TEXT, variant TEXT, user_id INTEGER, outcome REAL, created TEXT, platform TEXT

experiments: 10 of 200 rows
exp_idexp_namevariantuser_idoutcomecreatedplatform
1053checkout_v3variant_a10011.32026-02-02android
1106onboard_flowvariant_b19722.62026-03-03web
1159pricing_tiervariant_c29433.92026-04-04iOS
1212search_rankholdout39145.22026-05-05Android
1265rec_model_bcontrol48856.52026-06-06NULL
1318notif_freqvariant_a585NULL2026-07-07WEB
1371home_layoutvariant_b68279.12026-08-08NULL
1424new_feed_algovariant_c77990.42026-09-09ios
1477checkout_v3holdout8761.72026-01-10android
1530onboard_flowcontrol973132026-02-11web

Expected output

First 5 of 21 rows
outcomecnt
NULL5
70.12
50.32
92.71
80.81

Worked solution and explanation

What this really is

This is a frequency count over a nullable column, dressed up as an experiment report, with a calendar filter in front. Two things are probed. First, can you pick Mondays out of created at all: they are scattered across five years of dates, so no range or prefix does it, only the weekday of each date. Second, and this is where people lose it, do you keep the experiments with no recorded outcome as a group of their own. Write WHERE outcome IS NOT NULL or COUNT(outcome) and the most common Monday result on the seed, the missing-outcome group with 5 experiments, disappears or drops to 0 at the bottom.

Step 1: Pin the weekday convention first

strftime('%w', created) numbers the days 0 for Sunday through 6 for Saturday, so Monday is 1. Say it out loud before writing: Postgres EXTRACT(DOW FROM ...) agrees, while MySQL DAYOFWEEK starts Sunday at 1 and would make Monday 2.

Step 2: Cast the weekday before comparing

strftime returns text, and the text '1' never equals the integer 1, so an uncast comparison returns no rows at all and raises no error. CAST(... AS INTEGER) = 1 makes it a numeric comparison.

Step 3: Count rows, not outcomes

GROUP BY outcome collects every missing outcome into one NULL group. COUNT(*) counts the rows in it; COUNT(outcome) counts only non-null values and reports 0 for exactly the group that matters most.

Step 4: Sort by the count and stop

ORDER BY cnt DESC is the whole order the question asks for. Most outcomes appear once, and those ties may come back in any order: nobody asked for a winner among them, so there is no tiebreaker and no LIMIT.

Monday outcome frequency
SELECT outcome, COUNT(*) AS cnt
FROM experiments
WHERE CAST(strftime('%w', created) AS INTEGER) = 1
GROUP BY outcome
ORDER BY cnt DESC
Drops the missing outcomes
COUNT(outcome) returns the NULL row with cnt 0, and WHERE outcome IS NOT NULL removes it entirely. Either way the answer opens with a value that appears twice.
Keeps them as a group
COUNT(*) returns the NULL row with cnt 5, at the top: on Mondays, no recorded outcome is more common than any real result.

Follow-up questions

Common follow-up questions