Red Glitter Pointer

 

https://leetcode.com/problems/rising-temperature/description/

 

SELECT today.id AS id
FROM weather AS today
    INNER JOIN weather AS yesterday ON DATE_ADD(yesterday.recordDate, INTERVAL 1 DAY) = today.recordDate
WHERE today.temperature > yesterday.temperature

 

 

느낀 점 및 정리 ✍️

1. 셀프 조인의 경우, 동일 테이블을 사용하는 것인 만큼 별칭을 잘 활용해야 헷갈리지 않게 작성할 수 있는 것 같다

 

 

 

https://leetcode.com/problems/employees-earning-more-than-their-managers/description/

 

SELECT e.name AS Employee
FROM employee AS e
    INNER JOIN employee AS m ON e.managerid = m.id
WHERE e.salary > m.salary

 

느낀 점 및 정리 ✍️

1. 셀프 조인을 사용하였다

 

 

 

https://leetcode.com/problems/customers-who-never-order/description/

 

 

 

SELECT name AS Customers
FROM customers
    LEFT JOIN orders ON customers.id = orders.customerid
WHERE orders.customerid IS NULL

 

느낀 점 및 정리 ✍️

1. 한 번도 주문하지 않은 고객을 찾기 위해 INNER JOIN이 아닌 OUTER JOIN을 활용했다.

2. customers.id와 orders.customerId로  JOIN 시킨 뒤, Orders의 id가 NULL인 customer의 name을 가져왔다.

 

 

 

https://leetcode.com/problems/reformat-department-table/description/

 

 

SELECT id
        ,SUM(CASE WHEN month = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue
        ,SUM(CASE WHEN month = 'Feb' THEN revenue ELSE NULL END) AS Feb_Revenue
        ,SUM(CASE WHEN month = 'Mar' THEN revenue ELSE NULL END) AS Mar_Revenue
        ,SUM(CASE WHEN month = 'Apr' THEN revenue ELSE NULL END) AS Apr_Revenue
        ,SUM(CASE WHEN month = 'May' THEN revenue ELSE NULL END) AS May_Revenue
        ,SUM(CASE WHEN month = 'Jun' THEN revenue ELSE NULL END) AS Jun_Revenue
        ,SUM(CASE WHEN month = 'Jul' THEN revenue ELSE NULL END) AS Jul_Revenue
        ,SUM(CASE WHEN month = 'Aug' THEN revenue ELSE NULL END) AS Aug_Revenue
        ,SUM(CASE WHEN month = 'Sep' THEN revenue ELSE NULL END) AS Sep_Revenue
        ,SUM(CASE WHEN month = 'Oct' THEN revenue ELSE NULL END) AS Oct_Revenue
        ,SUM(CASE WHEN month = 'Nov' THEN revenue ELSE NULL END) AS Nov_Revenue
        ,SUM(CASE WHEN month = 'Dec' THEN revenue ELSE NULL END) AS Dec_Revenue
FROM department
GROUP BY id

 

 

느낀 점 및 정리 ✍️

1. CASE를 활용하여 피벗 테이블을 만들었다

2. 예를 들어, month의 값이 Jan인 revenue의 값들을 합산하여 Jan_Revenue에 값을 넣어주는 것이다.

 

 

https://leetcode.com/problems/duplicate-emails/description/

 

 

 

👇 정답 코드

SELECT Email
FROM Person
GROUP BY Email
HAVING COUNT(id) > 1

 

 

https://leetcode.com/problems/big-countries/description/

 

 

 

SELECT name, population, area
FROM world
WHERE area >= 3000000
OR population >= 25000000

 

 

 

 

https://leetcode.com/problems/not-boring-movies/description/

 

 

 

 

SELECT *
FROM cinema
WHERE description <> 'boring'
AND MOD(id, 2) = 1
ORDER BY rating DESC

 

느낀 점 및 정리 ✍️

1. odd-numbered ID => MOD(id, 2) = 1

2. description that is not boring => <> 'boring'

3. ordered by rating in descending order => ORDER BY rating DESC

 

 

+ Recent posts

loading