Leetcode database 刷题 MySQL 简单部分
程序员文章站
2022-03-29 22:56:51
Leetcode database 刷题MySQL简单部分176#select( select distinct salary from employee order by salary desc limit 1,1) as SecondHighestSalary这里的distinct为了防止有重复的工资,select出来null,套的select是为了防止nulllimit a,b 也可以写成limit a offset b,都表达跳过b个,取a个值。......
Leetcode database 刷题
MySQL简单部分
176# 查第二高的工资
select(
select distinct salary from employee order by salary desc
limit 1,1
) as SecondHighestSalary
这里的distinct为了防止有重复的工资;
当employee表里只有一条数据时,也就是说没有第二高的工资,内层select语句返回结果是null,当把内层当成临时表,外层select可以输出结果,无论是不是null。(或者用if null)
SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1),
NULL) AS SecondHighestSalary
limit a,b 也可以写成limit a offset b,都表达跳过b个,取a个值。
181# 找出工资比经理高的员工
select a.name as 'employee'
from employee a join employee b
on a.managerid=b.id
where a.salary>b.salary
需要搞清楚谁是主表
182# 找到重复的邮箱
select email from person group by email having count(email)>1
注意group by, having, where的应用场景,一个句子里的顺序是where, group by, having。where里不能有聚合函数。
183# 选出没有订单的客户
select c.name as customers
from customers c where c.id
not in (select orders.customerid from orders)
not in 的用法
196# 删除重复邮件
DELETE p1 FROM Person p1,
Person p2
WHERE
p1.Email = p2.Email AND p1.Id > p2.Id
需要注意delete的用法:DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL(多表删除)
197# 查温度比昨天高的序号
SELECT w2.Id
FROM Weather w1, Weather w2
WHERE DATEDIFF(w2.RecordDate, w1.RecordDate) = 1
AND w1.Temperature < w2.Temperature
对这种要自己和自己比较的题目,先用自连接,这题要再利用datediff函数
511# 512# 游戏玩法分析
使用min函数和嵌套联合查询
577# 员工奖金
注意null值也要显示在结果内
584# 寻找用户推荐人
is null/is not null的用法
!=/<>表示不等关系
586# 订单最多的客户
count(*)
595# 大的国家
select name, population, area from world where population>25000000 or area>3000000
596# 大于五个人
select class from courses group by class having count(distinct student)>=5
本文地址:https://blog.csdn.net/joan244/article/details/107415776
上一篇: java线程间通讯的一些方法总结