当前位置:网站首页>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 |
边栏推荐
- Basic concept and essence of Architecture
- 投资35.45亿元!格力集团参与小米产业基金
- stc12c5a60s2功能说明(STC12C5A60S2默认触发)
- Cell综述:人类微生物组研究中的单细胞方法
- VAE: understanding and implementation of variational self encoder
- Cell review: single cell methods in human microbiome research
- 基于MATLAB的函数拟合
- navicate修改数据库名的方式
- 中国首枚芯片邮票面世:内置120um超薄NFC芯片
- 【笔记】《启示录》:产品经理的实践经验与反省清单
猜你喜欢
随机推荐
Basic concept and essence of Architecture
【笔记】《启示录》:产品经理的实践经验与反省清单
中国首枚芯片邮票面世:内置120um超薄NFC芯片
彻底理解位运算——与(&)、非(~)、或(|)、异或(^)
English article translation - English article translation software - free batch translation
NPM installing and uninstalling global packages
Nokia expands its 5g agreement with BT and will become its largest wireless access device supplier
CodeIgnier框架实现restful API接口编程
Redis master-slave architecture (how to calculate sizeof function)
How does app automated testing achieve H5 testing
博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
个人博克系统登录点击图形验证码的集成与实现
shared_ptr 和 make_shared 的使用
NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
After reading the thesis for three years, I learned to read the abstract today
诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商
读了三年论文,我今天才学会阅读摘要
App自动化测试是怎么实现H5测试的
Test Development Notes
VAE: understanding and implementation of variational self encoder









