LeetCode--数据库

1.176. 第二高的薪水

问题:

分析:

  1. 找出第几个高的 可以先排序再用 limit:
    1.   limit  x,y  表示  从x 开始 抽取y条数据 ,x从0开始
  2. 没有第二高则返回null,则说明两种情况,a.数据表只有一条数据,b.多条重复数据
    1. 只一条数据查询不出来,使用ifnull 可以将查不出来的数据转为 null  输出
    2. 多条重复 使用  distinct
  3. 注意:最后的输出是要改 为SecondHighestSalary 
select  ifnull(
(select distinct salary from Employee order by salary desc limit 1,1),null )     SecondHighestSalary

 

2.181. 超过经理收入的员工

分析:

1.同一张表的话应该使用内连接查询 inner

2. 同时各自的工资和经理比较 ,

select e.name from employee e inner join employee m on e.ManagerId=m.Id  and e.salary>m.salary

解析:

select e.name from employee e inner join employee m on e.ManagerId=m.Id  

实际上相当于将employee 表进行了扩展,形成了下面的新表

select e.name from employee e inner join employee m on e.ManagerId=m.Id  and e.salary>m.salary

形成新表后 再进行字段的比较大小

3.182. 查找重复的电子邮箱

分析:

去重复使用 distinct

找重复怎么搞呢, 可以按 email 分组,再计算每组的数量 () 【having 是分组后使用的查询条件】  

select email from person group by email having count(*)>1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值