Recall & Review
beginner
What is the purpose of the HAVING clause in SQL?
The HAVING clause is used to filter groups of rows created by GROUP BY based on a condition. It works like WHERE but for grouped data.
Click to reveal answer
beginner
How does HAVING differ from WHERE in SQL?
WHERE filters rows before grouping, while HAVING filters groups after aggregation.
Click to reveal answer
intermediate
Write a simple SQL query using HAVING to find departments with more than 5 employees.
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department HAVING COUNT(*) > 5;
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 over the entire table, like HAVING COUNT(*) > 10.
Click to reveal answer
advanced
Why might you get an error if you use a column in HAVING that is not in GROUP BY or an aggregate function?
Because HAVING filters groups, any column used must be part of the grouping or an aggregate. Otherwise, SQL doesn't know how to apply the condition.
Click to reveal answer
What does the HAVING clause filter in an SQL query?
✗ Incorrect
HAVING filters groups created by GROUP BY after aggregation, unlike WHERE which filters rows before grouping.
Which clause is used to filter rows before grouping in SQL?
✗ Incorrect
WHERE filters rows before grouping, HAVING filters groups after aggregation.
Can HAVING be used without GROUP BY in MySQL?
✗ Incorrect
HAVING can filter aggregated results without GROUP BY, applying conditions on the entire result.
Which of these is a valid HAVING clause condition?
✗ Incorrect
HAVING conditions must use aggregate functions or grouped columns. COUNT(*) > 10 is valid.
What happens if you use a non-aggregated column in HAVING that is not in GROUP BY?
✗ Incorrect
SQL throws an error because it cannot apply HAVING condition on columns not grouped or aggregated.
Explain the difference between WHERE and HAVING clauses in SQL.
Think about when filtering happens in the query process.
You got /4 concepts.
Describe a scenario where you would use HAVING without GROUP BY.
Consider aggregate functions applied to the whole table.
You got /4 concepts.