Forum

Notifications
Clear all

[Solved] What's the difference between WHERE and HAVING?

  

1
Topic starter

How can I know when to use WHERE vs. when to use HAVING? What do both of these statements achieve?

Crammer Nation, if you're still confused about WHERE vs. HAVING after reading the answer below, don't hesitate to throw in another comment / question on this topic!

1 Answer
1

Both WHERE and HAVING are statements that you'll utilize to filter.

WHERE

You'll utilize WHERE to filter based off of field value.

For example, if we had a table of people and their favorite colors (represented with a field called "fav_color"), and we wanted to only view people with the favorite color of "Red", we'd write the following WHERE statement:

WHERE fav_color = "Red"

HAVING

You'll utilize HAVING to filter based off of aggregate function value.

For example, if we had a table of every city's gas prices (represented with a field called "price_of_gas") and wanted to filter for states in which the average gas price was below $3.50, we'd use the following HAVING statement:

HAVING AVG(price_of_gas) < 3.50

Notice how with this HAVING statement, we're not filtering based off of the field price_of_gas, but instead we're filtering based off the aggregate function AVG(price_of_gas)! That's why we're using a HAVING statement here!

Your Answer

Author Name

Author Email

Your question *

 
Preview 0 Revisions Saved