Can you use HAVING without GROUP BY?
Yes. Without GROUP BY, the whole table is treated as one group, so HAVING can filter that single aggregate row. If the condition is false, the query returns no rows.
Explain it simply
If you do not split the class into groups, the entire class is one group. HAVING then asks one yes or no question about that whole class, and you get either one row or nothing.
Example
SELECT COUNT(*) AS total_students, AVG(marks) AS avg_marks
FROM students
HAVING AVG(marks) > 60;If the class average is 72, you get one row with the count and 72. If it is 55, you get zero rows, not a row of NULLs.
Compare with the same query without HAVING, which always returns exactly one row because an aggregate without GROUP BY always yields one row.
Selecting a plain column such as name in this query would fail, since there is one group and many names.
What to say in the interview
"Without GROUP BY, the table is a single group, and HAVING filters that one aggregated row. It is rarely used, but it is valid and a good check of how aggregation really works."
Common mistakes
- Using HAVING for a row-level condition like
HAVING marks > 60instead of WHERE.