Considering the following relational schema
customers(id, name, age, address, salary)
I tried a query
SELECT SUM(salary), age FROM customers
GROUP BY age HAVING age > 23 ; ...(1)
I was surprised to see that it worked fine and that I could write a single column condition also in HAVING clause.
Even this is also working
SELECT SUM(salary), age FROM customers
GROUP BY age, salary HAVING age > 23 AND salary >2000; ...(2)
Otherwise, I should have written it like this : (using WHERE clause)
SELECT SUM(salary), age FROM customers
WHERE age > 23 GROUP BY age; ...(3)
And
SELECT SUM(salary), age FROM customers
WHERE age > 23 AND salary >2000 GROUP BY age, salary ; ..(4)
But when I tried with more combinations I found that
that column name must be present in GROUP BY clause also on which condition is applied in HAVING clause.
Am I correct or is it possible to write a single column condition in HAVING clause in any other way also ?
Why is it working because I had earlier studied that we can write only conditions on Aggregate functions in HAVING clause.