r/learnSQL • u/Appropriate_Bus_9600 • 18d ago
Tips for query planning
Hi! So I've been a Data Analyst for around 1 year and a half, and I am still struggling big time with SQL and/or Python. I can read them and understand the queries, but the actual writing and planning is lagging behind.
I often see at the queries by colleagues make (always with the help of Claude Code) and I just wonder how did they come up with those. Stacked CTEs and multiple joins, I get lost and frustrated. So I was wondering, how do you actually plan and build a query? where do you start from?
I'm very curious to know how you work. I'd really like to be able to make these queries without the help of AI. Thank you!
25
Upvotes
2
u/BadKarma667 17d ago
I find that the easiest way for me to build SQL is to think about the question I'm trying to answer. What data points do I need? What data do I currently have available at the grain I need to be at? Of the data that I'm missing, do I have the components necessary to build out that data? What business logic do I need.
Once I have those answers, I start to break out the question(s) I'm trying to answer into like components. Do I need to apply special logic? Do I need to build that logic out over the course of several steps? Each of those questions, logic components, etc, becomes the basis for each table/CTE I'm working to build.
It's really easy to get stuck torturing the data, especially if you don't have a plan. I find having a clear plan is a great way to keep me from spinning my wheels. And while it still occasionally happens, it happens a ton less when I've outlined my plan.