21

It looks like both WHERE and HAVING help filter rows. I wonder if, instead of having to HAVING, I can use WHERE ... AND.

I'm a little confused.

Thanks for helping

Richard77
  • 20,343
  • 46
  • 150
  • 252

4 Answers4

35

You use "WHERE" clauses to select rows for inclusion in the dataset before grouping operations have happend (GROUP BY). You use HAVING clauses to filter rows after grouping.

So like if you're aggregating a sum or a maximum or a minimum, you can use HAVING predicates to check the aggregated values on the candidate rows.

Pointy
  • 405,095
  • 59
  • 585
  • 614
10

There are some subtleties and intricacies, but quick description is a comparison: HAVING is to GROUP BY as WHERE is to SELECT.

In other words, if you think of HAVING as a WHERE clause that gets applied after the GROUP BY clause, things start to make sense.

Philip Kelley
  • 39,426
  • 11
  • 57
  • 92
  • So you mean, the difference between the 2 is mostly the fact that one is applied to SELECT and the other to GROUP BY? – Richard77 Feb 16 '11 at 00:14
6

Explanation and query samples in this fine article: Where Vs Having / Difference between having and Where clause

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
1

You would use the Having statement with Group By, typically as a way to filter on an aggregate column.

You can get more information here.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358