Home > Ask the Oracle Experts > Questions & Answers
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Difference between WHERE and HAVING

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 14 February 2005
What is the difference between where clause and having clause?

>
EXPERT RESPONSE

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.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts