
Ramón Andrés Sánchez Acosta
4.1K posts

Ramón Andrés Sánchez Acosta
@RaMonchi64
Con esperanzas de aprender algo mas sobre programación, inteligencia artificial, ciberseguridad y sobre todo por el bendito chisme











𝗗𝗮𝘆 𝟮𝟲/𝟲𝟬 𝗼𝗳 𝗦𝗤𝗟 𝗦𝗲𝗿𝗶𝗲𝘀 — 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 𝘃𝘀. 𝗡𝗼𝗻-𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 Not all subqueries work the same way. Some execute only once, while others execute repeatedly for every row processed by the outer query. Understanding the difference between correlated and non-correlated subqueries is essential for writing efficient SQL. Today’s lesson breaks down these two powerful techniques 👇 1️⃣ 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗡𝗼𝗻-𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝘆? A non-correlated subquery is independent of the outer query. It executes once, and its result is passed to the outer query. Example: SELECT name FROM employees WHERE salary > ( SELECT AVG(salary) FROM employees ); The average salary is calculated once before filtering employees. 2️⃣ 𝗛𝗼𝘄 𝗡𝗼𝗻-𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 𝘄𝗼𝗿𝗸 Execution order: ✔ Run the inner query ✔ Return the result ✔ Execute the outer query using that result This makes them efficient when the inner result doesn't depend on each row. 3️⃣ 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝘆? A correlated subquery depends on values from the outer query. It executes once for every row processed by the outer query. 4️⃣ 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝘆 𝗘𝘅𝗮𝗺𝗽𝗹𝗲 SELECT e.name, e.salary FROM employees e WHERE salary > ( SELECT AVG(salary) FROM employees WHERE department_id = e.department_id ); The subquery calculates the average salary for each employee's department. 5️⃣ 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 𝗿𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗼𝘂𝘁𝗲𝗿 𝗰𝗼𝗹𝘂𝗺𝗻𝘀 Notice the condition: WHERE department_id = e.department_id The inner query uses a value from the current row of the outer query. 6️⃣ 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲𝘀 Non-correlated subqueries generally perform better because they run once. Correlated subqueries may be slower since they execute repeatedly for each matching row. 7️⃣ 𝗪𝗵𝗲𝗻 𝘁𝗼 𝘂𝘀𝗲 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 They are useful when each row requires its own comparison. Examples include: • Employees above their department average • Customers with their latest order • Products above their category average price 8️⃣ 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 𝗮𝗻𝗱 𝗘𝗫𝗜𝗦𝗧𝗦 EXISTS is commonly used with correlated subqueries. Example: SELECT c.name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id ); The subquery checks for matching orders for each customer. 9️⃣ 𝗖𝗼𝗻𝘀𝗶𝗱𝗲𝗿 𝗝𝗢𝗜𝗡𝘀 𝗮𝗻𝗱 𝗪𝗶𝗻𝗱𝗼𝘄 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 Some correlated subqueries can be rewritten using JOINs or window functions for improved readability and performance. Choosing the right approach depends on the database engine and use case. 🔟 𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗯𝗼𝘁𝗵 𝘁𝘆𝗽𝗲𝘀 𝗶𝘀 𝗶𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁 Understanding when a subquery runs once versus once per row helps you write faster, cleaner, and more scalable SQL. 💡 𝗞𝗲𝘆 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆: A non-correlated subquery executes independently and runs only once. A correlated subquery depends on the outer query and executes once for each row. Knowing the difference helps you optimize performance and choose the best solution for complex SQL problems. Grab SQL Playbook: codewithdhanian.gumroad.com/l/hjmix Have you ever replaced a correlated subquery with a JOIN or window function for better performance?






























