版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/86589478
我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/department-top-three-salaries/description/
题目描述:
知识点:JOIN连接查询、子查询、DISTINCT关键字
思路:JOIN子句实现两表连接查询,子查询查询薪水排名,DISTINCT去除重复薪水
时间复杂度是O(n ^ 2),其中n为Employee表中的记录数。
SQL语句:
SELECT
t1.Name AS Department, t2.Name AS Employee, t2.Salary AS Salary
FROM
Employee AS t2 JOIN Department AS t1 ON t2.DepartmentId = t1.Id
WHERE
(SELECT
COUNT(DISTINCT Salary)
FROM
Employee AS t3
WHERE
t3.Salary > t2.Salary AND t3.DepartmentId = t2.DepartmentId) < 3;
LeetCode解题报告: