코린이의 소소한 공부노트

[LeetCode/Easy] 1757. Recyclable and Low Fat Products 본문

코딩테스트 풀이/SQL

[LeetCode/Easy] 1757. Recyclable and Low Fat Products

무지맘 2023. 4. 17. 01:34

1. Input

1) Table: Products (pk: product_id)

- low_fats = ('Y', 'N'): 저지방이면 Y

- recyclable = ('Y', 'N'): 재활용 가능하면 Y

 

2. Output

1) 저지방이고 재활용 가능한 상품의 id를 출력

- 출력 순서는 상관 없다.

 

3. Example

 

4. Code

1) 첫 코드(2023/04/17)

select product_id
from (
    select product_id, low_fats
    from Products
    where recyclable='Y'
) p
where low_fats='Y';

2) 서브 쿼리를 없애본 코드(2023/04/17)

select product_id
from Products
where low_fats='Y' and recyclable='Y';

- 두 코드의 큰 차이가 없는듯하다. 그냥 2번처럼 쓰는게 더 좋을 듯