SQL Tips: HAVING versus WHERE

Had a great opportunity this weekend to teach an introductory SQL class to about 50 MBA students. We focused a lot on queries needed for business analysis, including aggregation queries.

One of the key topics that probably isn’t the most intuitive for a newer database person is the difference between filtering results with WHERE and filtering results with HAVING.

Do you have the correct (data) filter?

Do you have the correct (data) filter?

In a nutshell, a WHERE clause filters data before aggregation takes place, while HAVING filters the results based on the aggregation.

Assume you have a database table with 200,000 records. A WHERE clause will filter those down, let’s say, to 125,000 records, then your GROUP BY will aggregate those 125,000 records.

Without a WHERE clause, though, all 200,000 records will be aggregated, then the HAVING condition at the end of the GROUP BY is applied to the aggregated values.

Want a more thorough walk-through with sample data? Just click here.

And as long as you’re here, how about a little shameless advertising? This week I completed a totally different project. My first card game, “Steal This Election!” became available via The Game Crafter. If pandemics and politics have left you in need of a laugh, check it out.