Use HAVING for Group-Level Thresholds
Write a query with WHERE for row eligibility and HAVING for aggregate thresholds.
Find stores with at least 100 completed pickups this month and average rating below 4.2. WHERE filters pickup rows; GROUP BY creates one row per store; HAVING filters those store groups. Trying to write WHERE COUNT() >= 100 asks SQL to use a count before groups exist. Before WHERE status = 'completed' AND COUNT() >= 100 AND AVG(rating) < 4.2 -- aggregate values do not exist in WHERE. After WHERE status = 'completed' AND pickup_at >= DATE '2026-06-01' AND pickup_at = 100 AND AVG(rating) < 4.2; Step 1 WHERE status = 'completed' Only completed pickup rows are evidence for both…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in