181题目地址:https://oj.leetcode.com/problems/employees-earning-more-than-their-managers/
181题又是一个简单题目,给定一个Employee表,里面存储了雇员的工资信息,包括名字、工资、经理ID,题目要求写一个sql查找出所有那些自身工资比经理还高的雇员的名字。
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
这个题目很简单,现有表不能做就是因为现有的一行记录里没有包含经理的工资信息,但是有经理的ID,那么我们做一下关联,把工资信息拿到,再过滤就好了,于是思路sql如下:
select
Name as Employee
from(
select
o1.Name
,o1.Salary as s
,o2.Salary as m
from(
select * from Employee
)o1
join(
select * from Employee
)o2
on(o1.ManagerId=o2.Id)
)t
where s>m
其中s是自己的工资,m是经理的工资~~一目了然
182题目地址:https://oj.leetcode.com/problems/duplicate-emails/
182也是Easy级别题目,题目描述就是写一个sql,把Person表中有重复Email的记录拉出来。
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
很容易想到的思路:按照email做聚合,把count>1的取出来,对应sql如下:
select
Email
from(
select
Email
,count(Id) as cnt
from Person
group by Email
)t
where cnt>1
183题目地址:https://oj.leetcode.com/problems/customers-who-never-order/
183题,一个网站包含两张表,一张Customers表存放客户数据,一张Orders表存放产生订单的客户ID,题目要求写sql查出没有在网站产生过订单的客户。说白了就是查询在Customers里而不在Orders里的数据,sql如下:
select
o1.Name as Customers
from(
select * from Customers
)o1
left outer join(
select * from Orders
)o2
on(o1.Id=o2.CustomerId )
where o2.CustomerId is null
3个简单题目,夯实sql基础~~