[PRO] SQL 고득점 Kit > IS NULL

vvo_ter·2023년 11월 24일
0

SQL 고득점 Kit

목록 보기
18/24
post-custom-banner

1. 경기도에 위치한 식품창고 목록 출력하기 - Lv1


👉 제출 코드

방법 1: CASE WHEN 사용

SELECT WAREHOUSE_ID, WAREHOUSE_NAME, ADDRESS,
    CASE
        WHEN FREEZER_YN IS NULL THEN 'N'
        ELSE FREEZER_YN
    END AS FREEZER_YN
FROM FOOD_WAREHOUSE
WHERE ADDRESS LIKE '경기도%'
ORDER BY WAREHOUSE_ID

방법 2: IF 함수 사용

SELECT WAREHOUSE_ID, WAREHOUSE_NAME, ADDRESS,
    --- IF(조건문, 참일 때 값, 거짓일 때 값)
    IF(FREEZER_YN IS NULL, 'N', FREEZER_YN) FREEZER_YN
FROM FOOD_WAREHOUSE
WHERE ADDRESS LIKE '경기도%'
ORDER BY WAREHOUSE_ID

방법 2: IFNULL 함수 사용

SELECT WAREHOUSE_ID, WAREHOUSE_NAME, ADDRESS,
	--- IFNULL(칼럼, NULL일 때 값)
    IFNULL(FREEZER_YN, 'N') FREEZER_YN
FROM FOOD_WAREHOUSE
WHERE ADDRESS LIKE '경기도%'
ORDER BY WAREHOUSE_ID

2. NULL 처리하기 - Lv2


👉 제출 코드

--- CASE WHEN 사용
SELECT ANIMAL_TYPE, 
    CASE WHEN NAME IS NULL THEN 'No name'
         ELSE NAME
    END NAME,
    SEX_UPON_INTAKE
FROM ANIMAL_INS
--- IF 사용
SELECT ANIMAL_TYPE, 
    IF(NAME IS NULL, 'No name', NAME) NAME,
    SEX_UPON_INTAKE
FROM ANIMAL_INS
--- IFNULL 사용
SELECT ANIMAL_TYPE, 
    IFNULL(NAME, 'No name') NAME,
    SEX_UPON_INTAKE
FROM ANIMAL_INS
profile
's Coding Memory
post-custom-banner

0개의 댓글