leetcode高阶sql
时间: 2023-11-27 09:05:53 浏览: 78
LeetCode是一个在线的技术面试平台,提供了许多算法和数据结构的题目,同时也提供了许多SQL的题目。下面是一些LeetCode高阶SQL的例子:
1. 部门工资最高的员工
```sql
SELECT Department.Name as Department, Employee.Name as Employee, Salary
FROM Employee
JOIN Department ON Employee.DepartmentId = Department.Id
WHERE (DepartmentId, Salary) IN (
SELECT DepartmentId, MAX(Salary)
FROM Employee
GROUP BY DepartmentId
)
```
2. 每门课程成绩都大于80分的学生姓名
```sql
SELECT s1.Student
FROM Scores s1
WHERE NOT EXISTS (
SELECT *
FROM Scores s2
WHERE s1.Student = s2.Student AND s2.Score <= 80
)
```
3. 连续出现的数字
```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
```
阅读全文