employee
表包含所有員工資訊,每個員工有其對應的 id, salary 和 department id 。
+----+-------+--------+--------------+
| id | name | salary | departmentid |
+----+-------+--------+--------------+
| 1 | joe | 70000 | 1 |
| 2 | henry | 80000 | 2 |
| 3 | sam | 60000 | 2 |
| 4 | max | 90000 | 1 |
| 5 | janet | 69000 | 1 |
| 6 | randy | 85000 | 1 |
+----+-------+--------+--------------+
department
表包含公司所有部門的資訊。
+----+----------+
| id | name |
+----+----------+
| 1 | it |
| 2 | sales |
+----+----------+
編寫乙個 sql 查詢,找出每個部門工資前三高的員工。例如,根據上述給定的**,查詢結果應返回:
+------------+----------+--------+
| department | employee | salary |
+------------+----------+--------+
| it | max | 90000 |
| it | randy | 85000 |
| it | joe | 70000 |
| sales | henry | 80000 |
| sales | sam | 60000 |
+------------+----------+--------+
思路1:把184. 部門工資最高的員工的第二段**的and後面的條件改一下
select d.name as department, e.name as employee, e.salary
from employee as e, department as d
where e.departmentid= d.id
and (select count(distinct e2.salary) from employee as e2 where e2.departmentid=e.departmentid and e2.salary > e.salary) <3
order by e.departmentid, e.salary desc
思路2:把每個部門前三薪水及對應部門存在乙個新錶中
select d.name as department, e.name as employee, e.salary
from employee as e, department as d,
(select e1.id as id, e1.salary as salary11
from employee as e1
where (select count(distinct e2.salary) from employee as e2 where e2.departmentid = e1.departmentid and e2.salary > e1.salary )<3
)as t -- 每個部門
where e.departmentid= d.id
and e.id= t.id
order by e.departmentid, e.salary desc
185 部門工資前三高的所有員工
employee 表包含所有員工資訊,每個員工有其對應的工號 id,姓名 name,工資 salary 和部門編號 departmentid department 表包含公司所有部門的資訊。編寫乙個 sql 查詢,找出每個部門獲得前三高工資的所有員工。例如,根據上述給定的表,查詢結果應返回 it 部...
185 部門工資前三高的所有員工
employee 表包含所有員工資訊,每個員工有其對應的工號 id,姓名 name,工資 salary 和部門編號 departmentid id name salary departmentid 1 joe 85000 1 2 henry 80000 2 3 sam 60000 2 4 max 9...
部門工資前三高所有員工
employee 表包含所有員工資訊,每個員工有其對應的工號 id,姓名 name,工資 salary 和部門編號 departmentid id name salary departmentid 1 joe 85000 1 2 henry 80000 2 3 sam 60000 2 4 max 9...