LeetCode Database题解
2016-08-01 21:41
323 查看
175. Combine Two Tables
使用外连接即可。
176. Second Highest Salary
查询第二高工资值,可能无此值。使用聚集函数与子查询即可,注意MAX可能返回NULL。
177. Nth Highest Salary
查询第N高工资值,可能无此值。使用IFNULL,然后再对结果进行限制即可。
178. Rank Scores
按得分排名并计算排名。使用子查询、聚集函数,然后再对结果进行排序即可。
180. Consecutive Numbers
找出至少连续出现3次的Num。先排序,再查询即可。也有人使用变量来解答。
181. Employees Earning More Than Their Managers
查询工资比上司高的员工。使用自连接即可。
182. Duplicate Emails
查找表中有重复的emails。使用数据分组或自连接即可。
183. Customers Who Never Order
查出没有订单的客户。使用数据过滤即可。
184. Department Highest Salary
查询每个部内最高的工资,最高工资可能有多个员工。这题相对来说稍复杂,使用子查询与数据分组来解决。
使用外连接即可。
# Write your MySQL query statement below select FirstName, LastName, City, State from Person left outer join Address on Person.PersonId = Address.PersonId; # Write your MySQL query statement below select p.FirstName, p.LastName, a.City, a.State from Person as p left outer join Address as a on p.PersonId = a.PersonId;
176. Second Highest Salary
查询第二高工资值,可能无此值。使用聚集函数与子查询即可,注意MAX可能返回NULL。
# Write your MySQL query statement below select max(Salary) as SecondHighestSalary from Employee where Salary < (select max(Salary) from Employee); # Write your MySQL query statement below select max(Salary) as SecondHighestSalary from Employee where Salary not in(select max(Salary) from Employee);
177. Nth Highest Salary
查询第N高工资值,可能无此值。使用IFNULL,然后再对结果进行限制即可。
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN DECLARE M INT; SET M = N - 1; RETURN ( # Write your MySQL query statement below. SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1), NULL) ); END
178. Rank Scores
按得分排名并计算排名。使用子查询、聚集函数,然后再对结果进行排序即可。
# Write your MySQL query statement below select Scores.Score, (select count(*) from (select distinct Score from Scores) as DistinctScores where DistinctScores.Score >= Scores.Score) as Rank from Scores order by Score desc; # Write your MySQL query statement below select Scores.Score, count(DistinctScores.Score) as Rank from Scores, (select distinct Score from Scores) as DistinctScores where Scores.Score <= DistinctScores.Score group by Scores.Id, Scores.Score order by Scores.Score desc;
180. Consecutive Numbers
找出至少连续出现3次的Num。先排序,再查询即可。也有人使用变量来解答。
# Write your MySQL query statement below select distinct(l1.Num) as ConsecutiveNums from Logs l1, Logs l2, Logs l3 where l1.Id + 1 = l2.Id and l1.Id + 2 = l3.Id and l1.Num = l2.Num and l1.Num = l3.Num;
181. Employees Earning More Than Their Managers
查询工资比上司高的员工。使用自连接即可。
# Write your MySQL query statement below select e1.Name as Employee from Employee as e1, Employee as e2 where e1.ManagerId = e2.Id and e1.Salary > e2.Salary;
182. Duplicate Emails
查找表中有重复的emails。使用数据分组或自连接即可。
# Write your MySQL query statement below select Email from Person group by Email having count(*) > 1; # Write your MySQL query statement below select distinct(p1.Email) from Person as p1, Person as p2 where p1.Id != p2.Id and p1.Email = p2.Email;
183. Customers Who Never Order
查出没有订单的客户。使用数据过滤即可。
# Write your MySQL query statement below select Name as Customers from Customers where Customers.Id not in (select distinct(CustomerId) from Orders);
184. Department Highest Salary
查询每个部内最高的工资,最高工资可能有多个员工。这题相对来说稍复杂,使用子查询与数据分组来解决。
# Write your MySQL query statement below select Department.Name as Department, Employee.Name as Employee, max(Salary) as Salary from Employee, Department where Employee.DepartmentId = Department.Id; # 错误解法,结果会出现[null, null, null]这种情况 # Write your MySQL query statement below select Department.Name as Department, Employee.Name as Employee, Salary from Employee, Department where Employee.DepartmentId = Department.Id order by Salary limit 1; # 错误解法,结果只会有一行 # Write your MySQL query statement below select d.Name as Department, e.Name as Employee, e.Salary as Salary from Department as d, (select Name, max(Salary) as Salary, DepartmentId from Employee group by DepartmentId) as e where d.Id = e.DepartmentId; # 错误解法,当同一部门有两个最高的Salary时候,结果只有一行,故必须使用下面解法,使用2个Employee表 # Write your MySQL query statement below select d.Name as Department, e.Name as Employee, e.Salary as Salary from Department as d, Employee as e, (select max(Salary) as Salary, DepartmentId from Employee group by DepartmentId) as h where d.Id = h.DepartmentId and e.Salary = h.Salary and e.DepartmentId = h.DepartmentId;
相关文章推荐
- [LeetCode 题解]: Merge k Sorted Lists
- [LeetCode]题解(python):084-Largest Rectangle in Histogram
- LeetCode 226. Invert Binary Tree 题解 —— Java
- [题解][LeetCode][Pascal's Triangle II]
- 【LeetCode题解】160_相交链表
- 【LeetCode题解】61_旋转链表(Rotate-List)
- [LeetCode]题解(python):138-Copy List with Random Pointer
- [题解][LeetCode][Scramble String]
- leetcode题解||Regular Expression Matching 问题
- leetcode 题解 || 3Sum 问题
- [LeetCode]题解(python):007-Reverse Integer
- LeetCode 357. Count Numbers with Unique Digits 题解(C++)
- LeetCode题解:Pow(x,n)
- LeetCode题解:Odd Even Linked List
- LeetCode题解:Pascal's Triangle II
- LeetCode题解:Bulls and Cows
- leetcode题解-125. Valid Palindrome && 680. Valid Palindrome II
- LeetCode题解——Median of Two Sorted Arrays
- Leetcode题解--18_Fraction to Recurring Decimal