What is the purpose of the HAVING clause in SQL?
A
To filter rows before grouping B
To filter rows after grouping C
To sort the result set D
To join tables together
Analysis & Theory
HAVING filters groups after GROUP BY has been applied.
Which clause filters rows before GROUP BY?
A
HAVING B
ORDER BY C
WHERE D
GROUP BY
Analysis & Theory
WHERE filters rows before grouping.
What will this query do?
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5;
A
Select departments with more than 5 employees B
Select departments with exactly 5 employees C
Select all departments regardless of size D
Error: HAVING cannot be used with COUNT()
Analysis & Theory
HAVING COUNT(*) > 5 filters to departments with more than 5 employees.
Which of these clauses can use aggregate functions?
A
WHERE B
GROUP BY C
HAVING D
ORDER BY
Analysis & Theory
HAVING can use aggregate functions, while WHERE cannot.
Which clause is evaluated first in a query with GROUP BY and HAVING?
A
HAVING B
ORDER BY C
GROUP BY D
WHERE
Analysis & Theory
GROUP BY happens before HAVING.
What happens if you omit GROUP BY but still use HAVING?
A
Error occurs B
HAVING works like WHERE on aggregated data C
HAVING is ignored D
The query selects all rows
Analysis & Theory
HAVING can filter aggregated results even without GROUP BY (i.e., treating the whole table as one group).
Which of these queries counts departments with fewer than 3 employees?
A
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) < 3; B
SELECT department, COUNT(*) FROM employees HAVING COUNT(*) < 3; C
SELECT department FROM employees GROUP BY HAVING COUNT(*) < 3; D
SELECT COUNT(*) FROM employees GROUP BY department WHERE COUNT(*) < 3;
Analysis & Theory
GROUP BY department groups the rows, HAVING filters the groups.
Which clause comes after HAVING in a SELECT query?
A
ORDER BY B
GROUP BY C
WHERE D
FROM
Analysis & Theory
ORDER BY comes after HAVING.
True or False: You can use HAVING without GROUP BY if there is an aggregate function in the SELECT.
A
True B
False
Analysis & Theory
This is allowed: the whole table is treated as one group.
Which of the following statements about HAVING is correct?
A
HAVING filters rows before aggregation B
HAVING cannot be used with COUNT() C
HAVING filters groups created by GROUP BY D
HAVING replaces WHERE
Analysis & Theory
HAVING filters the groups that GROUP BY creates.