Conditional Statements: IF-THEN, IF-ELSE, multiple conditions, nested IF, CASE

Conditional statements allow your database programs to inspect data dynamically and choose different execution paths on the fly.

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Choosing a route when critical errors happen

Imagine a customer logs a new ticket on TicketDesk at midnight. If the priority is Critical, you need to text a senior manager immediately. If the priority is High, sending an email notification is enough. If it is Low, you can just leave it in the database queue until morning. Standard SQL updates every single row uniformly, but real life requires making choices. How can your database code look at data dynamically and choose exactly one operational path out of many options?

Theory

The Railway Track Switch

Think of a basic SQL statement as a straight train track running from one station to another without stopping. Every row travels down the exact same track. Conditional control in PL/SQL is like a railway track switch. As the train approaches, a sensor checks its destination card. If the card says Express, the switch flips to the left track. If it says Local, the switch stays right. The data itself dictates which route the system takes.

Theory

Conditional Statements in Oracle PL/SQL

In the Oracle dialect, conditional statements allow you to control program execution flow based on runtime values. Instead of executing code line by line blindly, the engine evaluates a logical condition that results in true, false, or null. PL/SQL provides two main structural constructs for decision making: the IF statement family (including IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF) and the CASE statement. These constructs let you handle multi-path branches cleanly.

At a glance

Comparison of decision control options within Oracle procedural code

Construct TypeBest Use CaseKey Structural Keyword
IF-THENExecuting a block of code only when a single condition is metTHEN
IF-THEN-ELSEChoosing between two mutually exclusive alternative pathwaysELSE
IF-THEN-ELSIFTesting multiple sequential conditions without deep nestingELSIF
CASE StatementMatching an expression against many specific discrete valuesWHEN

Practical

Implementing Priority Routing Logic

-- Enable console printing in Oracle SQL*Plus
SET SERVEROUTPUT ON;

DECLARE
  v_priority VARCHAR2(20) := 'High';
  v_action VARCHAR2(100);
BEGIN
  -- Evaluate conditions sequentially using ELSIF
  IF v_priority = 'Critical' THEN
    v_action := 'Send SMS alert to Senior Engineer';
  ELSIF v_priority = 'High' THEN
    v_action := 'Assign to Senior Agent and email group';
  ELSE
    v_action := 'Route to general helpdesk queue';
  END IF;
  
  DBMS_OUTPUT.PUT_LINE('Action taken: ' || v_action);
END;
/

Copy and open Oracle FreeSQL
Oracle FreeSQL is a free online editor for Oracle SQL. The code is copied first: paste it there and run it.

Quiz

Look closely at the spelling of the multi-condition keyword used in the intermediate branches of an Oracle PL/SQL IF block. Which option represents the correct syntax?

  1. ELSEIF
  2. ELSIF
  3. ELSE IF
  4. IFELSE
Show the answer

ELSIF

Oracle PL/SQL uses the explicit keyword ELSIF without an E after the S and written as a single word. Common mistakes include writing it as ELSEIF or splitting it into two words like ELSE IF, both of which trigger an immediate compilation syntax failure in Oracle database environments.

Think first

Mental Challenge: Using CASE for Value Matching

Suppose you want to replace an IF-THEN-ELSIF structure with a simple CASE statement to print labels based on status values: 'Open', 'In Progress', or 'Closed'. How would you lay out the CASE statement inside your mind before tapping?

Show the answer

You would use the CASE selector construct:

CASE v_status

WHEN 'Open' THEN DBMS_OUTPUT.PUT_LINE('New ticket');

WHEN 'In Progress' THEN DBMS_OUTPUT.PUT_LINE('Work started');

WHEN 'Closed' THEN DBMS_OUTPUT.PUT_LINE('Job completed');

ELSE DBMS_OUTPUT.PUT_LINE('Unknown status');

END CASE;

Note that unlike a regular SQL query CASE expression, a procedural PL/SQL CASE statement terminates with END CASE; and a trailing semicolon.

Watch out

The Common ELSIF and END IF Spelling Errors

The most frequent mistake Indian BCA students make during university laboratory practical exams is misspelling keywords. Writing ELSEIF with an 'E' will cause Oracle to reject your entire block. Another classic trap is omitting the trailing semicolon after END IF; or writing it as ENDIF as a single word. Every separate IF construct requires an explicit END IF; spacing with a terminating semicolon to tell the parser that the conditional zone is finished.

Theory

Real-World Priority Escalation Systems

In major tech environments like product integrity operations or enterprise ticket routing tools, this exact conditional logic runs millions of times daily. When a ticket bypasses SLA limits, conditional rules instantly update variables and escalate assignments. You will build upon this foundational decision structure next semester in your Sem 3 SQLite mobile database programming course, where you will use logical branching inside smartphone local sync routines.

Summary

Key takeaways

  • Conditional statements dynamically select a specific operational path based on evaluated logical logic.
  • The fundamental Oracle structures include the IF-THEN family and discrete CASE statement branches.
  • Watch out for the keyword spelling: it is always written as ELSIF without the character E.
  • Every procedural IF block container must officially terminate using the two words END IF; statement.
  • CASE structures are excellent alternatives for clean code when matching one expression against scalar options.
  • Memory hook: Check with IF, add paths with ELSIF, fallback with ELSE, and close with END IF!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from PL/SQL and Conditional Statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Conditional Statements: IF-THEN, IF-ELSE, multiple conditions, nested IF, CASE · Mastering SQL - PL/SQL (SEC-02 option A) · Gri-Learn