Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between WHERE and HAVING

Tags:

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

like image 982
Richard77 Avatar asked Feb 16 '11 00:02

Richard77


2 Answers

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.

like image 101
Pointy Avatar answered Sep 20 '22 10:09

Pointy


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.

like image 28
Philip Kelley Avatar answered Sep 18 '22 10:09

Philip Kelley