Difficulty: Medium
Correct Answer: Acts like a WHERE clause but is used for groups rather than rows.
Explanation:
Introduction / Context:
Aggregations with GROUP BY produce group-level rows. Filtering can occur before grouping (row-level) or after grouping (group-level). Correct placement of predicates determines whether they apply to individual rows or to aggregated results.
Given Data / Assumptions:
Concept / Approach:
WHERE filters individual rows before grouping and cannot reference aggregate results. HAVING filters after GROUP BY and may reference aggregates, thereby including or excluding entire groups based on conditions like COUNT() > 10 or SUM(amount) > 1000.
Step-by-Step Solution:
Verification / Alternative check:
Attempting to use aggregates in WHERE yields an error; moving the condition to HAVING resolves it.
Why Other Options Are Wrong:
Common Pitfalls:
Using HAVING for conditions that could be in WHERE, which can hurt performance by grouping unnecessary rows. Prefer WHERE when possible.
Final Answer:
Acts like a WHERE clause but is used for groups rather than rows.
Discussion & Comments