数据库基本知识以前学过次数较多,今天看完一遍后都是可以理解的。直接刷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 salaryFROM EmployeeORDER BY salary DESCLIMIT M, 1);
END
美妙!!!limit这个函数是想不到是这样用,思路灵活好重要
178.分数排名
select score,dense_rank() over(order by score desc) as "rank"
from Scores
order by score desc;
180.连续出现的数字
selectdistinct 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_rnfrom Logs
) t
group by t.num, (t.rn - t.id_rn)
having count(1) >= 3
;