Having and Where Clause


Where and Having both are used to apply filter

Difference:

You can not use Aggregate function in where clause, like

select * from employee where SUM(salary) > 5000;


but you can use Aggregate function in having clause.

But having has to be used in conjunction with "GROUPED BY" clause.

GROUP BY, makes the list of group, say if you have applied two "Grouped by"


select city,gender 
 from employee 
 grouped by city, gender;

in above case, you will have below type of  data

Group-1

id  name      city      gender salary
2   Dharmu  BLR    M         4000
8   deepak    BLR   ML       5000


Group-2

id  name      city      gender salary
1   rashmi    BLR    F          10000
3   Sargun    BLR   F           11000


Group-3
and so on


Now you can perform all sort of aggregation function on this including having,

select city,gender 
 from employee 
 grouped by city, gender;
having SUM(salary_ > 4000;


So it will apply sum of salary in each group and if that group satisfied the condition, it will list city and gender from that group, remember, Group is not about finding any individual, it about finding info of group of ppl sharing common properties


Comments

Popular posts from this blog

Oracle User vs Oracle Schemas