vijay
vijay

Reputation: 11

Whats the purpose of n in where clause in sql query

SELECT * 
FROM Employees Emp1 
WHERE (n) = ( SELECT COUNT(DISTINCT(Emp2.Salary)) 
              FROM Employees Emp2 
              WHERE Emp2.Salary >= Emp1.Salary )

Upvotes: 0

Views: 78

Answers (1)

har07
har07

Reputation: 89315

I think what matters most is the subquery. It returns number of distinct salary that is greater than or equals to current Emp1.Salary. This value returned is equal to the employee's salary rank.

Assume that you're the employee with third greatest salary, 10000. The subquery will count number of distinct salary that is greater than you which is 2, plus one (2+1=3). Plus one counted from employee, including your self, having salary equals to 10000. This is because >= used in the WHERE clause.

Having said that, it makes perfect sense that the entire query select employee based on his salary rank.

Upvotes: 1

Related Questions