Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having Clause can work without group by

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not know that having can be used without group by in the select list.

But Oracle docs made it clear that having can work without group by also.

The HAVING clause is applied to each group of the grouped table, much as a WHERE clause is applied to a select list. If there is no GROUP BY clause, the HAVING clause is applied to the entire result as a single group.


create table gotcha(id number);
insert into gotcha values(1);
insert into gotcha values(2);
insert into gotcha values(1);

select count(*)
from gotcha
having count(*) > 3;

no rows selected

select count(*)
from gotcha
having count(*) >2

count(*)
----------
3
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, Oracle may work without a Group but Santa Clause needs a Group. Elf, there's just too much work to do by himself.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an implicit group in your results i.e. a single group including the entire set of data that you're fetching, because you haven't specified any other group. I can't think of many situations where using a HAVING clause without an explicit GROUP BY would be especially useful, though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic