当前位置:网站首页>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 |
边栏推荐
- lua语言的左对齐函数(手写)
- String中常用的API
- 峰值速率超2Gbps!高通在中国首发通过5G毫米波MIMO OTA测试
- MIR专题征稿 | 常识知识与推理:表示、获取与应用 (10月31日截稿)
- Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
- 中国首枚芯片邮票面世:内置120um超薄NFC芯片
- Iclr21 (classification) - future classic "vit" an image is worth 16x16 words (including code analysis)
- Rust Getting Started Guide (crite Management)
- npm安装和卸载全局包
- Leetcode day2 连续出现的数字
猜你喜欢

When CNN meets transformer cmt:revolutionary neural networks meet vision transformers

leetcode day1 分数排名

企业级分布式爬虫框架入门

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters

Oracle insert数据时字符串中有‘单引号问题

英语文章翻译-英语文章翻译软件-免费批量翻译

基于QTGUI图像界面的空战游戏设计

English article translation - English article translation software - free batch translation

Convertible bond concept table x notation gives you a convenient and fast experience!
随机推荐
App自动化测试是怎么实现H5测试的
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
Serial port receiving application ring buffer
Rust 入门指南(crate 管理)
Design of library management database system
云计算笔记part.2——应用管理
诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商
This customized keyboard turns me on~
Saltstack system initialization
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
China's first chip stamp released: built-in 120um ultra-thin NFC chip
Rust Getting Started Guide (modules and engineering structures)
MATLAB实现的图像分割之边缘检测和连接
MySQL8 基于clone创建主从复制
个人博克系统登录点击图形验证码的集成与实现
投资35.45亿元!格力集团参与小米产业基金
MySQL8 Status Variables: Internal Temporary Tables and Files
leetcode day4 部门工资最高的员工
Android-第十三节03xUtils-数据库框架(增删改查)详解