|
Hey Reader! Here's a mistake I see all the time: An analyst needs to find which expedition difficulty levels generate the most revenue. They write this:
The query fails and confusion sets in. "Why can't I filter by the sum?" It seems like it SHOULD work, but the truth is that many analysts get tripped up on this. The answer:
Let me show you the difference using real data. The Right Way: WHERE vs HAVINGI just ran this query against the live Summit Adventures database (the fake adventure tourism company I created to help people learn business analytics):
Here's what we found for Summit Adventures:
Advanced and Beginner trips generate nearly identical revenue (both around $121K), but Advanced trips have 43% higher average payments ($1,782 vs $1,345). This kind of information tells marketing where to focus their budget. Now, let's look at difficulty levels that have more than $100k total revenue (here's where HAVING comes in)
WHERE:
HAVING:
When To Use EachUse WHERE when filtering individual records:
Use HAVING when filtering aggregated results:
If your filter uses SUM(), COUNT(), AVG(), MAX(), or MIN(), you need HAVING. If it doesn't, you need WHERE. The SQL Order of ExecutionSQL executes in this order:
That's why you can't use SUM() in WHERE - the grouping hasn't happened yet! Try This At Your JobNext time you need to analyze grouped data, ask yourself: "Am I filtering before or after grouping?"
Common business scenarios:
Understanding this one concept will help you avoid most GROUP BY frustrations. Real-World ApplicationLet's say your CMO asks: "Which marketing channels brought in customers who spent more than $10,000 total?" Wrong approach (common mistake):
Right approach (what works):
The difference? You deliver the answer in minutes instead of hours of debugging. Until next time, Brian P.S. What else do you want to know about SQL? Let me know in this 2-minute survey. |
Learn to build analytics projects with SQL, Tableau, Excel, and Python. For data analysts looking to level up their career and complete beginners looking to get started. No fluff. No theory. Just step-by-step tutorials anyone can follow.
Hey Reader, Imagine you're a marketing analyst and your marketing director walks over and says: "I need our customers broken into spending tiers (VIP, Standard, and Low Value) so I can send different emails to each group." You have the data. But every customer just has a dollar amount. There's no "tier" column in the database. How do you create categories that don't exist yet? This is where CASE WHEN becomes one of the most useful tools in your SQL toolkit. The problem is that raw data...
Hey Reader, This week's newsletter is different. Not a regular "how to solve problem X with SQL" kinda thing. Because I've realized something: you can write perfect queries and still never get the career impact you want. You need "soft skills" like presenting your results to leadership in addition to "technical skills" like using CTEs and WINDOW functions. Let me show you what I mean. The Scenario Wednesday, 2pm. Q2 review meeting. CMO: "Our booking conversion is down. What's causing it?"...
Hello Reader, Imagine this scenario: Your CMO walks into Monday's meeting with a question: "I need to see our top 3 highest-revenue expeditions in EACH category - hiking, climbing, safari, cultural, and photography." Most analysts would write 5 separate queries, one per category. Or export to Excel and manually filter. An hour of work, minimum. Confident analysts write one query using window functions. Takes 3 minutes. Here's the skill that separates intermediate analysts from advanced ones....