mysql-leetcode01

01.LeetCode组合俩个表


image.png
# Write your MySQL query statement below
select  
    person.FirstName,person.LastName,Address.City,Address.State 
from 
    person 
left join 
    Address 
on 
    person.personId=Address.personId

02.第二高的薪水


image.png
#Oracle可以用nvl代替 ifnull
select 
    ifnull((select distinct(salary) from Employee order by salary desc limit 1,1),null)
as 
    SecondHighestSalary
#oracle 查询排名第二名的数据
select *
from (select name, cj, dense_rank() over(order by cj) pm from xscj s)
where pm = 2

03.第N高薪水


image.png
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N=N-1;
  RETURN (
      # Write your MySQL query statement below.
      select ifnull((select distinct Salary 
      from Employee order by Salary desc limit N,1),null) as getNthHighestSalary
  );
END

04.分数排名


image.png
#方法一
select score,dense_rank() over (order by score desc) 'rank'
from scores;
#方法二
SELECT Score,
 (SELECT count(DISTINCT score) FROM Scores WHERE score >= s.score) AS 'Rank' 
FROM Scores s 
ORDER BY Score DESC;

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Hello!!我回归啦~ 今天开始继续刷SQL的题目~ 争取这个寒假可以在SQL有一点点进步! 闯关开始啦 关卡1...
    认真学习的兔子阅读 319评论 0 1
  • 题目选自leetcode 上的题库 可能不一定都是最优解,答案仅供参考每道题后面都应相应的难度等级,如果没时间做的...
    顾子豪阅读 830评论 0 6
  • 176. 第二高的薪水[https://leetcode-cn.com/problems/second-highe...
    1nvad3r阅读 53评论 0 0
  • 175. 组合两个表[https://leetcode-cn.com/problems/combine-two-t...
    寒江老翁阅读 214评论 0 0
  • (Since 2020.10.14-2021.3.10) LeetCode刷题笔记,共两百多题,记录整理如下: 动...
    周恩国的学习笔记阅读 747评论 0 1