When is WHERE evaluated relative to GROUP BY?
Reveal the answer and explore the explanation.
01EasyCoreWhen is WHERE evaluated relative to GROUP BY?
Answer
EasyCore
When is WHERE evaluated relative to GROUP BY?
Answer
Filters input rows before grouping in logical query processing.
Practical example
Select attempts from the last month before aggregating.
SQLRelated concept example
WITH ranked AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC, id DESC) AS rn FROM test_attempts)
SELECT * FROM ranked WHERE rn = 1;A deterministic tie breaker gives one latest attempt per user.
Reference: Further reading ↗