I been going thru some of the sql syntax to study for the oracle sql exam, I found something rather confusing
based on the official references, the select syntax is as follow :
SELECT
[ hint ]
[ { { DISTINCT | UNIQUE } | ALL } ]
select_list
FROM { table_reference | join_clause | ( join_clause ) }
[ , { table_reference | join_clause | (join_clause) } ] ...
[ where_clause ]
[ hierarchical_query_clause ]
[ group_by_clause ]
[ HAVING condition ]
[ model_clause ]
based on this you cannot have the HAVING clause before the GROUP BY clause . However if i were to execute the following sql in the test server :
select
department_id , count (*)
from
employees
having
count(*) > 6
group by
department_id ;
it does not produce a syntax error , can some one help explain this ? I don't like to think that the reference docs is wrong , but if so I need some confirmation.
As stated here:
Use the HAVING clause to restrict the groups of returned rows to those groups for which the specified condition is TRUE. If you omit this clause, then the database returns summary rows for all groups.
Specify GROUP BY and HAVING after the where_clause and hierarchical_query_clause. If you specify both GROUP BY and HAVING, then they can appear in either order.