tags:

views:

129

answers:

9

here's an example of a SQL statement where we use HAVING:

select column1 from table1
where condition1
having condition2;

isn't it the same exact thing if we do this:

select column1 from table1
where condition1 AND condition2;

what is the difference between these two?

+2  A: 

Hi,

No, because having is for aggregate functions or group by clause.

For example:

SELECT COUNT(ID)
FROM tablexpto
where name = 'a'
having count(ID) > 1

The first query would not run.

Bruno Costa
+4  A: 

HAVING is for use with aggregates, e.g., HAVING SUM(column1) > 200, WHERE is just for the columns, e.g., WHERE column1 < 20.

Hank Gay
+1  A: 

Having only works with a group by clause and limits records after they are grouped.

Femaref
+6  A: 

In your example, they should do the same thing. But WHERE gets processed before any GROUP BY, and so it doesn't have access to aggregated values (that is, the results of Min(), Max(), etc. functions). HAVING gets processed after GROUP BY and so can be used to constrain the result set to only those with aggregated values that match a certain predicate.

Daniel Pryden
To put Daniel's answer another way, the where clause applies to all rows in the result set. The having clause is applied to the groups created by a group by clause. So if your group consists of column1 in your example and the conditions were on column 1, then the where is the same as the having, since a row is the same as the "group".
Strommy
@Strommy: Exactly. And if for some reason you don't want to use `HAVING`, you can accomplish the same thing by using a nested `SELECT` and using the outer `WHERE` clause to express your desired predicate.
Daniel Pryden
+2  A: 

No, they are completely different.

Having conditions are for grouping aggregate functions. They are computed after the aggregated value was computed.

Example:

select id, count(1) 
  from table
 where COND1
 having count(1) > 1

Here, the having part is evaluated after the query computed the count(1) value for each group.

Pablo Santa Cruz
+2  A: 

In your example, it is the same because you have no GROUP BY

Otherwise, HAVING is applied after GROUP BY which is applied after WHERE...

Saying that, HAVING with a simple filter (x = 2) is exactly the same as WHERE because x = 2 only has meaning if you grouped on it. You normally use HAVING on an aggregate (COUNT(*) > 2 for example) that can only be applied after GROUP BY

gbn
+1  A: 

HAVING specifies a search condition for a group or an aggregate function used in a SELECT statement.

A HAVING clause is like a WHERE clause, but applies only to groups as a whole, whereas the WHERE clause applies to individual rows.

Having http://blog.sqlauthority.com/2007/07/04/sql-server-definition-comparison-and-difference-between-having-and-where-clause/

dretzlaff17
A: 

to use having you need a group by clause. you will get an error without one

kacalapy
Incorrect: if you use a literal value (i.e. no column names) in the `SELECT` clause and omit a `GROUP BY` clause then `HAVING` clause will be applied to the whole table (or filtered resultset if a `WHERE` clause is used`) and return either one row if the `HAVING` clause is evaluated as TRUE otherwise zero rows. Example: find whether there are any gaps in a sequence of numbers: `SELECT 1 FROM Numbers HAVING MAX(num) = COUNT(*);`
onedaywhen
that was sneaky, does anyone ever use this in the real world? how?
kacalapy
+1  A: 

As others have (mostly) correctly stated, in SQL the WHERE clause is evaluated before the SELECT clause, therefore the result of a set function is 'out of scope' in the WHERE clause.

For example, you CANNOT do this:

SELECT Subject, MAX(Mark) AS TopScore
  FROM Exam_Marks
 GROUP 
    BY Subject
 WHERE TopScore <= 70;

because the column correlation name TopScore is not in scope for the WHERE clause.

Of course we could use a subquery:

SELECT DT1.TopScore
  FROM (
        SELECT Subject, MAX(Mark) AS TopScore
          FROM Exam_Marks
         GROUP 
            BY Subject
       ) AS DT1
 WHERE DT1.TopScore <= 70;

The problem was, early implementations of SQL (starting with IBM's System R) lacked support for derived tables, hence the unintuitive HAVING was born.

You can read the whole sorry story in HAVING A Blunderful Time (or Wish You Were WHERE) by Hugh Darwen, from which I've borrowed the above examples.

onedaywhen