hdonghun / SQL

1 stars 0 forks source link

인프런 - SQL 고급 강의 듣고 공부 05 _ WINDOW함수로 풀기 #28

Open hdonghun opened 2 years ago

hdonghun commented 2 years ago

LeetCode -180. Consecutive Numbers 출처 : https://leetcode.com/problems/consecutive-numbers/

Table: Logs

+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ id is the primary key for this table.

Write an SQL query to find all numbers that appear at least three times consecutively.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: Logs table: +----+-----+ | id | num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ Output: +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+ Explanation: 1 is the only number that appears consecutively for at least three times.

hdonghun commented 2 years ago

답안 : WINDOW 함수 - LEAD

SELECT DISTINCT l.num AS ConsecutiveNums FROM( SELECT num, LEAD(num,1) OVER (ORDER BY id) AS next, LEAD(num,2) OVER (ORDER BY id) AS afternext FROM Logs) l WHERE l.num = l.next AND l.num = l.afternext

hdonghun commented 2 years ago

LeetCode - 184. Department Highest Salary 출처 : https://leetcode.com/problems/department-highest-salary/

Table: Employee +--------------+---------+ | Column Name | Type | +--------------+---------+ | id | int | | name | varchar | | salary | int | | departmentId | int | +--------------+---------+

id is the primary key column for this table. departmentId is a foreign key of the ID from the Department table. Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.

Table: Department +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+

id is the primary key column for this table. Each row of this table indicates the ID of a department and its name. Write an SQL query to find employees who have the highest salary in each of the departments. Return the result table in any order.

The query result format is in the following example. Example 1:

Input: Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Jim | 90000 | 1 | | 3 | Henry | 80000 | 2 | | 4 | Sam | 60000 | 2 | | 5 | Max | 90000 | 1 | +----+-------+--------+--------------+ Department table: +----+-------+ | id | name | +----+-------+ | 1 | IT | | 2 | Sales | +----+-------+

Output: +------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Jim | 90000 | | Sales | Henry | 80000 | | IT | Max | 90000 | +------------+----------+--------+ Explanation: Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.

hdonghun commented 2 years ago

답안 : SELECT ms.Department, ms.name AS Employee, ms.Salary AS Salary FROM ( SELECT department.name AS Department , Employee.name , Employee.Salary , MAX(salary) OVER (PARTITION BY departmentid) AS max_salary From Employee INNER JOIN Department ON Employee.departmentId = Department.id ) ms WHERE ms.salary = ms.max_salary

hdonghun commented 2 years ago

LeetCode - 185. Department Top Three Salaries 출처 : https://leetcode.com/problems/department-top-three-salaries/ Table: Employee

+--------------+---------+ | Column Name | Type | +--------------+---------+ | id | int | | name | varchar | | salary | int | | departmentId | int | +--------------+---------+ id is the primary key column for this table. departmentId is a foreign key of the ID from the Department table. Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.

Table: Department +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+ id is the primary key column for this table. Each row of this table indicates the ID of a department and its name.

A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department. Write an SQL query to find the employees who are high earners in each of the departments. Return the result table in any order.

The query result format is in the following example. Example 1:

Input: Employee table: +----+-------+--------+--------------+ | id | name | salary | departmentId | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | | 7 | Will | 70000 | 1 | +----+-------+--------+--------------+ Department table: +----+-------+ | id | name | +----+-------+ | 1 | IT | | 2 | Sales | +----+-------+

Output: +------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Joe | 85000 | | IT | Randy | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+ Explanation: In the IT department:

In the Sales department: