Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Correlated Subquery

Learn how a correlated subquery works with the outer query and is evaluated for each row.

What is a Correlated Subquery?

A Correlated Subquery is a subquery that depends on a value from the outer query.

Unlike a normal subquery, a correlated subquery cannot work independently because it references a column from the outer query.

The subquery is evaluated for each row processed by the outer query.

Basic Structure


SELECT column_name
FROM table_name AS outer_table
WHERE condition
(
    SELECT column_name
    FROM table_name AS inner_table
    WHERE inner_table.column = outer_table.column
);
            

Notice that the inner query refers to a column from the outer query. This dependency makes the subquery correlated.

Example Table

Consider the following Employees table:

EmployeeId EmployeeName DepartmentId Salary
1 Rahul 101 30000
2 Priya 101 45000
3 Amit 101 50000
4 Sneha 102 40000
5 Neha 102 55000

Example: Employees Above Department Average

Suppose we want to find employees whose salary is greater than the average salary of their own department.


SELECT
    e.EmployeeName,
    e.DepartmentId,
    e.Salary
FROM Employees e
WHERE e.Salary >
(
    SELECT AVG(e2.Salary)
    FROM Employees e2
    WHERE e2.DepartmentId = e.DepartmentId
);
            

How Does a Correlated Subquery Work?

  1. The outer query selects an employee.
  2. The inner query finds the average salary for that employee's department.
  3. The inner query uses e.DepartmentId from the outer query.
  4. The employee's salary is compared with the calculated department average.
  5. The process is repeated for the other employees.

Normal Subquery vs Correlated Subquery

Normal Subquery Correlated Subquery
Can generally execute independently. Depends on the outer query.
Does not normally reference the outer query. References a column from the outer query.
Generally evaluated independently of individual outer rows. Evaluated in relation to rows of the outer query.

Example: Highest Paid Employee in Each Department

The following correlated subquery finds employees whose salary is equal to the maximum salary in their department.


SELECT
    e.EmployeeName,
    e.DepartmentId,
    e.Salary
FROM Employees e
WHERE e.Salary =
(
    SELECT MAX(e2.Salary)
    FROM Employees e2
    WHERE e2.DepartmentId = e.DepartmentId
);
            

The inner query calculates the maximum salary for the department of the current employee.

Important Points

  • A correlated subquery depends on the outer query.
  • The inner query references a column from the outer query.
  • Table aliases make correlated subqueries easier to read.
  • Correlated subqueries are useful for row-by-row comparisons.
  • They can be used with aggregate functions such as AVG(), MAX() and MIN().
  • For large datasets, query performance should be considered when using correlated subqueries.

Summary

A correlated subquery is a subquery that depends on the outer query. It references a column from the outer query and is evaluated in relation to the rows being processed. It is especially useful for comparing each row with aggregated or related data from the same group.