leetcode上的数据库题汇总(1)

  1. Combine Two Tables

https://leetcode.com/problems/combine-two-tables/

 select a.FirstName,a.LastName,b.City,b.State from Person a LEFT JOIN Address b on a.PersonId=b.PersonId
  1. Second Highest Salary

https://leetcode.com/problems/second-highest-salary/

 select MAX(Salary) as SecondHighestSalary from Employee where Salary < (select MAX(Salary) from Employee)
  1. Nth Highest Salary

https://leetcode.com/problems/nth-highest-salary/

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  declare M INT;
  set M=N-1;
  RETURN (
      # Write your MySQL query statement below.
      select distinct Salary 
      from Employee order by Salary desc
      limit M,1
  );
END
  1. Rank Scores

https://leetcode.com/problems/rank-scores/

SELECT S.Score, COUNT(DISTINCT T.Score) AS Rank
FROM Scores S 
JOIN Scores T ON S.Score <= T.Score
GROUP BY S.Id
ORDER BY S.Score DESC
  1. Consecutive Numbers

https://leetcode.com/problems/consecutive-numbers/

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

你可能感兴趣的:(mysql数据库总结,LeetCode刷题练习,leetcode做题代码合集)