oracle sql select syntax with GROUP BY and HAVING clause

不打扰是莪最后的温柔 提交于 2020-01-02 04:47:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/20188862/oracle-sql-select-syntax-with-group-by-and-having-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!