当前位置:网站首页>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 );
边栏推荐
- Doris creates OLAP, mysql, and broker tables
- IP address and classification
- Summary of virtualization technology development
- Scala method and function notes
- [azure data platform] ETL tool (9) -- ADF performance optimization case sharing (1)
- Masa Auth - SSO and Identity Design
- Isolation level, unreal read, gap lock, next key lock
- Several functions in YAF framework controller
- [JVM Series 5] performance testing tool
- English grammar_ Frequency adverb
猜你喜欢

2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)

Azure SQL db/dw series (12) -- using query store (1) -- report Introduction (1)

The latest collation of the number of years of education per capita in the country and provinces -1989-2020- includes the annual original data, calculation process and result summary

QML connecting to MySQL database

MASA Auth - 从用户的角度看整体设计

brew工具-“fatal: Could not resolve HEAD to a revision”错误解决

Panel data set of rural cities and towns: per capita consumption and expenditure of prefecture level cities 2012-2019 & rural data of provinces 2013-2019

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

Reading notes of effective managers

Masa auth - overall design from the user's perspective
随机推荐
Sparksql of spark
MySQL learning summary 8: addition, deletion and modification of data processing
Least recently used cache (source force deduction)
Level II C preparation -- basic concepts of program design
MySQL learning summary 12: system variables, user variables, definition conditions and handlers
Carbon neutralization & Patent Innovation: multi indicator data such as patent panels (original documents) of provinces, cities and counties, and the number of low-carbon patents authorized
[JVM series 4] common JVM commands
Use PHP to count command line calls on your computer
Onnx+tensorrt+yolov5: yolov5 deployment based on trt+onnx 1
The most complete ongdb and neo4j resource portal in history
[azure data platform] ETL tool (3) - azure data factory copy from local data source to azure
Panel data set of rural cities and towns: per capita consumption and expenditure of prefecture level cities 2012-2019 & rural data of provinces 2013-2019
[azure data platform] ETL tool (8) - ADF dataset and link service
Parallel one degree relation query
2016. maximum difference between incremental elements
C language programming - input a string arbitrarily from the keyboard, calculate the actual number of characters and print out. It is required that the string processing function strlen() cannot be us
MySQL and PostgreSQL installation subtotal
PHP import classes in namespace
MASA Auth - SSO与Identity设计
Pollution discharge fees of listed companies 2010-2020 & environmental disclosure level of heavy pollution industry - original data and calculation results