简体   繁体   中英

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 )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM