leetcode SQL题目

文章目录

组合两个表

sql 复制代码
SELECT firstName,lastName,city,state FROM Person LEFT JOIN Address ON Person.PersonId=Address.PersonId

第二高的薪水

sql 复制代码
SELECT
IFNULL(
(SELECT DISTINCT salary FROM Employee Order by salary desc LIMIT 1,1),null
) AS SecondHighestSalary

第N高的薪水

sql 复制代码
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) 
  );
END

分数排名

sql 复制代码
SELECT score, (SELECT count(DISTINCT score)+1 FROM Scores S2 Where S2.score>Scores.score) AS 'rank' FROM Scores ORDER BY score DESC

连续出现的数字

sql 复制代码
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

超过经理收入的员工

sql 复制代码
SELECT e.name AS Employee FROM Employee e,Employee e2 WHERE e2.id=e.managerId AND e.salary > e2.salary

查找重复的电子邮件

sql 复制代码
SELECT DISTINCT p1.email AS Email FROM Person p1,Person p2 WHERE p1.email=p2.email AND p1.id !=p2.id

从不订购的客户

sql 复制代码
SELECT name AS Customers FROM Customers WHERE id NOT IN (SELECT DISTINCT customerId FROM Orders)

部门工资最高的员工

sql 复制代码
SELECT Department.name AS Department,e.name AS Employee,e.salary AS Salary FROM Department,Employee e WHERE Department.id=e.departmentId AND e.salary >= ALL(SELECT salary FROM Employee e2 WHERE e.departmentId = e2.departmentId)

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

sql 复制代码
SELECT Department.name AS Department, e.name AS Employee,e.salary AS Salary FROM Employee e,Department WHERE e.departmentId=Department.id AND
(SELECT COUNT(Distinct e2.salary) FROM Employee e2 WHERE e2.departmentId=e.departmentId AND e2.salary>e.salary)<3 ORDER BY e.departmentId ASC

删除重复的电子邮箱

sql 复制代码
DELETE p2 FROM Person p1,Person p2 WHERE p1.email=p2.email AND p1.id<p2.id

上升的温度

sql 复制代码
SELECT w2.id AS Id FROM Weather w1,Weather w2 WHERE DATEDIFF(w2.recordDate,w1.recordDate)=1 AND w1.Temperature < w2.Temperature

DATEDIFF()函数用于计算两个日期的天数差

游戏玩法分析Ⅰ

sql 复制代码
SELECT player_id,MIN(event_date) AS first_login FROM Activity GROUP BY player_id

游戏玩法Ⅳ

sql 复制代码
SELECT ROUND(
(SELECT COUNT(DISTINCT b.player_id) FROM Activity b WHERE (b.player_id,b.event_date) IN
(
SELECT a.player_id,DATE(MIN(a.event_date)+INTERVAL 1 DAY) FROM Activity a GROUP BY a.player_id
))
/
(SELECT COUNT(DISTINCT player_id) FROM Activity),2
) AS fraction
相关推荐
九圣残炎3 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
lulu_gh_yu8 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
成富25 分钟前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
songqq2726 分钟前
SQL题:使用hive查询各类型专利top 10申请人,以及对应的专利申请数
数据库·sql
丫头,冲鸭!!!28 分钟前
B树(B-Tree)和B+树(B+ Tree)
笔记·算法
Re.不晚32 分钟前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
为什么这亚子1 小时前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
2 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
~yY…s<#>2 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
幸运超级加倍~3 小时前
软件设计师-上午题-16 算法(4-5分)
笔记·算法