Skip to main content

Command Palette

Search for a command to run...

๐Ÿš€ Using CASE Statements in Oracle SQL for Conditional Logic

Updated
โ€ข2 min read
๐Ÿš€ Using CASE Statements in Oracle SQL for Conditional Logic

As part of my continued learning with Oracle Autonomous Database, I explored how to use CASE statements in SQL. CASE statements allow us to apply conditional logic directly within queries, making data more meaningful.

๐ŸŽฏ Objective

The goal of this activity was to:

  • Understand how CASE statements work

  • Apply conditional logic in SQL queries

  • Categorize data dynamically

๐Ÿ’ป Step 1: Creating the Table

CREATE TABLE employees (
  emp_id NUMBER,
  emp_name VARCHAR2(50),
  salary NUMBER
);

๐Ÿงช Step 2: Inserting Data

INSERT INTO employees VALUES (1, 'Ali', 50000);
INSERT INTO employees VALUES (2, 'Ahmed', 70000);
INSERT INTO employees VALUES (3, 'Sara', 60000);
INSERT INTO employees VALUES (4, 'Zara', 65000);

๐Ÿ” Step 3: Using CASE Statement

SELECT emp_name, salary,
  CASE 
    WHEN salary >= 65000 THEN 'High Salary'
    WHEN salary >= 55000 THEN 'Medium Salary'
    ELSE 'Low Salary'
  END AS salary_category
FROM employees;

This query categorizes employees based on their salary.

๐Ÿ’ก What I Learned

  • How to use CASE for conditional logic

  • Categorizing data dynamically

  • Writing smarter SQL queries

๐Ÿš€ Conclusion

This activity helped me understand how SQL can be used not only for retrieving data but also for applying logic and generating meaningful insights.

3 views