I'm trying to understand below query, how its working.
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary
)
Lets say I have 5
distinct salaries and want to get 3rd
largest salary. So Inner query will run first and then outer query ?
I'm getting confused how its being done in sql engine. Curious to know. Becasue if its 3rd
largest then 3-1 = 2
, so that 2
needs to be matched with inner count as well. How inner count is being operated.
Can anyone explain the how its working .. ?