Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reg: Having without GROUP BY

EV259Jul 3 2014 — edited Jul 3 2014

Hi All,

Request you to please clarify me on the below statements. How the HAVING is working, is it like a WHERE clause. As far, I have studied, HAVING will work only on the GROUP BY.

SQL> ed

Wrote file afiedt.buf

  1  declare

  2  n number;

  3  begin

  4  select count(empno) into n from emp having 1=2;

  5  dbms_output.put_line(n);

  6  exception

  7  when no_data_found then

  8  dbmS_output.put_line('no problem');

  9* end;

SQL> /

no problem

PL/SQL procedure successfully completed.

SQL> select count(empno) from emp having 1=3;   --Why HAVING is different here compared to the below statement.

no rows selected

SQL> select count(empno) from emp having ename='SCOTT';

select count(empno) from emp having ename='SCOTT'

                                    *

ERROR at line 1:

ORA-00979: not a GROUP BY expression

SQL>

Another one, how these two are different in execution.

SQL> select count(empno) from emp where 1=2;

COUNT(EMPNO)

------------

           0

SQL> select count(empno) from emp having 1=2;

no rows selected

Thanks

This post has been answered by Frank Kulash on Jul 3 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 31 2014
Added on Jul 3 2014
5 comments
1,315 views