leetcode의 데이터베이스 문제 집합 (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
    
  • Second Highest Salary

  • https://leetcode.com/problems/second-highest-salary/
     select MAX(Salary) as SecondHighestSalary from Employee where Salary < (select MAX(Salary) from Employee)
    
  • 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
    
  • 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
    
  • 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
    

    좋은 웹페이지 즐겨찾기