Understanding Correlated Subqueries Execution Model in PostgreSQL
📖 Scenario: You are working with a small company database that has two tables: employees and departments. You want to find employees who earn more than the average salary in their own department.
🎯 Goal: Build a SQL query using a correlated subquery to list employees whose salary is higher than the average salary of their department.
📋 What You'll Learn
Create a table called
departments with columns dept_id (integer) and dept_name (text).Create a table called
employees with columns emp_id (integer), emp_name (text), salary (integer), and dept_id (integer).Insert exact data into
departments and employees as specified.Write a correlated subquery that compares each employee's salary to the average salary of their department.
Use the exact table and column names as given.
💡 Why This Matters
🌍 Real World
Correlated subqueries are useful when you need to compare each row to a group-level calculation, such as finding employees earning above their department average.
💼 Career
Understanding correlated subqueries helps in writing efficient and clear SQL queries for reporting and data analysis tasks common in many data-related jobs.
Progress0 / 4 steps