LeetCode sql题 笔记

简单:

 1. 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

IdSalary
1  100  
2200  
3300

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

SecondHighestSalary
200  

 

select 
MAX (case when t.a = '2' then t.Salary else null end ) 'SecondHighestSalary' 
from 
(select Salary,dense_rank() over (order by Salary desc) a
from Employee
) t

2.查找重复的电子邮箱

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

Id
 Email
1
a@b.com
2
c@d.com
3
a@b.com

 

根据以上输入,你的查询应返回以下结果:

 Email
a@b.com

说明:所有电子邮箱都是小写字母。

select Email
from Person
group by Email
Having  count(Email) >1

 

3.从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers
Id
Name  
1
Joe  
2
Henry
3
Sam
4
Max
Orders
Id 
CustomerId 
13
21

 

例如给定上述表格,你的查询应返回:

结果表
Customers 
Henry     
Max       

 

select c.Name as 'Customers'
from Customers c
left join Orders o
on c.ID = o.CustomerId 
where o.ID is null

 

 

4.删除重复的电子邮箱

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

Person
Id 
Email            
1
john@example.com
2
bob@example.com  
3
john@example.com

 

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

结果表
Id 
Email            
1john@example.com
2bob@example.com

 

delete  from Person
where Id not in(
    select p.nid 
    from
    (select min(id) over (partition by Email) nid
    from Person) p
)

中等:

 1.第N高的薪水

  编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

IdSalary
1  100  
2200  
3300

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

SecondHighestSalary(2)
200  

 

CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS
BEGIN
    RETURN (
       select 
        MAX (case when t.a = @N then t.Salary else null end ) 'SecondHighestSalary' 
        from 
            (select Salary,dense_rank() over (order by Salary desc) a
            from Employee
            ) t
        
    );
END

2.编写一个 SQL 查询来实现分数排名。

如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

Id 
score
1
3.50 
2
3.65 
3
4.00
4
3.85
5
4.00
6
3.65

 

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

score
score
4.00
1 
3.00
1 
3.85
2
3.65
3
3.50
4

 

重要提示:对于 MySQL 解决方案,如果要转义用作列名的保留字,可以在关键字之前和之后使用撇号。例如 `Rank`

select Score,dense_rank() over(order by Score desc ) Rank
from Scores
order by Score desc

 

3.部门工资最高的员工

Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。

Id 
Name  
Salary 
DepartmentId 
1  
Joe   
70000  
1
2
Jim 
90000  
1
3
Henry  
80000  
2
4
Sam   
60000  
2
5
Max 
90000  
1

 

Department 表包含公司所有部门的信息。

Id 
Name     
1
IT       
2
Sales    

 

编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

Department 
Employee 
Salary 
IT         
Max      
90000  
IT         
Jim      
90000
Sales      
Henry    
80000  

 

解释:

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

select t.n1 as 'Department', t.n2 'Employee',t.s1 'Salary'
from 
(select d.Name n1,e.Name n2,e.Salary s1,dense_rank() over ( partition by e.DepartmentId order by e.Salary desc) r
    from Employee e
    join Department d
    on  e.DepartmentId = d.Id 
) t
where t.r = 1

困难:

1.部门工资前三高的所有员工

Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。

Id 
Name  
Salary 
DepartmentId 
1  
Joe   
85000  
1
2
Henry 
80000  
2
3
Sam   
60000  
2
4
Max   
90000  
1
5
Janet 
69000  
1
6
Randy 
85000  
1
7
Will  
70000  
1

 

Department 表包含公司所有部门的信息。

Id 
Name     
1
IT       
2
Sales    

 

编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

Department 
Employee 
Salary 
IT         
Max      
90000  
IT         
Randy    
85000  
IT         
Joe      
85000  
IT         
Will     
70000  
Sales      
Henry    
80000  
Sales      
Sam      
60000  

 

解释:

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

select t.n1 as 'Department', t.n2 'Employee',t.s1 'Salary'
from 
(select d.Name n1,e.Name n2,e.Salary s1,dense_rank() over ( partition by e.DepartmentId order by e.Salary desc) r
    from Employee e
    join Department d
    on  e.DepartmentId = d.Id 
) t
where t.r in (1,2,3)

 

 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/second-highest-salary

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值