Lessons learned rewriting a SQL query

I rewrote a SQL query 3 times before I finally understood what it was actually doing. It was supposed to calculate average goals per league (Aug, 2013/14 season) but I kept getting stuck in nested subqueries and unnecessary id IN (...) filters. Some key lessons I learned: → 𝗕𝗿𝗲𝗮𝗸 𝗽𝗿𝗼𝗯𝗹𝗲𝗺𝘀 𝗶𝗻𝘁𝗼 𝗹𝗮𝘆𝗲𝗿𝘀 Filter → Transform → Join → Aggregate → 𝗖𝗧𝗘𝘀 𝗮𝗿𝗲 𝗳𝗼𝗿 𝗰𝗹𝗮𝗿𝗶𝘁𝘆, 𝗻𝗼𝘁 𝗰𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 Instead of messy nested queries, structure the logic so you can understand it later. → 𝗡𝗼𝘁 𝗲𝘃𝗲𝗿𝘆 𝘀𝘂𝗯𝗾𝘂𝗲𝗿𝘆 𝗶𝘀 𝗻𝗲𝗰𝗲𝘀𝘀𝗮𝗿𝘆 Why filter the same table with a subquery when a simple WHERE season = ... AND EXTRACT(MONTH...) = ... does the job? → 𝗔𝗹𝘄𝗮𝘆𝘀 𝗸𝗲𝗲𝗽 𝘁𝗵𝗲 𝗷𝗼𝗶𝗻 𝗸𝗲𝘆 If your CTE drops the linking column, the whole query breaks later. → 𝗔𝗩𝗚 𝗶𝘀 𝗷𝘂𝘀𝘁 𝗦𝗨𝗠/𝗖𝗢𝗨𝗡𝗧 Once you see that, debugging becomes much easier. Don’t just write queries. Design the data flow. #DataAnalytics #SQL 

To view or add a comment, sign in

Explore content categories