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
Action Steps
- Use WHERE to filter rows before grouping data
- Use HAVING to filter grouped results after applying aggregate functions like COUNT, SUM, and AVG
- Apply GROUP BY to package data into groups of rows that share the same value
- Practice using SELECT DISTINCT and GROUP BY to understand the difference between them
- 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------------------)

# I kept using WHERE when I should have been using HAVING
[](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
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------------------)

# I kept using WHERE when I should have been using HAVING
[](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
DeepCamp AI