[LeetCode][SQL]Department Top Three Salaries

Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the 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            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query tofind employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

https://leetcode.com/problems/department-top-three-salaries/



不愧是hard,特别费劲。

一开始想得很简单,就像一个循环一样,外层塞一个DepartmentId给内层,内层去找这个部门下的top3的,轻松愉快。

问题是MySQL不支持这语法Orz.

‘This version of MySQL doesn‘t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery‘

1 select D.Name, E1.Name, E1.Salary from Employee E1, Department D where E1.Id in
2 (
3     select E2.Id from Employee E2
4     where E1.DepartmentId = E2.DepartmentId
5     order by E2.Salary desc
6 )
7 and E1.DepartmentId = D.Id
8 order by E1.DepartmentId, E1.Salary desc

然后换了个曲线救国的写法,还是外层塞DepartmentId进去,里层数有几个。

搞了很久都挂在这个case上:

1 insert into Employee values(‘1‘, ‘Joe‘, ‘70000‘, ‘1‘);
2 insert into Employee values(‘2‘, ‘Henry‘, ‘80000‘, ‘1‘);
3 insert into Employee values(‘3‘, ‘Sam‘, ‘80000‘, ‘1‘);
4 insert into Employee values(‘4‘, ‘Max‘, ‘90000‘, ‘1‘);
5
6 insert into Department values(‘1‘, ‘IT‘);

Case期望4条都选出来....

本来是count(*),最后改成count(distinct(E2.Salary)) 就过了

1 select D.Name, E1.Name, E1.Salary from Employee E1, Department D
2 where (
3     select count(distinct(E2.Salary)) from Employee E2
4     where E1.DepartmentId = E2.DepartmentId
5     and E1.Id <> E2.ID
6     and E1.Salary < E2.Salary
7 ) < 3
8 and E1.DepartmentId = D.Id
9 order by E1.DepartmentId, E1.Salary desc
时间: 2024-10-13 16:27:13

[LeetCode][SQL]Department Top Three Salaries的相关文章

LeetCode Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000

【SQL】185. Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000

185. Department Top Three Salaries (Hard)

Source: https://leetcode.com/problems/department-top-three-salaries/#/description Description: The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. +----+-------+--------+--------------+

leetcode数据库sql之Department Top Three Salaries

leetcode原文引用: How would you print just the 10th line of a file? For example, assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Your script should output the tenth line, which is: Li

leetcode185 部门工资前三高的所有员工 Department Top Three Salaries

Employee 表包含所有员工信息,每个员工有对应的 Id,此外还有一列部门 Id. 创建表和数据: Create table If Not Exists Employee (Idint, Name varchar(255), Salary int, DepartmentId int); Create table If Not Exists Department (Idint, Name varchar(255)); Truncate table Employee; insert into E

LeetCode SQL题目(第一弹)

LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才能使用,我没有使用SSMS 或Workbench,而是直接使用sqlcmd,解决登陆问题可以参考这个链接(http://www.cnblogs.com/skynothing/archive/2010/08/26/1809125.html)感谢这位博主的帮助. 181. Employees Earni

SQL的top语句

TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: SELECT TOP number|percent column_name(s) FROM table_name MySQL 和 Oracle 中的 SQL SELECT TOP 是等价的 MySQL 语法 SELECT column_name(s) FROM table_name LIMIT number 例子

SQL SELECT TOP 语句

SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP 子句对于拥有数千条记录的大型表来说,是非常有用的. 注释:并非所有的数据库系统都支持 SELECT TOP 子句. SQL Server / MS Access 语法 SELECT TOP number|percent column_name(s) FROM table_name; MySQL 和 Oracle 中的 SQL SELECT TOP 是等价的 MySQL 语法 SELECT column_name(s)

[9]SQL SELECT TOP, LIMIT, ROWNUM

[9]SQL SELECT TOP, LIMIT, ROWNUM SQL SELECT TOP 实例 下面的 SQL 语句从 "Websites" 表中选取头两条记录: SELECT * FROM Websites LIMIT 2;