这个查询如何工作?我在第1行感到困惑

时间:2015-07-21 12:45:28

标签: sql

员工

DatabaseName

查询:

===========================
|  Employee ID | Salary    |
===========================
|     3         |  200     |
|     4         |   800    |
|     7         |   450    |
============================

以上查询输出"员工"的第二高薪。表。 Line1如何工作?我不熟悉语法类型:

SELECT *
FROM Employee Emp1
WHERE (1) = (          --Line1
             SELECT COUNT(DISTINCT(Emp2.Salary))
             FROM Employee Emp2
             WHERE Emp2.Salary > Emp1.Salary)

1 个答案:

答案 0 :(得分:0)

这样可能更有意义:

SELECT *
FROM Employee Emp1
WHERE 
(SELECT COUNT(DISTINCT(Emp2.Salary))
    FROM Employee Emp2
    WHERE Emp2.Salary > Emp1.Salary)
 = 1

基本上就像你说的那样 - 选择薪水第二高的所有员工。