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.

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.

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:

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:

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 >>

Subscribe to our newsletter for more free interview questions.

FOLLOW Varoon Sahgal, Author of ProgrammerInterviewon
  • nike roshe run nm

    Just keep in mind that the only people who never make mistakes or fail are either people who never do anything with their life or people who are already dead. If you want to really live life, do things that you think will make you happy and give you excitement even if it’s scary.
    [url=]nike roshe run nm[/url]

  • Jordan 12

    You’ve probably experienced this in the past. The new year starts, you’re determined and get right to working on your goals. Things are going well for the first couple of weeks or even the first month but then all of the sudden, you find yourself a bit less motivated. You might skip taking action for a day or two due to your very reasonable excuses.
    [url=]Jordan 12[/url]

  • Nike Roshe Run

    Before invading Iraq, Bush, Chency and Rumsfeld had convinced not only, Americans but the invading armies as well that they would be greeted not as invaders, but as liberators. The solders advanced on Basra, Najf and Nasiriya hade expected cheering crowds and garlands. They met with rockets and bullets instead. The US blamed these on fedayeen, a word that no one had heard in the context of Iraq till then, on Baath Party fanatics and Saddam’s loyalists.
    [url=]Nike Roshe Run[/url]

  • Jai Ganesh Jayaraman

    Nice explanation.

  • Amod

    Awesome explanation.

  • Dolly

    Very well explained !!
    Thanks :)

  • gnanasundaram mano


  • qin

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

  • abhay

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

  • BHS

    Great point !

  • 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!

  • 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.

  • 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:



    Is this a correct assumption?

  • 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


  • Washington Norys

    Very goo!

  • mr

    that's good but only one point discuss……

  • Vaibhav Tikoo

    Awesome Description….Thanks

  • varoon10

    Thanks puhleez!

  • varoon10

    No prob Vasili!

  • varoon10

    Thanks Vimal!

  • varoon10

    Thanks Venkatesh, glad it helped!

  • Aditya C

    Thanks you very much very clear…

  • parth

    Well said

  • bond007

    loads of thanks…

  • Vimal

    Best Site… provided good explanation

  • sreeporna

    excellent site to clear concepts!!!

  • noname

    nice explanation…thanks bro.!!

  • ROcky

    nice one…Thanks!


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

  • dick

    good example

  • java

    mast bey

  • vasili74

    Thanks for the explanation..

  • Uttam Barik

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

  • chicken
  • Venkatesh Dhanasekaran

    Throughly Understand.. Thanks Dude :-)

  • puhleez

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

  • KrishnaKumar Arunachalam


  • Hiral

    nice expaination