0
0
SQLquery~5 mins

HAVING clause for filtering groups in SQL - 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 created by GROUP BY based on a condition, similar to how WHERE filters rows before grouping.
Click to reveal answer
beginner
How does HAVING differ from WHERE in SQL queries?
WHERE filters individual rows before grouping, while HAVING filters groups after aggregation is done.
Click to reveal answer
beginner
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, but it is less common.
Click to reveal answer
beginner
Why do aggregate functions like COUNT() or SUM() often appear in HAVING clauses?
Because HAVING filters groups based on aggregated values, aggregate functions are used to specify conditions on those groups.
Click to reveal answer
What does the HAVING clause filter in an SQL query?
AGroups after aggregation
BColumns in the SELECT statement
CIndividual rows before grouping
DDatabase tables
Which clause is used to filter rows before grouping in SQL?
AORDER BY
BGROUP BY
CHAVING
DWHERE
Which aggregate function is commonly used in HAVING clauses?
AROUND()
BNOW()
CCOUNT()
DSUBSTRING()
Can HAVING be used without GROUP BY in SQL?
ANo, it always requires GROUP BY
BYes, but it filters aggregated results over the entire table
CYes, it filters individual rows
DNo, HAVING is only for sorting
Which clause would you use to find groups with SUM(sales) greater than 1000?
AHAVING SUM(sales) > 1000
BWHERE SUM(sales) > 1000
CGROUP BY SUM(sales) > 1000
DORDER BY SUM(sales) > 1000
Explain the difference between WHERE and HAVING clauses in SQL.
Think about when filtering happens in the query process.
You got /4 concepts.
    Write an example SQL query using HAVING to show only groups with an average score above 80.
    Use GROUP BY and HAVING with AVG() function.
    You got /3 concepts.