|
Hey Reader, Most analysts can't answer something like this: "Which of our guides are generating the most revenue, and are we assigning them to the right expeditions?" This isn't a one-step question. You need to:
That's four analytical steps. Trying to write this as a single SQL query would be unreadable. CTEs (Common Table Expressions) let you build each step separately and combine them at the end. What Are CTEs?CTEs are temporary named result sets you define with the
Each step has a clear name. Each step does one thing. The final SELECT combines them into your answer. The Full Analysis: Guide PerformanceHere's the multi-step analysis for Summit Adventures (the fake adventure tourism company I created to help people learn business analytics):
One Query. Four Steps.Look at what we just did:
Each step is a self-contained calculation. If something looks wrong in the results, you can run each CTE independently to find the issue. Try doing that with a 50-line nested query. The Power of Thinking with CTEsCTEs aren't just a SQL feature. They're a thinking framework. When you face a complex business question, resist the urge to write one massive query. Instead:
This maps directly to how experienced analysts think through problems. You're not just writing SQL — you're structuring your analytical reasoning. Common CTE PatternsPattern 1: Calculate then filter
Pattern 2: Multiple dimensions
Pattern 3: Progressive enrichment
CTEs vs. SubqueriesBoth accomplish similar things. Here's when to use each: Use CTEs when:
Use subqueries when:
For most business analysis, CTEs are the better choice. They make complex work readable, and readable work builds trust. Try This At Your JobNext time you face a multi-part business question, write down the sub-questions before opening your SQL editor:
Then turn each piece into a CTE. The query practically writes itself after that. Until next time, Brian (say hi on twitter!) P.S. CTEs are used throughout SQL for Business Impact, but Module 8 (The Gordon Ramsay Blueprint) really showcases how to combine multiple analytical steps into a cohesive investigation. It's about systematic problem-solving under pressure. Check it out at sqlforbusinessimpact.com. P.P.S. What's the most complex business question you've had to answer with SQL? Reply and share — I'd love to hear how you broke it down (or how you wish you had). I read every response. |
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, Last month, we covered LEFT JOIN to find customers who never booked. That's one type of absence — people who never showed up. But there's another kind that's even more expensive: people who showed up, committed, and then left. The dreaded cancellation. Customers who said "yes", then said "actually, never mind". In the Summit Adventures database (the fake adventure tourism company I created to help people learn business analytics), nearly 47% of all bookings end in cancellation....
Hello Reader, This week's newsletter is a bit different. There's SQL in here, but the real topic is career strategy. Because one of the most common questions I get from readers is some version of: "I've been learning SQL for a few months. How do I prove I can actually do this job?" The honest answer: a portfolio of 2-3 projects that demonstrate business thinking, not just technical syntax. The problem is most portfolios I've reviewed look the same. A Kaggle competition. A tutorial from...
Hello Reader, A common analytics question that sounds simple but isn't: "Which customers have booked the same type of expedition more than once?" You can't answer this with a regular JOIN between two different tables. The information lives in one table. You need to compare rows within that table to find patterns. That's what a self-JOIN does. It joins a table to itself. It sounds unusual, but once you see the pattern, you'll recognize situations where it's exactly what you need. The Business...