NO.2 简单 (176. 第二高的薪水)
# 将第一高的工资筛选掉,再次用MAX就可以取到第二高的工资了。
select max(Salary) as SecondHighestSalary from Employee
where Salary <>
(select max(Salary) from Employee)
NO.3 中等 (184. 部门工资最高的员工)
# 先用group by的方法求出部门最高薪资,再接着判断Employee表中哪些数据符合即可,
# 过程中会用到join来连接Employee表和Department表。
select d.Name Department, e.Name Employee, Salary
from Employee e
join Department d on e.DepartmentId=d.Id
where (DepartmentId, Salary) in
(
select DepartmentId, MAX(Salary)
FROM Employee
group by DepartmentId
)
NO.14 简单 (182. 查找重复的电子邮箱)
# 相同邮箱但不同Id即为重复数据(记得DISTINCT去重)
SELECT DISTINCT p1.Email
FROM Person p1, Person p2
WHERE p1.Email=p2.Email AND p1.Id<>p2.Id
SQL求出每门课程成绩都大于80分的学生姓名
表A
name subject score
zhangsan math 80
zhangsan chinese 90
wangwu math 70
wangwu chinese 85
aa math 90
aa chinese 95
最后结果:aa
思路一:比较复杂
1.对表A的name分组,计算count
2.对表A的name分组where score>80 ,计算count
3.用1的结果去左连接2的结果,当count相等即可。
select name
from
(select name,count(name) as num
from A
group by name)a
left join
(select name,count(name) as num
from A where score>80
group by name)b
on a.name=b.name where a.num=b.num
思路二:反向求解(name只要不在小于80的里面)
先求出分数小于等于80的name作为条件
select name from A where name not in (select name from A where score<=80)