|
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, Quick question: Could a colleague open your most recent SQL query and understand what it does without asking you a single question? If the answer is "probably not," this newsletter is for you. Writing SQL that works is step one. Writing SQL that someone else can read, trust, and maintain — that's the skill that changes how people see your work. Why This Matters More Than You Think Here's what I've noticed over 15+ years in analytics: the analysts who get promoted aren't always the...
Hey Reader, Imagine you're presenting a finding to your team: "Customer A spent $4,200 with us." The first question from the room: "Is that a lot?" Most analysts learning SQL don't realize that, without context, the number means nothing. You need comparison. Is the average customer spend $500 or $5,000? Is $4,200 in the top 10% or the middle of the pack? This is the "compared to what?" problem, and subqueries solve it elegantly. Numbers without context don't drive decisions. Executives don't...
Hello Reader, Almost every business question boils down to one of two things: "How many?" (COUNT) "How much?" (SUM) How many customers booked this month? How much revenue did we generate? How many trips were cancelled? How much did we lose? Once you're comfortable with COUNT and SUM, you can answer the majority of questions that come your way (like sales analytics using SQL). Let me show you both patterns using a real business scenario. The Scenario Your operations manager asks: "Give me a...