0
0
SQLquery~5 mins

WHERE vs HAVING mental model in SQL - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is the main purpose of the WHERE clause in SQL?
The WHERE clause filters rows before any grouping happens. It decides which rows to include in the query based on conditions.
Click to reveal answer
beginner
What does the HAVING clause do in SQL?
The HAVING clause filters groups after the data has been grouped by GROUP BY. It lets you filter aggregated results.
Click to reveal answer
intermediate
When should you use WHERE instead of HAVING?
Use WHERE to filter individual rows before grouping. Use HAVING to filter groups after aggregation.
Click to reveal answer
intermediate
Can HAVING be used without GROUP BY? Explain.
Yes, HAVING can be used without GROUP BY to filter aggregated results on the whole table, but this is less common.
Click to reveal answer
beginner
Example: Which clause filters rows before aggregation: WHERE or HAVING?
WHERE filters rows before aggregation, so it affects which data is grouped and aggregated.
Click to reveal answer
Which SQL clause filters rows before grouping?
AWHERE
BHAVING
CGROUP BY
DORDER BY
Which clause is used to filter groups after aggregation?
AHAVING
BSELECT
CWHERE
DFROM
Can WHERE filter aggregated results directly?
AYes, always
BOnly with HAVING
COnly with GROUP BY
DNo, WHERE filters before aggregation
Which clause would you use to filter customers with total sales over 1000?
AWHERE total_sales > 1000
BORDER BY total_sales > 1000
CHAVING total_sales > 1000
DGROUP BY total_sales > 1000
What happens if you put an aggregate function in WHERE clause?
AIt works fine
BSyntax error or unexpected result
CIt filters groups
DIt orders rows
Explain the difference between WHERE and HAVING clauses in SQL.
Think about when filtering happens: before or after grouping.
You got /4 concepts.
    Describe a real-life example where you would use HAVING instead of WHERE.
    Consider filtering on sums or counts, not individual rows.
    You got /4 concepts.