4

SQL比较弱求解释下这条SQL(子查询)

```表Employee：
1 +----+-------+--------+--------------+
2 | Id | Name  | Salary | DepartmentId |
3 +----+-------+--------+--------------+
4 | 1  | Joe   | 70000  | 1            |
5 | 2  | Henry | 80000  | 2            |
6 | 3  | Sam   | 60000  | 2            |
7 | 4  | Max   | 90000  | 1            |
8 | 5  | Janet | 69000  | 1            |
9 | 6  | Randy | 85000  | 1            |
10 +----+-------+--------+--------------+

1 +----+----------+
2 | Id | Name     |
3 +----+----------+
4 | 1  | IT       |
5 | 2  | Sales    |
6 +----+----------+

1 +------------+----------+--------+
2 | Department | Employee | Salary |
3 +------------+----------+--------+
4 | IT         | Max      | 90000  |
5 | IT         | Randy    | 85000  |
6 | IT         | Joe      | 70000  |
7 | Sales      | Henry    | 80000  |
8 | Sales      | Sam      | 60000  |
9 +------------+----------+--------+```

```SELECT
a. NAME AS Department,
b. NAME AS Employee,
b.Salary
FROM
Department a,
Employee b
WHERE
b.DepartmentId = a.Id
AND (
SELECT
count(DISTINCT Salary)
FROM
Employee
WHERE
DepartmentId = a.Id
AND Salary > b.Salary
) < 3
ORDER BY
Department```

Lewe2010

--- 共有 2 条评论 ---
Lewe2010回复 @快速开发师 : 为什么用<3呢？ 2年前

查询工资水平在其部门排名前4的员工，不知道这样说对不对
--- 共有 2 条评论 ---

Lewe2010这个我知道，但是我是想知道SQL的思路 子查询那块 2年前