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
相关推荐
LNTON羚通3 小时前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
重生之Java开发工程师5 小时前
MySQL中的CAST类型转换函数
数据库·sql·mysql
哭泣的眼泪4085 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
清炒孔心菜5 小时前
每日一题 LCR 078. 合并 K 个升序链表
leetcode
Microsoft Word5 小时前
c++基础语法
开发语言·c++·算法
天才在此6 小时前
汽车加油行驶问题-动态规划算法(已在洛谷AC)
算法·动态规划
莫叫石榴姐7 小时前
数据科学与SQL:组距分组分析 | 区间分布问题
大数据·人工智能·sql·深度学习·算法·机器学习·数据挖掘
茶猫_8 小时前
力扣面试题 - 25 二进制数转字符串
c语言·算法·leetcode·职场和发展
安迁岚8 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验三 数据操作
运维·服务器·数据库·sql·mysql
安迁岚8 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验九 触发器
数据库·sql·mysql·oracle·实验报告