[SQL] LeetCode 문제풀이 (#577, #595, #607, #610, #619)

Jinyoung Cheon·2026년 4월 23일

LeetCode

목록 보기
5/9

577. Employee Bonus

문제설명

Write a solution to report the name and bonus amount of each employee who satisfies either of the following:

  • The employee has a bonus less than 1000.
  • The employee did not get any bonus.

Return the result table in any order.

두가지 조건을 만족하는 employee를 추출하는 문제이다.

1. 보너스가 1000보다 이하인 employee
2. 보너스를 받지 않은 employee 

LEFT JOIN을 활용하면 쉽게 풀 수 있는 문제였다!

# Write your MySQL query statement below
SELECT e.name, b.bonus
FROM Employee e
LEFT JOIN Bonus b 
ON e.empId = b.empId
WHERE b.bonus < 1000 
OR b.bonus IS NULL;

595. Big Countries

문제설명

A country is big if:

it has an area of at least three million (i.e., 3000000 km2), or
it has a population of at least twenty-five million (i.e., 25000000).
Write a solution to find the name, population, and area of the big countries.

Return the result table in any order.

해당 조건을 만족하는 나라를 출력하는 되는 문제이다.

SELECT name, population, area

FROM World

WHERE area >= 3000000
    OR population >= 25000000

WHERE절에 해당 조건을 넣어주기만 하면 되기 때문에 쉽게 풀 수 있었다.

607. Sales Person

문제 설명

Write a solution to find the names of all the salespersons who did not have any orders related to the company with the name "RED".

Return the result table in any order.

The result format is in the following example.

SalesPerson Company Orders 3개의 테이블이 주어져서 어떻게 해결해야할까 고민을 많이 했다. 처음에는 SalesPerson Orders 두개의 테이블을 이용해서 WHERE o.com_id != 1 조건을 사용했지만 이건 "RED 회사와 거래한 적이 있는 사람 전체를 제외해야 하는데, RED 회사와 거래한 '개별 주문 건'만 제외하기 때문"에 원하는 방식이다.

RED 회사와 거래한 sales_id 목록"을 먼저 구한 뒤, 그 목록에 포함되지 않은 사람만 뽑는 방식을 사용했다.

# Write your MySQL query statement below
# "RED 회사와 거래한 sales_id 목록"을 먼저 구한 뒤, 그 목록에 포함되지 않은 사람만 뽑는 방식

SELECT name

FROM SalesPerson

WHERE sales_id NOT IN (
    SELECT o.sales_id
    FROM Company c
    JOIN Orders o
    ON c.com_id = o.com_id
    WHERE c.name = 'RED'
)

610. Triangle Judgement

문제설명

Report for every three line segments whether they can form a triangle.

Return the result table in any order.

삼각형을 만들기 위한 조건이 제일 어려웠던 문제였다.. 생각 안나.. 🤦🏻‍♂️

삼각형 결정 조건(Triangle Inequality Theorem)
이 정리에 따르면, 세 변 중 어떤 두 변의 길이를 더해도 나머지 한 변의 길이보다 항상 커야 한다.
즉, x,y,zx, y, z가 삼각형을 이루려면 다음 세 가지 조건을 모두 만족

  • x+y>zx + y > z
  • x+z>yx + z > y
  • y+z>xy + z > x
SELECT x, y, z,
    CASE 
        WHEN (x+y > z) AND (x+z > y) AND (y+z > x) THEN 'Yes'
        ELSE 'No'    
        END AS triangle

FROM Triangle

619. Biggest Single Number

문제설명

A single number is a number that appeared only once in the MyNumbers table.

Find the largest single number. If there is no single number, report null.

SELECT MAX(num) AS num
FROM (
    SELECT num
    FROM MyNumbers
    GROUP BY num
    HAVING COUNT(num) = 1
) AS single_nums;

집계함수는 NULL을 자동으로 반환한다!!

집계 함수의 결과값 동작

함수결과 집합이 비어있을 때 (대상 없음)
MAX()NULL
MIN()NULL
SUM()NULL
AVG()NULL
COUNT()0

NULL이 나오는 것이 싫고, 데이터가 없으면 0이나 다른 기본값을 보여주고 싶다면
COALESCE(값, 대체값)을 활용한다.
COALESCE(값, 대체값)은 첫 번째 값이 NULL이면 두 번째 값을 반환한다.

SELECT COALESCE(MAX(num), 0) AS num
FROM (
    SELECT num
    FROM MyNumbers
    GROUP BY num
    HAVING COUNT(num) = 1
) AS single_numbers;
profile
데이터를 향해, 한 걸음씩 천천히.

0개의 댓글