Explain the 'CASE' expression in Oracle SQL statement

In Oracle SQL, the CASE expression is used to add conditional logic within a SQL statement. It allows you to perform different actions based on different conditions. The basic syntax of the CASE expression is:

Here's an example to demonstrate how to use the CASE expression:

Suppose we have a table named employees with columns employee_id, first_name, last_name, and salary. We want to create a new column called salary_category that categorizes employees based on their salary into three categories: "Low", "Medium", and "High".

In this example:

  • If the salary is less than 30,000, the category will be "Low".
  • If the salary is greater than or equal to 30,000 but less than 60,000, the category will be "Medium".
  • If the salary is 60,000 or above, the category will be "High".

Output might look something like this:

This query creates a new column salary_category in the result set, where each employee's salary is categorized based on the specified conditions using the CASE expression.

No comments:

Post a Comment