DECODE()
is Oracle’s version of a conditional IF-THEN-ELSE
logic, similar to a CASE
statement.
It compares an expression to multiple values and returns the matching result.
DECODE(expression, search1, result1, search2, result2, ..., default);
expression
: the value to compare.search
: the value you're checking against.result
: what to return if the search value matches.default
: what to return if no match is found (optional).
SELECT emp_id,
first_name,
salary,
DECODE(emp_id,
'453', 'divya',
'123', 'prabhu',
'461', 'ayyappa',
'unknown') AS REVISED_NAME
FROM sample_emp;
Explanation:
This query checks the emp_id
value and returns a custom alias name using DECODE()
.
🔄 How it works:
If emp_id = '453'
→ return 'divya'
If emp_id = '123'
→ return 'prabhu'
If emp_id = '461'
→ return 'ayyappa'
Else → return 'unknown'
Use Cases of DECODE()
:-
Replace codes with readable labels
- Data transformation in reports
- Simplify
IF-ELSE
logic in SELECT