Reputation: 115
Below is a sample question from Oracle
Q) Which of the following statements is true about HAVING? (Choose two.)
A. It can be used only in the SELECT statement.
B. It must occur after the GROUP BY clause.
C. It must occur after the WHERE clause.
D. It cannot reference an expression unless that expression is first reference in the GROUP BY clause.
To me, the answers should be A & B. The actual answers are A & C. "HAVING cannot be used without GROUP BY, but it is not required to follow GROUP BY".
Is that statement correct? Please advice.
Upvotes: 0
Views: 4126
Reputation: 94913
No, it isn't. aggregation doesn't need a GROUP BY clause, hence HAVING can be used without one.
Example: Give me the maximum salary, but only if the minimum salary is greater than 1000.
select max(salary)
from salaries
having min(salary) > 1000;
A and C is correct, but not for the reason given.
Upvotes: 4