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
Lessons learned rewriting a SQL query
More Relevant Posts
-
Day 26/90 — SQL Series | Week 4 "My WHERE clause is not filtering dates correctly." "My SUM is returning NULL instead of a number." 9 times out of 10 — it's a data type mismatch. CAST and CONVERT fix it. CAST('250' AS INT) → turns text into number so you can do math CAST('2024-01-15' AS DATE) → turns text into date so filters work CAST(order_id AS VARCHAR) → turns number into text for concatenation Rule: use CAST (works everywhere). Use CONVERT only when you need date formatting in SQL Server. #SQL #CastConvert #DataAnalytics #LearnSQL #DataAnalyst
To view or add a comment, sign in
-
-
Day 33/90 — SQL Series | Phase 2 Subquery in FROM = building a custom table mid-query. "Show only cities with more than 10 orders — sorted by order count." You cannot do this with WHERE alone because WHERE runs before GROUP BY. Solution: group first inside a subquery → then filter the grouped result. SELECT city, order_count FROM ( SELECT city, COUNT(*) AS order_count FROM orders GROUP BY city ) AS city_summary WHERE order_count > 10; The inner query creates a virtual table called city_summary. The outer query reads and filters it like a normal table. Rule: always give the inner query an alias — SQL requires it. CTEs do the exact same thing but cleaner — coming next week. #SQL #Subquery #DataAnalytics #LearnSQL #DataAnalyst
To view or add a comment, sign in
-
-
🛑 Stop dealing with negative numbers in your SQL reports! Need to find the difference between two values regardless of which one is higher? The ABS() function is your best friend. Whether you're calculating: ✅ Price discrepancies ✅ Absolute variance ✅ Distance metrics ... ABS() turns those negative numbers positive in a snap! ⚡ I just dropped a new video covering how to use it with practical examples: #SQL #DataAnalytics #DataScience #SQLTips #LearningSQL
SQL ABS Function: Everything You Need to Know (With Examples) #backend
https://www.youtube.com/
To view or add a comment, sign in
-
🚀 EXISTS in SQL 💡 What is EXISTS? ✔️ Check if a subquery returns any rows ✔️ Returns TRUE if at least 1 row exists ✔️ Returns FALSE if no rows 🔍 How it works: ➡️ Outer query runs ➡️ Inner query checks for matching rows ➡️ If a match is found, the row is included ➡️ If no match, the row is excluded 🗝️ Key Concept 👉 EXISTS does not care about values 👉 It only checks if rows exist 🧠 Even if the subquery returns NULL ➡️ EXISTS can still be TRUE #SQL #DataAnalytics #LearningSQL #TechTips #InterviewPrep #DataEngineering
To view or add a comment, sign in
-
-
In x++, if you want to print the SQL statement of an AOT query, use this class internal final class RunMyQuery { public static void main(Args _args) { Query query = new Query(queryStr(QueryName)); QueryRun queryRun; str sql; queryRun = new QueryRun(query); sql = queryRun.query().getSQLStatement(); info(sql); } }
To view or add a comment, sign in
-
🚀 JOIN vs Subquery – Which is Faster? Still confused between JOIN and Subquery in SQL? 👉 Here’s the simple logic: ✔ Subquery = Query inside query ✔ JOIN = Combine tables ✔ JOIN works faster for large data ⚡ ✔ Execution plan decides performance 💡 Learn smart, write optimized queries! 💬 Comment: JOIN or Subquery? 📌 Follow for more SQL tips
To view or add a comment, sign in
-
𝐒𝐐𝐋 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 | 𝐋𝐞𝐯𝐞𝐥: 𝐀𝐝𝐯𝐚𝐧𝐜𝐞𝐝 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: Convert each row into multiple rows based on value Do it using pure SQL Avoid using loops (think set-based 💡) 𝐖𝐡𝐚𝐭 𝐭𝐡𝐢𝐬 𝐭𝐞𝐬𝐭𝐬: Recursive thinking Set-based SQL approach Understanding of row expansion Advanced SQL concepts (CTE / generate_series / window tricks) Drop your solution in the comments! P.S - Get create and insert statement in the first comment.
To view or add a comment, sign in
-
-
An interesting problem posted by Tyagi, which can be solved by using Recursive logic. Do check the comment to see my simple solution.
Data Engineer | Azure, AWS, Snowflake, Databricks, Fabric | 4x Microsoft Certified | 1x Snowflake Certified | SQL | ETL
𝐒𝐐𝐋 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 | 𝐋𝐞𝐯𝐞𝐥: 𝐀𝐝𝐯𝐚𝐧𝐜𝐞𝐝 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: Convert each row into multiple rows based on value Do it using pure SQL Avoid using loops (think set-based 💡) 𝐖𝐡𝐚𝐭 𝐭𝐡𝐢𝐬 𝐭𝐞𝐬𝐭𝐬: Recursive thinking Set-based SQL approach Understanding of row expansion Advanced SQL concepts (CTE / generate_series / window tricks) Drop your solution in the comments! P.S - Get create and insert statement in the first comment.
To view or add a comment, sign in
-
-
After a a few days off , I am here to prsent my Day 25 of SQL Night Study 🌙 Today I learned about the SQL EXISTS operator and it’s a very practical one. The EXISTS operator is used in a WHERE clause to check if a subquery returns any result. In simple terms: 👉 If the subquery finds at least one matching row, EXISTS returns TRUE 👉 If it finds nothing, it returns FALSE 🔹 Basic Syntax SELECT column_name FROM table_name WHERE EXISTS (subquery); 🔹 Example (Relatable) Imagine you have: A Customers table An Orders table You want to find customers who have placed at least one order. Query: SELECT customer_name FROM Customers c WHERE EXISTS ( SELECT 1 FROM Orders o WHERE o.customer_id = c.customer_id ); 👉 This will return only customers who have orders. 💡 Why this is useful Instead of counting or joining tables, EXISTS simply checks if a relationship exists, making it efficient and easy to read. Little by little, I am understanding how SQL helps answer real business questions. #SQL #SQLLearning #DataAnalytics #LearningInPublic #TechJourney #ContinuousLearning
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development