当前位置:网站首页>LeetCode185. All employees with the top three highest wages in the Department (MySQL)
LeetCode185. All employees with the top three highest wages in the Department (MySQL)
2022-06-13 03:29:00 【TRX1024】
Employee Table contains all employee information , Each employee has his / her work number Id, full name Name, Wages Salary And department number DepartmentId .
+----+-------+--------+--------------+
| 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 |
+----+-------+--------+--------------+
Department The table contains information about all departments of the company .
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL Inquire about , Find out all the employees who get the top three salaries in each department . for example , According to the table given above , Query results should be returned :
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
explain :
IT In the Department ,Max Got the highest wage ,Randy and Joe All got the second highest salary ,Will The third highest salary . Sales Department (Sales) There are only two employees ,Henry The highest salary ,Sam The second highest salary .
Method 1
Ideas : First sub department , Sort the salary of each department rank, Then output to each department rank<=3 Employee information
Sort method step by step :LeetCode 178. Score ranking (Mysql)
select
Department,
Employee,
Salary
from (
select
d.Name as Department,
e.Name as Employee,
e.Salary as Salary,
(select count(distinct Salary) from Employee where Salary >= e.Salary and DepartmentId = e.DepartmentId) as `rank`
from Employee e join Department d on e.DepartmentId = d.Id
group by e.DepartmentId,e.Name,d.Name
order by e.Salary desc
) res where res.`rank` <=3;Method 2
Ideas : In front of the company 3 A high salary means no more than 3 The salary is bigger than these .
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 );
边栏推荐
- Graph data modeling tool
- [JVM Series 7] garbage collector
- Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
- Neil eifrem, CEO of neo4j, interprets the chart data platform and leads the development of database in the next decade
- How to write product requirements documents
- look on? What is the case between neo4j and ongdb?
- C语言程序设计——从键盘任意输入一个字符串(可以包含:字母、数字、标点符号,以及空格字符),计算其实际字符个数并打印输出,即不使用字符串处理函数strlen()编程,但能实现strlen()的功能。
- CXGRID keeps the original display position after refreshing the data
- How to Draw Useful Technical Architecture Diagrams
- Doris creates OLAP, mysql, and broker tables
猜你喜欢

Figure data * reconstruction subgraph

PostgreSQL common SQL

Complex network analysis capability based on graph database

Dish recommendation system based on graph database

简述:分布式CAP理论和BASE理论

Get to know druid IO real time OLAP data analysis storage system
![[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)](/img/5d/1fc04b4384af07b953c99ec37383b3.jpg)
[azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
![Loading process of [JVM series 3] classes](/img/a7/707c5cb95de71d95bf6ad9b2f69afa.jpg)
Loading process of [JVM series 3] classes

Masa auth - SSO and identity design

(九)详解广播机制
随机推荐
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
Masa auth - overall design from the user's perspective
MASA Auth - SSO与Identity设计
Stack information, GC statistics
Level II C test skills
PHP import classes in namespace
Masa Auth - SSO and Identity Design
CXGRID keeps the original display position after refreshing the data
Four ways of array traversal in PHP
[JVM Series 2] runtime data area
KITTI数据集无法下载的解决方法
Masa auth - SSO and identity design
Technical documentbookmark
Union, intersection and difference sets of different MySQL databases
Configuration and practice of shardingsphere JDBC sub database separation of read and write
[JVM series 4] common JVM commands
Figure data * reconstruction subgraph
Part II. S3. intuitionistic fuzzy multi-attribute decision-making method when attribute weight is intuitionistic fuzzy number
Unified scheduling and management of dataX tasks through web ETL
Data of all bank outlets in 356 cities nationwide (as of February 13, 2022)