Correlated subqueries for beginners (step by step walkthrough to find Nth highest salary in sql)
Correlated Subqueries Let's explore correlated subqueries with the example of finding the 3rd highest salary among 5 employees. A correlated subquery is a subquery that references columns from the outer query. It executes once for each row processed by the outer query. Example Table: Employees ID Name Salary 1 Alice 9000 2 Bob 8000 3 Carol 8500 4 Dave 7000 5 Eve 9500 Query to Find the 3rd Highest Salary SELECT Salary FROM Employees AS E1 WHERE 2 = ( SELECT COUNT(DISTINCT Salary) FROM Employees AS E2 WHERE E2.Salary > E1.Salary ) Step-by-Step Wa...