秋招学习数据库LeetCode刷题

数据库基本知识以前学过次数较多,今天看完一遍后都是可以理解的。直接刷Leetcode题吧
牛客上题库刷基础,Leetcode刷 写语句题(争取坚持每日2个sql语句题)


牛客:https://www.nowcoder.com/exam/intelligent?questionJobId=10&tagId=21015
Leetcode:https://leetcode.cn/problems/second-highest-salary/description/

20240403

1.第二高的薪水

在这里插入图片描述

select  max(salary) as SecondHighestSalary
from Employee 
where salary<(select max(salary) from Employee)

2.第N高薪水

在这里插入图片描述
在这里插入图片描述

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT; 
    SET M = N-1; 
  RETURN (
      SELECT DISTINCT salary
      FROM Employee
      ORDER BY salary DESC
      LIMIT M, 1
  );
END

美妙!!!limit这个函数是想不到是这样用,思路灵活好重要

2024.04.06(清明放假玩了2天)

178.分数排名

在这里插入图片描述

select score,dense_rank() over(order by score desc) as "rank"
from Scores
order by score desc;

180.连续出现的数字

在这里插入图片描述

select
    distinct t.num as ConsecutiveNums 
from
(
    select 
        id,
        num,
        row_number() over(order by id) as rn,
        row_number() over(partition by num order by id) as id_rn
    from Logs 
) t
group by t.num, (t.rn - t.id_rn)
having count(1) >= 3
;

20240407

10个牛客sql题

181.超过经理收入的员工

在这里插入图片描述

select  
    a.Name as 'Employee' From  
       Employee AS a,     Employee AS b where  
          a.ManagerId=b.Id          and a.Salary > b.Salary;

182.查找重复的电子邮箱

在这里插入图片描述第一想法
耗时长

select Email  from Person group by  Email having count(Email)>1
select Email 
from (   select Email, count(Email) as num   from Person   group by Email ) 
as statistic 
where num > 1 ;
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值