当前位置:网站首页>Leetcode day4 the highest paid employee in the Department
Leetcode day4 the highest paid employee in the Department
2022-07-28 19:44:00 【wyqgg123】
184. The highest paid employees in the Department
Medium difficulty
SQL framework
Employee Table contains all employee information , Each employee has its own corresponding Id, salary and department Id.
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department The table contains information about all departments of the company .
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL Inquire about , Identify the highest paid employees in each department . For the above table , Your SQL The query should return the following line ( The order of the lines doesn't matter ).
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
explain :
Max and Jim stay IT The wages of all departments are the highest ,Henry The highest salary is in the sales department .
Answer key :
1、 Even the table query
Through the description of the topic, it is not difficult to see that we first need to pass the two tables DepartmentID Connect
sql Code :
select Department.Name as Department, Employee.Name as Employee,Salary
from Employee
left join Department
on Employee.DepartmentId = Department.Id
The operation results are shown in the following table :
| Department | Employee | Salary |
|---|---|---|
| IT | Joe | 70000 |
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
| IT | Max | 90000 |
Next, we can know from the meaning of the topic , As long as the salary is the highest in the Department, the employee information needs to be displayed , So we can't use max() Aggregate function when the result of the query , We need to max() As a condition of judgment , When the salary of the employees in the Department is equal to the maximum salary, we will display the information of the changed employees .
2、 Construction conditions
The maximum salary of each department can be found by group query .
select max(Salary),DepartmentId
from Employee
group by DepartmentId
Running results :
| max(Salary) | DepartmentId |
|---|---|
| 90000 | 1 |
| 80000 | 2 |
Here we can use in Conditions to judge , When departmentid and max(Salary) Show the employee information when they are satisfied .
3、 Code implementation
sql Code
select Department.Name as Department, Employee.Name as Employee,Salary
from Employee
left join Department
on Employee.DepartmentId = Department.Id
where
(Salary,Department.Id)
in
(
select max(Salary),DepartmentId
from Employee
group by DepartmentId
)
It's used here in() Pass two parameters in , It means that only when these two conditions are met can the conditions be met .
Running results :
| Department | Employee | Salary |
|---|---|---|
| IT | Max | 90000 |
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
边栏推荐
- VAE: understanding and implementation of variational self encoder
- MATLAB实现的图像分割之边缘检测和连接
- 认识中小型局域网WLAN
- leetcode day3 超过经理收入的员工
- How does app automated testing achieve H5 testing
- 英语文章翻译-英语文章翻译软件-免费批量翻译
- leetcode day1 分数排名
- 串口接收应用——环形缓冲buffer
- WPF 实现带蒙版的 MessageBox 消息提示框
- When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
猜你喜欢

Force buckle 1331. Array serial number conversion

How does app automated testing achieve H5 testing

source insight项目导入和使用教程

Basic concept and essence of Architecture

VAE: understanding and implementation of variational self encoder

Rust 入门指南(rustup, cargo)

架构基本概念和架构本质

How navicate modifies the database name

Rust Getting Started Guide (crite Management)

Saltstack system initialization
随机推荐
微信公众号授权登录后报redirect_uri参数错误的问题
【笔记】《结网:互联网产品经理改变世界》
redis 主从架构(sizeof函数怎么计算)
leetcode day3 查找重复的电子邮箱
Thoroughly understand bit operations - shift left, shift right
When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
Scrapy Spider源码分析
MySQL8 tmp_table_size和max_heap_table_size
Netcoreapi operation excel table
In order to develop high-end photoresist, Jingrui Co., Ltd. invested 75million yuan to purchase SK Hynix ASML lithography machine
架构基本概念和架构本质
leetcode day1 分数排名
11. Learn MySQL union operator
Redis笔记
诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商
【微信小程序开发】页面导航与传参
Sword finger offer II 109. unlock the password lock
C language cycle sentence strengthening exercises
Pytoch: implementation of crossentropyloss and labelsmoothing
How does app automated testing achieve H5 testing