I kept using WHERE when I should have been using HAVING

📰 Medium · Data Science

Learn when to use WHERE and HAVING in SQL queries to filter data before and after grouping

beginner Published 16 Apr 2026
Action Steps
  1. Use WHERE to filter rows before grouping data
  2. Use HAVING to filter grouped results after applying aggregate functions like COUNT, SUM, and AVG
  3. Apply GROUP BY to package data into groups of rows that share the same value
  4. Practice using SELECT DISTINCT and GROUP BY to understand the difference between them
  5. Test your understanding by writing SQL queries that use WHERE and HAVING correctly
Who Needs to Know This

Data analysts and scientists who work with SQL databases can benefit from understanding the difference between WHERE and HAVING to improve their data filtering skills

Key Insight

💡 WHERE filters rows before grouping, while HAVING filters the grouped results after

Share This
📊 Learn when to use WHERE and HAVING in SQL queries to filter data like a pro! 🚀

Key Takeaways

Learn when to use WHERE and HAVING in SQL queries to filter data before and after grouping

Full Article

Title: I kept using WHERE when I should have been using HAVING

URL Source: https://medium.com/@fisayosangolade/i-kept-using-where-when-i-should-have-been-using-having-5ef2b2c2ccad?source=rss------data_science-5

Published Time: 2026-04-16T14:15:03Z

Markdown Content:
# I kept using WHERE when I should have been using HAVING | by Fisayosangolade | Apr, 2026 | Medium

[Sitemap](https://medium.com/sitemap/sitemap.xml)

[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40fisayosangolade%2Fi-kept-using-where-when-i-should-have-been-using-having-5ef2b2c2ccad&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)

Get app

[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)

[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)

Sign up

[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40fisayosangolade%2Fi-kept-using-where-when-i-should-have-been-using-having-5ef2b2c2ccad&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

![Image 1](https://miro.medium.com/v2/resize:fill:32:32/1*dmbNkD5D-u45r44go_cf0g.png)

# I kept using WHERE when I should have been using HAVING

[![Image 2: Fisayosangolade](https://miro.medium.com/v2/resize:fill:32:32/1*f2Yy7KsQ-y4-P8DzEUf1Yw.jpeg)](https://medium.com/@fisayosangolade?source=post_page---byline--5ef2b2c2ccad---------------------------------------)

[Fisayosangolade](https://medium.com/@fisayosangolade?source=post_page---byline--5ef2b2c2ccad---------------------------------------)

Follow

1 min read

·

Just now

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2F5ef2b2c2ccad&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40fisayosangolade%2Fi-kept-using-where-when-i-should-have-been-using-having-5ef2b2c2ccad&user=Fisayosangolade&userId=5c0193602259&source=---header_actions--5ef2b2c2ccad---------------------clap_footer------------------)

[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2F5ef2b2c2ccad&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40fisayosangolade%2Fi-kept-using-where-when-i-should-have-been-using-having-5ef2b2c2ccad&source=---header_actions--5ef2b2c2ccad---------------------bookmark_footer------------------)

[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3D5ef2b2c2ccad&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40fisayosangolade%2Fi-kept-using-where-when-i-should-have-been-using-having-5ef2b2c2ccad&source=---header_actions--5ef2b2c2ccad---------------------post_audio_button------------------)

Share

I thought **WHERE**and **HAVING**were the same thing. They both filter data, so what’s the difference?

Turns out I was missing one thing.

GROUP BY, unlike SELECT DISTINCT, packages the data into groups of rows that share the same value. Something exists inside those packages and that’s where the magic happens. Inside each group, you can now do calculations.

## Get Fisayosangolade’s stories in your inbox

Join Medium for free to get updates from this writer.

Subscribe

Subscribe

- [x]

Remember me for faster sign in



Those packages are what make aggregate functions like COUNT, SUM, and AVG possible.

Something simple clicked. WHERE filters rows before grouping, while HAVING filters the grouped results after.

SELECT color, COUNT(*) AS total_sold

FROM sli
Read full article → ← Back to Reads

Related Videos

Question #2: AI ke baad Data Analyst ka role khatam ho jayega? 🤖
Question #2: AI ke baad Data Analyst ka role khatam ho jayega? 🤖
Project Shift
5 Non Technical Skills Every Data Analyst Needs to Get Hired 🚨
5 Non Technical Skills Every Data Analyst Needs to Get Hired 🚨
Project Shift
This is one of the most common questions we get and that's exactly what this reel answers.
This is one of the most common questions we get and that's exactly what this reel answers.
Project Shift
Build these 4 projects to land a Data Analyst job #dataanalytics #projects #shorts
Build these 4 projects to land a Data Analyst job #dataanalytics #projects #shorts
Project Shift
[Student Review] Data Analyst Launchpad: 6-Day Live Bootcamp by Project Shift 👇
[Student Review] Data Analyst Launchpad: 6-Day Live Bootcamp by Project Shift 👇
Project Shift
[Student Review] Data Analyst Launchpad: 6-Day Live Bootcamp by Project Shift 👇
[Student Review] Data Analyst Launchpad: 6-Day Live Bootcamp by Project Shift 👇
Project Shift