Possible Duplicate:
SQL: What’s the difference between HAVING and WHERE?
i am learning sql syntax and i can’t understand this.
the second half of the question is a much more technical one. what is actually happening behind the scenes of the database between WHERE and HAVING? which one uses more resources? are they same algorithm just applying to different data sets?
thanks!
Where is in most queries and limits the records that the query cares about.
Having is used in “Group By” queries, and acts upon the grouped results.
Think of the
Whereclause as happening first. If there are 1000 records in the db, the where clause might make it so only 80 matter. And let’s say that theGroup Byclause groups these 80 db records into (for example) 15 aggregate recordset rows. Without the Having clause, you would get all 15 of these aggregate rows back. TheHavingclause if for filtering upon these 15 aggregate rows.Let’s say you want a list of all the customers from Texas who made more than 5 orders last year.
Whereclause to get all the orders from last year by people from TexasGroup Byclause to group all the orders by customer (with aCount(OrderID) As OrderCountin your select clause)Havingclause would limit the customers listed to those having 5 or more orders.