In SQL, what’s the difference between the having clause and the where clause?

 

The difference between the having and where clause is best illustrated by an example. Suppose we have a table called emp_bonus as shown below. Note that the table has multiple entries for employees A and B.

emp_bonus
Employee Bonus
A 1000
B 2000
A 500
C 700
B 1250

If we want to calculate the total bonus that each employee received, then we would write a SQL statement like this:

select employee, sum(bonus) from emp_bonus group by employee;

The Group By Clause

In the SQL statement above, you can see that we use the "group by" clause with the employee column. What the group by clause does is allow us to find the sum of the bonuses for each employee. Using the ‘group by’ in combination with the ‘sum(bonus)’ statement will give us the sum of all the bonuses for employees A, B, and C.

Subscribe to our newsletter for more free interview questions.

Running the SQL above would return this:

Employee Sum(Bonus)
A 1500
B 3250
C 700

Now, suppose we wanted to find the employees who received more than $1,000 in bonuses for the year of 2007. You might think that we could write a query like this:

BAD SQL:
select employee, sum(bonus) from emp_bonus 
group by employee where sum(bonus) > 1000;

The WHERE clause does not work with aggregates like SUM

The SQL above will not work, because the where clause doesn’t work with aggregates – like sum, avg, max, etc.. Instead, what we will need to use is the having clause. The having clause was added to sql just so we could compare aggregates to other values – just how the ‘where’ clause can be used with non-aggregates. Now, the correct sql will look like this:

GOOD SQL:
select employee, sum(bonus) from emp_bonus 
group by employee having sum(bonus) > 1000;

Difference between having and where clause

So we can see that the difference between the having and where clause in sql is that the where clause can not be used with aggregates, but the having clause can. One way to think of it is that the having clause is an additional filter to the where clause.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon
  • Jai Ganesh Jayaraman

    Nice explanation.
    Thanks

  • Amod

    Awesome explanation.

  • Dolly

    Very well explained !!
    Thanks :)

  • gnanasundaram mano

    Okay….

  • abhay

    I rate this website ***** 5/5 stars. This website is a big achievement for its developers and content providers. (Y)

  • shweta

    does using ‘ having ‘ increases the performance of the query better than the ‘where’ clause in sql queries and how? Please explain

  • cromateu

    this is nice..thanks a ton

  • Ed

    Very good explanation! A little addendum – a follow-up question might be: Is there a disadvantage to using HAVING, especially if there’s an equivalent query with WHERE?

    The answer is: There could be performance issues. Queries with the HAVING statement need to filter the result set with O(n) (n being the number of records in the result set) for the HAVING expressions, while WHERE clauses can use indexes and other optimizations, making it possible to have O(log n) or even O(1) execution time complexity.

    The reason for this is exactly the reason why HAVING exists in the first place: The DB generates the result set with the WHERE expression *before* the aggregate function is executed (so it can’t be evaluated *after* it). Since there can’t be indexes on result sets (they are dynamic, after all), a HAVING filter has to check each result row.

    Hope it helps!

    • BHS

      Great point !

    • qin

      Is there a way where we can use the functions in having but have a better efficiency?

  • Kailash Aade

    Thanks dude.. really helped a lot

  • anna

    thanks a lot ! good example !

  • Sushil Kumar

    thanks sir!

  • brendan

    It might be nice to show the different output of the queries. I assume it would look like this:

    WHERE
    EmployeeSum(Bonus)A1000B3250

    HAVING
    EmployeeSum(Bonus)A1500B3250

    Is this a correct assumption?

    • DPJ

      No, as the author said, you cannot use aggregate functions in WHERE clause. So the first query will not execute, guess you would get a syntax error there.

  • Qasim

    Thank you very very much,
    After studying from 5-6 sites I was still in confusion
    but from here i clear my concept in only 5 minutes.

    Thanks , excellent explanation.

  • ashok

    Good one bro

  • Swapna

    nicely explained.. thanku :)

  • Avinash

    Hey thanks man…. it doesnt get any simpler than this…. great explanation

  • robin

    Thanks all my doubt are cleared now

  • L

    Thanks.. that explains it perfectly..

  • JK

    Thanks boss… good explanation with the example…

  • Sanaya Khan

    superb

  • Washington Norys

    Very goo!

  • mr

    that's good but only one point discuss……

  • Vaibhav Tikoo

    Awesome Description….Thanks

  • Aditya C

    Thanks you very much very clear…

  • parth

    Well said

  • bond007

    loads of thanks…

  • Vimal

    Best Site… provided good explanation

    • varoon10

      Thanks Vimal!

  • sreeporna

    excellent site to clear concepts!!!

  • ROcky

    nice one…Thanks!

  • SANDHYALAL KUMAR

    Very good explanation of each covered topic , helpful , interesting and clear……

  • dick

    good example

  • java

    mast bey

  • vasili74

    Thanks for the explanation..

    • varoon10

      No prob Vasili!

  • chicken
    • Uttam Barik

      chicken. it is simple you can not use group function in where clause. it is not misleading

  • Venkatesh Dhanasekaran

    Throughly Understand.. Thanks Dude :-)

    • varoon10

      Thanks Venkatesh, glad it helped!

  • puhleez

    This is one of those basic concepts that I was having trouble wrapping my mind around. Excellent, simple example. Thanks!!!

    • varoon10

      Thanks puhleez!

  • KrishnaKumar Arunachalam

    superb

    • noname

      nice explanation…thanks bro.!!

  • Hiral

    nice expaination