Can you use HAVING without GROUP BY, and what does it mean?
Yes. Without GROUP BY the entire table is treated as one implicit group, so HAVING filters that single aggregate result. It is rarely used this way in practice but is valid SQL and useful for existence checks.
How to think about it
This is a “does it work, and why?” question. The answer is yes — and understanding why reveals something fundamental about how HAVING works: it filters groups, and when there is no GROUP BY, the whole table is one implicit group.
What happens without GROUP BY
When you omit GROUP BY, all rows in the table form a single aggregate group. HAVING then acts as a gate on that one group’s aggregate value. The query either returns one row (if the condition is true) or zero rows (if it’s false).
A practical use: data validation in one line
This pattern is occasionally useful as a sanity check. “Does this table have any rows at all?” or “Is the total revenue above a threshold?” You can write that as a single query that returns a row when healthy and zero rows when not.
-- Returns one row only if the table has data to process
SELECT SUM(amount) AS total_revenue
FROM orders
HAVING COUNT(*) > 0;
For production alerting or pipeline validation, this is a quick way to short-circuit downstream steps when a table is unexpectedly empty.
When to use it vs. a WHERE on a subquery
The more readable alternative for most cases is a WHERE clause on a subquery result:
-- Usually clearer in application code
SELECT COUNT(*) FROM orders;
-- Then check the value in your application
HAVING without GROUP BY is valid and occasionally elegant for in-query conditionals, but in most contexts a subquery with WHERE is easier for colleagues to parse.