简体   繁体   中英

Having clause without Group by

Please help me to understand this situation. Why in documentation and other resources i can finde information that HAVING clause can not be used without GROUP BY? Because while testing in SQL Developer I can use HAVING without GROUP BY. Who can clarify this? Thanks in advance.

Example:

    select sum(salary) from employees
having sum(salary)>0;

I understand that logically this is nonsense. But syntaxis allowed to do this. I want to pass exam 1z0-071 and if I will see the question related to this, then which answer should be correct? This is what confuse me.

Sorry it is my mistake. Because i already found correct information in documentation. "Having" can be used without "Group by" but it is not recomended. The wrong information about "Having" and "Group by" not in documentation. I was mistaken and looked at another resource.

Topic can be closed. Thanks to everyone for sharing knowledge.

Well, yes - it works:

SQL> select count(*) from emp having count(*) > 0;

  COUNT(*)
----------
        14

SQL> select count(*) from emp having count(*) > 20;

no rows selected

SQL>

So, this isn't an error , but is kind of questionable as of what will you use it for.

The below two statements are logically the same hence I think you are not getting an error for not using group by . The db_fiddle here

Statement 1 :-

        select count(1)
        from foo
        group by null
        having count(*) > 1;

Statement2 :-

        select count(1)
        from foo
        having count(*) > 1;

The oracle documentation says the below and it is nowhere mentioned that having should always be followed by group by

HAVING Clause

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM