0
0
MySQLquery~5 mins

HAVING clause in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly NULL values
BIndividual rows before grouping
CGroups after aggregation
DTable schema
Which clause is used to filter rows before grouping in SQL?
AWHERE
BHAVING
CGROUP BY
DORDER BY
Can HAVING be used without GROUP BY in MySQL?
AYes, to filter aggregated results over the whole table
BOnly with ORDER BY
CNo, it always requires GROUP BY
DOnly with JOIN
Which of these is a valid HAVING clause condition?
AHAVING salary > 50000
BHAVING COUNT(*) > 10
CHAVING department = 'Sales'
DHAVING employee_id = 100
What happens if you use a non-aggregated column in HAVING that is not in GROUP BY?
AResults are sorted
BQuery runs fine
CColumn is ignored
DSQL error occurs
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.