Sub topic 1
Why Queries Need Conditional Logic
SQL usually describes which rows to retrieve and which columns to show. Many reports also need a decision: label a department, classify a salary, or choose a message based on a value. Conditional logic lets one query make those decisions while it reads the data.
In Oracle SQL, the main tool for conditional logic is the CASE expression. An expression produces a value, so CASE can appear anywhere a calculated value is allowed. Oracle also provides DECODE, a compact function for comparing one expression with several possible values.
Prerequisites
This chapter assumes that you can write basic SELECT statements with WHERE and ORDER BY. You should know the common Oracle data types NUMBER, VARCHAR2, and DATE. You should also understand that NULL means an unknown or missing value, and that GROUP BY, SUM, and COUNT summarize rows.
The problem conditional logic solves
Suppose an employees table stores department numbers, not department names. A query can return the number directly:
SELECT employee_id, department_id
FROM employees;That result is useful to the database, but less useful to a person reading a report. You can translate each number into a label as part of the query:
SELECT employee_id,
department_id,
CASE department_id
WHEN 10 THEN 'Administration'
WHEN 20 THEN 'Marketing'
WHEN 50 THEN 'Shipping'
ELSE 'Other'
END AS department_label
FROM employees;The query keeps the stored number and adds a readable value. The CASE expression checks department_id, finds a matching branch, and returns the corresponding text. Rows that match none of the listed values receive Other.
Conditional logic can also create groups from numeric data. For example, a report might show a salary band instead of exposing the exact salary:
SELECT employee_id,
salary,
CASE
WHEN salary < 5000 THEN 'Low'
WHEN salary < 10000 THEN 'Medium'
ELSE 'High'
END AS salary_band
FROM employees;This form tests conditions rather than matching one expression against fixed values. The conditions are checked from top to bottom. The first true condition determines the result.
| Reporting need | Conditional result |
|---|---|
| Translate a code | A descriptive label |
| Group a number | A range such as Low or High |
| Handle missing data | A fallback message |
| Sort special values first | A custom numeric sort key |
A working model
Think of a conditional expression as a small decision table inside a query:
The expression does not change the stored table data. It calculates a value for the query result. This makes it useful for presentation, classification, and later operations such as sorting or aggregation.
DECODE addresses the simpler value-matching case. Its shape is an expression followed by search-and-result pairs and an optional default:
SELECT employee_id,
DECODE(department_id,
10, 'Administration',
20, 'Marketing',
'Other') AS department_label
FROM employees;Later chapters examine evaluation order, NULL, data types, and the differences between CASE and DECODE. Those details matter when conditions overlap or data is missing.
Exercise
Why might a report use CASE to show a salary band instead of returning only the salary?
Show answer
CASE converts numeric values into meaningful categories, making the report easier to read and allowing rows to be grouped or counted by category.