SQL에서 NULL
값은 “값이 존재하지 않음”을 의미하며, 연산이나 조건문에서 의도하지 않은 결과를 초래할 수 있습니다. 이를 방지하기 위해 NVL
, NVL2
, COALESCE
같은 함수들이 제공됩니다. 이번 글에서는 각 함수의 사용법과 예제 데이터를 활용한 실습을 정리해보겠습니다.
NVL 함수
NVL
함수는 NULL
값을 특정 값으로 변환하는 함수입니다.
문법
NVL(column, replace_value)
column
:NULL
값을 확인할 열replace_value
:NULL
일 경우 대체할 값
사용 예
아래 employees
테이블이 있다고 가정해보겠습니다.
emp_id | emp_name | salary | bonus |
---|---|---|---|
101 | Alice | 5000 | 500 |
102 | Bob | 6000 | NULL |
103 | Charlie | 7000 | 700 |
104 | David | NULL | NULL |
다음 SQL을 실행하면 NULL
값을 기본값(0)으로 변환할 수 있습니다.
SELECT emp_name, salary, NVL(salary, 0) AS salary_with_default FROM employees;
출력 결과
emp_name | salary | salary_with_default |
---|---|---|
Alice | 5000 | 5000 |
Bob | 6000 | 6000 |
Charlie | 7000 | 7000 |
David | NULL | 0 |
NVL2 함수
NVL2
함수는 NULL
여부에 따라 서로 다른 값을 반환하는 함수입니다.
문법
NVL2(column, value_if_not_null, value_if_null)
column
:NULL
여부를 확인할 열value_if_not_null
:NULL
이 아닐 때 반환할 값value_if_null
:NULL
일 때 반환할 값
사용 예
SELECT emp_name, bonus, NVL2(bonus, '적용됨', '미적용') AS bonus_status FROM employees;
출력 결과
emp_name | bonus | bonus_status |
---|---|---|
Alice | 500 | 적용됨 |
Bob | NULL | 미적용 |
Charlie | 700 | 적용됨 |
David | NULL | 미적용 |
COALESCE 함수
COALESCE
함수는 여러 개의 값 중 첫 번째로 NULL
이 아닌 값을 반환합니다.
문법
COALESCE(value1, value2, ..., valueN)
- 왼쪽부터 차례로
NULL
이 아닌 값을 찾아 반환 - 모든 값이
NULL
이면NULL
반환
사용 예
SELECT emp_name, salary, bonus, COALESCE(salary, bonus, 0) AS first_non_null FROM employees;
출력 결과
emp_name | salary | bonus | first_non_null |
---|---|---|---|
Alice | 5000 | 500 | 5000 |
Bob | 6000 | NULL | 6000 |
Charlie | 7000 | 700 | 7000 |
David | NULL | NULL | 0 |
NVL vs NVL2 vs COALESCE 차이점 비교
함수 | 설명 | 사용 예시 |
---|---|---|
NVL | NULL 값을 단일 값으로 대체 | NVL(salary, 0) → salary가 NULL 이면 0 반환 |
NVL2 | NULL 여부에 따라 서로 다른 값을 반환 | NVL2(bonus, '적용됨', '미적용') |
COALESCE | 여러 값 중 NULL 이 아닌 첫 번째 값 반환 | COALESCE(salary, bonus, 0) |
CASE WHEN을 활용한 결측치 처리
CASE WHEN
문은 보다 복잡한 논리를 적용할 수 있어 유연한 NULL
처리가 가능합니다.
아래와 같은 employees
테이블이 있다고 가정해보겠습니다.
emp_id | emp_name | salary | bonus |
---|---|---|---|
101 | Alice | 5000 | 500 |
102 | Bob | 6000 | NULL |
103 | Charlie | 7000 | 700 |
104 | David | NULL | NULL |
SELECT emp_name,
COALESCE(salary, bonus, 0) AS first_non_null,
CASE
WHEN COALESCE(salary, bonus, 0) = 0 THEN '정보 없음'
ELSE '급여 또는 보너스 있음'
END AS payment_status
FROM employees;
위와 같은 쿼리를 입력하면 아래와 같은 결과를 얻을 수 있습니다. Null 값도 처리하고 추가 가공도 할 수 있게 되는거죠
emp_name | first_non_null | payment_status |
---|---|---|
Alice | 5000 | 급여 또는 보너스 있음 |
Bob | 6000 | 급여 또는 보너스 있음 |
Charlie | 7000 | 급여 또는 보너스 있음 |
David | 0 | 정보 없음 |
SQL에서 NULL
값 처리는 데이터 분석과 보고서 작성에서 매우 중요합니다. NVL
, NVL2
, COALESCE
함수는 각각의 목적에 맞게 활용하면 NULL
값을 효과적으로 다룰 수 있습니다.
참고하면 좋은 글

SQL 데이터 전처리 1 : TRIM, IS NULL, COALESCE
데이터를 가공하다 보면 원하지 않은 데이터를 변경하거나 제거해야 하는 경우가 반드시 존재합니다. 하물며 우리가 모델링한 DB에서 나온 데이터가 아닌 공개 DB나 어디서인가 데이터를 받았을 때는… Read more

SQL 데이터 전처리 2 : 이상치 제거, 표준화, 정규화
“데이터 전처리“는 원시 데이터를 분석이나 모델링에 활용하기 적합하도록 정제하고 가공하는 과정으로, 결측치 처리, 이상치 제거, 변수 스케일링과 같은 단계를 포함하여 데이터의 품질을 향상시키는 작업을 의미합니다…(Min-Max Normalization)는 … Read more
Good post! We will be linking to this particularly great post on our site. Keep up the great writing
Its like you read my mind You appear to know so much about this like you wrote the book in it or something I think that you can do with a few pics to drive the message home a little bit but instead of that this is excellent blog A fantastic read Ill certainly be back
I was suggested this web site by my cousin Im not sure whether this post is written by him as no one else know such detailed about my trouble You are incredible Thanks
Your blog is a testament to your dedication to your craft. Your commitment to excellence is evident in every aspect of your writing. Thank you for being such a positive influence in the online community.
Your writing has a way of resonating with me on a deep level. I appreciate the honesty and authenticity you bring to every post. Thank you for sharing your journey with us.
Attractive section of content I just stumbled upon your blog and in accession capital to assert that I get actually enjoyed account your blog posts Anyway I will be subscribing to your augment and even I achievement you access consistently fast
Your writing has a way of resonating with me on a deep level. It’s clear that you put a lot of thought and effort into each piece, and it certainly doesn’t go unnoticed.
Your writing is a true testament to your expertise and dedication to your craft. I’m continually impressed by the depth of your knowledge and the clarity of your explanations. Keep up the phenomenal work!
This is a fantastic resource. I’m sharing widely.
There is definately a lot to find out about this subject. I like all the points you made