leetcode上的数据库题汇总(1)

本文精选了LeetCode上的经典SQL挑战题,包括表格数据整合、获取次高薪资、找出第N高薪资、成绩排名及连续数字的查询。通过实际案例,深入解析SQL查询技巧,帮助读者掌握复杂查询的编写方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Combine Two Tables

https://leetcode.com/problems/combine-two-tables/

 select a.FirstName,a.LastName,b.City,b.State from Person a LEFT JOIN Address b on a.PersonId=b.PersonId
  1. Second Highest Salary

https://leetcode.com/problems/second-highest-salary/

 select MAX(Salary) as SecondHighestSalary from Employee where Salary < (select MAX(Salary) from Employee)
  1. Nth Highest Salary

https://leetcode.com/problems/nth-highest-salary/

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  declare M INT;
  set M=N-1;
  RETURN (
      # Write your MySQL query statement below.
      select distinct Salary 
      from Employee order by Salary desc
      limit M,1
  );
END
  1. Rank Scores

https://leetcode.com/problems/rank-scores/

SELECT S.Score, COUNT(DISTINCT T.Score) AS Rank
FROM Scores S 
JOIN Scores T ON S.Score <= T.Score
GROUP BY S.Id
ORDER BY S.Score DESC
  1. Consecutive Numbers

https://leetcode.com/problems/consecutive-numbers/

select distinct l1.Num as ConsecutiveNums
from  Logs l1, Logs l2, Logs l3 
where l1.Id=l2.Id-1 and l2.Id=l3.Id-1 and l1.Num = l2.Num and l2.Num = l3.Num
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值