先看题目自己试着解一下,有困难的同学可以先看考点解析试下,
最后再看SQL解答,有不同意见和解法的欢迎评论区留言交流
来源:大厂面试真题
题目:
来源:面试真题
Whose salary is higher than their manager for Month “2018-05” ? (Base on “Salary” table)
table:Salary
EmployeeID | ManagerID | Salary | Month |
---|---|---|---|
1001 | 1003 | 2000 | 2018-05 |
1002 | 1003 | 2500 | 2018-05 |
1003 | 1004 | 2250 | 2018-05 |
1004 | 1005 | 3000 | 2018-05 |
1001 | 1003 | 1500 | 2018-06 |
1002 | 1003 | 1500 | 2018-06 |
1003 | 1004 | 1050 | 2018-06 |
1004 | 1005 | 2400 | 2018-06 |
题目数据下载
结果输出
考点解析
表的自连接,内连接,左连接和where条件筛选
逻辑口述:文章来源:https://www.toymoban.com/news/detail-524217.html
每个人都是员工也可能是另一个员工的经理,用managerID 和EmployeeID 去管理,得到不同经理的工资,然后去做比较即可文章来源地址https://www.toymoban.com/news/detail-524217.html
SQL代码
# 方法一 自连接
SELECT
s1.EmployeeID, s1.ManagerID,
s1.Salary Salary_Employee,
S2.Salary Salary_Manager,S1.`Month`
FROM salary s1,salary s2
where s1.ManagerID = s2.EmployeeID
and s1.`Month` = s2.`Month`
and s1.`Month` = "2018-05"
and s1.Salary > S2.Salary;
# 方法二 inner自连接
SELECT
s1.EmployeeID,
s1.ManagerID,
s1.Salary Salary_employee,
s2.Salary Salary_Manager,
S1.`Month`
FROM salary s1
inner join salary s2
on s1.ManagerID = S2.EmployeeID
and s1.`Month` = s2.`Month`
where s1.`Month` = "2018-05"
having Salary_employee > Salary_Manager
--having也可以写在where 里 and s1.Salary > s2.Salary;
# 方法三 左连接
SELECT * FROM salary s1
left join
(select distinct EmployeeID,Salary,`Month` from salary) s2
on s1.ManagerID = S2.EmployeeID
and s1.`Month` = s2.`Month`
where s1.`Month` = "2018-05";
到了这里,关于100天SQL面试刷题 Day005的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!