题目

查询部门工资前三高的员工。

我用的数据库是oracle。

下面是数据表的信息。

Employee表数据:

| ID | NAME | Salary | DepartmentId |
| -- | ---- | ------ | ------------ |
|1 | Joe | 85000 | 1 |
|2 | Henry | 80000 | 2 |
|3 | Sam | 60000 | 2 |
|4 | Max | 90000 | 1 |
|5 | Janet | 69000 | 1 |
|6 | Randy | 85000 | 1 |
|7 | Will | 70000 | 1 |
|8 | edav | 50000 | 2 |
|9 | easonv | 40000 | 2 |

8、9行为我自行添加,为了更清晰展示查询结果。

创建表

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。

create table Employee (
Id number(5),
Name varchar2(10) ,
Salary number(5),
DepartmentId number(5)
);

Department 表包含公司所有部门的信息。

create table Department  (
Id number(5),
Name varchar2(10)
);

插入数据Employee,脚本如下

insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('1', 'Joe', '85000', '1'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('2', 'Henry', '80000', '2'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('3', 'Sam', '60000', null); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('4', 'Max', '90000', '1'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('5', 'Janet', '69000', '1'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('6', 'Randy', '85000', '1'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('7', 'Will', '70000', '1'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('8', 'eda', '50000', '2'); insert into Employee (ID, NAME, SALARY, DEPARTMENTID)
values ('9', 'eason', '40000', '2');

插入数据Department,脚本如下

insert into Department (ID, NAME)
values ('1', 'IT'); insert into Department (ID, NAME)
values ('2', 'Sales');

查询

以下使用四种SQL语句查出的结果,前两个是用oracle特有函数,后两个是标准SQL92写法。

你觉得哪个对?哪个性能高?

函数1 ROW_NUMBER

select Department,Employee,Salary
from (select (ROW_NUMBER()
over(PARTITION by t1.departmentid order by Salary desc)) lev,
t2.name Department,
t1.name Employee,
t1.Salary Salary
from Employee t1, Department t2
where t1.departmentid = t2.id) A
where lev <= 3;

函数2 dense_rank

select D.Name Department, E.Name Employee, E.Salary Salary
from (select Name,
Salary,
DepartmentId,
dense_rank() over(partition by DepartmentId order by Salary desc) Trank
from Employee) E
right join Department D
on E.DepartmentId = D.id
where Trank <= 3;

通用写法1

select d.name as Department, e.name as Employee, e.salary as Salary
from employee e
inner join department d
on e.DepartmentId = d.id
where (select count(distinct salary)
from employee
where salary > e.salary
and departmentid = e.DepartmentId) < 3
order by e.departmentid, Salary desc;

通用写法2

SELECT t3.name Department, t2.name Employee, t2.salary Salary
FROM Employee t2, Department t3
WHERE t2.id NOT IN (SELECT b.id
FROM Employee a, Employee b
WHERE a.DepartmentId = b.DepartmentId
AND a.salary > b.salary
GROUP BY b.id
HAVING COUNT(*) >= 3)
AND t2.DepartmentId = t3.id
ORDER BY Department, t2.salary DESC;

吐槽

感兴趣的同学可以自己跑下。

我个人觉得所谓官方答案是有问题的。

官方题解如下,mysql版本:

SELECT d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
FROM Employee e1
JOIN Department d
ON e1.DepartmentId = d.Id
WHERE 3 > (SELECT COUNT(DISTINCT e2.Salary)
FROM Employee e2
WHERE e2.Salary > e1.Salary
AND e1.DepartmentId = e2.DepartmentId);

改写成oracle版,加上排序:

SELECT d.Name Department, e1.Name Employee, e1.Salary
FROM Employee e1
JOIN Department d
ON e1.DepartmentId = d.Id
WHERE 3 > (SELECT COUNT(DISTINCT e2.Salary)
FROM Employee e2
WHERE e2.Salary > e1.Salary
AND e1.DepartmentId = e2.DepartmentId)
order by d.id,salary desc

查出来的数据是与通用写法1一样的,

两个同样的85000的数据

|序号| Department | Employee | Salary |
|--- | ---- | ------- | ------------ |
|1 | IT | Max | 90000
|2 | IT | Randy | 85000
|3 | IT | Joe | 85000
|4 | IT | Will | 70000
|5 | Sales | Henry | 80000
|6 | Sales | Sam | 60000
|7 | Sales | eda| 50000

这个题目出的歧义太大,如果是在考试中,应该是查出前三名、前四名的都对。

个人认为应该查出前三名应该是不包含70000这条数据的,就算是并列第二,那么就应该没有第三了,高校排名不也是这样吗?

所以私以为正确答案应该是查出这样的数据

|序号| Department | Employee | Salary |
|--- | ---- | ------- | ------------ |
|1 | IT | Max | 90000
|2 | IT | Randy | 85000
|3 | IT | Joe | 85000
|4 | Sales | Henry | 80000
|5 | Sales | Sam | 60000
|6 | Sales | eda| 50000

那么,我写的四条语句中,应该是函数1及通用写法2可以满足这个条件。


我的公众号

Leetcode的SQL题解:185. 部门工资前三高的员工的更多相关文章

  1. [SQL]LeetCode185. 部门工资前三高的员工 | Department Top Three Salaries

    SQL 架构 Create table If Not Exists Employee (Id ), Salary int, DepartmentId int) Create table If Not ...

  2. sql查询:部门工资前三高的员工和部门工资最高的员工

    创建表:Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int);Cr ...

  3. 185. 部门工资前三高的所有员工 + 多表联合 + join + dense_rank()

    185. 部门工资前三高的所有员工 LeetCode_MySql_185 题目描述 方法一:使用join on # Write your MySQL query statement below sel ...

  4. SQL查询每个部门工资前三名的员工信息

    --通用sql select deptno, ename, sal from emp e1 where ( ) from emp e2 where e2.deptno=e1.deptno and e2 ...

  5. 部门工资前三高的所有员工 - LeetCode

    Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId . +----+-------+--------+---- ...

  6. mysql查询每个部门/班级前几名

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id . +----+-------+--------+--------------+ | I ...

  7. [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  8. LeetCode:184.部门工资最高的员工

    题目链接:https://leetcode-cn.com/problems/department-highest-salary/ 题目 Employee 表包含所有员工信息,每个员工有其对应的 Id, ...

  9. leetcode 184 部门工资最高的员工

    题目描述:Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. Department 表包含公司所有部门的信息. 编写一个 SQL 查询,找 ...

随机推荐

  1. 阿里巴巴 -- MySQL DBA 面试题

    1.MySQL的复制原理以及流程 (1).先问基本原理流程,3个线程以及之间的关联: (2).再问一致性延时性,数据恢复: (3).再问各种工作遇到的复制bug的解决方法. 2.MySQL中myisa ...

  2. 基于 MySQL Binlog 的 Elasticsearch 数据同步实践 原

    一.背景 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品.订单等数据的多维度检索. 使用 Elasticsearch 存储业务数据可以 ...

  3. C语言学习书籍推荐《明解C语言》下载

    柴田望洋 (作者), 管杰 (译者), 罗勇 (译者) <明解C语言>是日本的C语言经典教材,自出版以来不断重印.修订,被誉为“C语言圣经”.作者在日本IT界家喻户晓,出版过一系列极富影响 ...

  4. PKIX:unable to find valid certification path to requested target

    1.Communications link failure,The last packet successfully received from the server was * **millisec ...

  5. Pownerdesigner画用例图_类图_时序图

    1. 问题描述 软件过程中,设计阶段有几个常用的工具:Rational Rose.Visio.Pownerdesigner,一般用Rose用例图/类图/时序图,Visio画流程图,Pownerdesi ...

  6. ServiceFabric极简文档-2 部署环境搭建-配置文件

    类型:ClusterConfig.Unsecure.MultiMachine 说明:至少3台机子 { "name": "SampleCluster", &quo ...

  7. python爬虫笔记之用cookie访问需要登录的网站

     目标:用cookie访问一个需要登录的网站 如图,直接访问会跳转到登录页面,提示登录. 运行结果: 直接在浏览器上输入该url,网站立马跳转到登录页面.  方法: 1.先手动登录,通过抓包获取coo ...

  8. 知识图谱学习与实践(4)——通过例句介绍Sparql的使用

    通过例句介绍Sparql的使用 1 简介 SPARQL的定义,是一个递归的定义,为SPARQL Protocal and RDF Query Language,是W3C制定的RDF知识图谱标准查询语言 ...

  9. 基于 Github 存储附件的 wordpress 插件

    前言 插件名称 wp-github-gos, 本插件核心功能使用了 GitHub API 设置页面和核心业务逻辑主要参考插件 wordpress-qcloud-cos 的实现,替换了其中 腾讯云 CO ...

  10. linux初学者-sshd服务

     linux初学者-sshd服务   在linux系统操作中,经常需要连接其他的主机,连接其他主机的服务是openssh-server,它的功能是让远程主机可以通过网络访问sshd服务,开始一个安全s ...