Ask the Expert

Difference between WHERE and HAVING

What is the difference between where clause and having clause?

    Requires Free Membership to View

The difference is that WHERE operates on individual rows, while HAVING operates on groups.

You can have WHERE without HAVING, you can have HAVING without WHERE, you can have both WHERE and HAVING, and you can have neither WHERE nor HAVING. But you can't have HAVING without grouping, even if the group consists of the entire result set.

A good example of the difference between WHERE and HAVING is given in my earlier answer Why WHERE cannot be used after GROUP BY.

Typically you will have at least one column to GROUP BY, such as:

select cust_id
     , count(distinct order_id) as orders
  from sales
 where order_date >= '2005-01-01'  
group
    by cust_id
having sum(order_amt) > 10000

This query returns the cust_id and number of orders for every customer who has order totals of $10,000 or more (the HAVING condition) for orders made this year (the WHERE condition).

An example of the entire result set as a single group is:

select count(*) as orders
  from sales
 where order_date >= '2005-01-01'

This query returns the number of orders this year. The aggregate function COUNT(*) operates on the single group consisting of all the rows that satisfy the WHERE clause. Note that it would be unusual to have a HAVING clause in this situation.

This was first published in February 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: