Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Math Functions in PL/SQL
Next article icon

Decision Making in PL/SQL

Last Updated : 14 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PL/SQL (Procedural Language/Structured Query Language) is Oracle’s extension to SQL that allows for procedural programming within databases. It features various conditional statements to control the flow of execution based on specific conditions.

In this article, We will learn about the various PL/SQL Conditional Statements in detail with the help of examples and so on.

PL/SQL Conditional Statements

PL/SQL (Procedural Language/Structured Query Language) is an extension of SQL used in Oracle databases to write procedural code. It includes various conditional statements that allow developers to execute different blocks of code based on specific conditions. Decision-making statements in programming languages decide the direction of the flow of program execution. Conditional Statements available in PL/SQL are defined below:

  1. IF THEN
  2. IF THEN ELSE
  3. NESTED-IF-THEN
  4. IF THEN ELSIF-THEN-ELSE Ladder

1. IF THEN

if then the statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if condition then
-- do something
end if;

Here, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. if and endif consider as a block here.

Example:

SQL
declare -- declare the values here  begin  if condition then dbms_output.put_line('output');  end if;  dbms_output.put_line('output2'); end; 

 

SQL
-- pl/sql program to illustrate If statement declare num1 number:= 10; num2 number:= 20;  begin  if num1 > num2 then dbms_output.put_line('num1 small'); end if;  dbms_output.put_line('I am Not in if');  end; 

As the condition present in the if statement is false. So, the block below the if statement is not executed. Output:

I am Not in if

2. IF THEN ELSE

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:-

if (condition) then
-- Executes this block if
-- condition is true
else
-- Executes this block if
-- condition is false

 

Example:- 

SQL
-- pl/sql program to illustrate If else statement declare num1 number:= 10; num2 number:= 20;  begin  if num1 < num2 then dbms_output.put_line('i am in if block');  ELSE dbms_output.put_line('i am in else Block'); end if;  dbms_output.put_line('i am not in if or else Block'); end; 

Output:-

i'm in if Block
i'm not in if and not in else Block

The block of code following the else statement is executed as the condition present in the if statement is false after calling the statement which is not in block(without spaces).

3. NESTED-IF-THEN

A nested if-then is an if statement that is the target of another if statement. Nested if-then statements mean an if statement inside another if statement. Yes, PL/SQL allows us to nest if statements within if-then statements. i.e, we can place an if then statement inside another if then statement. Syntax:-

if (condition1) then
-- Executes when condition1 is true
if (condition2) then
-- Executes when condition2 is true
end if;
end if;

 

SQL
-- pl/sql program to illustrate nested If statement declare num1 number:= 10; num2 number:= 20; num3 number:= 20;  begin if num1 < num2 then dbms_output.put_line('num1 small num2');    if num1 < num3 then     dbms_output.put_line('num1 small num3 also');   end if;  end if;  dbms_output.put_line('after end if'); end; 

Output:-

num1 small num2
num1 small num3 also
after end if

4. IF THEN ELSIF-THEN-ELSE Ladder

Here, a user can decide among multiple options. The if then statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. Syntax:-

if (condition) then
--statement
elsif (condition) then
--statement
.
.
else
--statement
endif

Flow Chart:-

 Example:- 

SQL
-- pl/sql program to illustrate if-then-elif-then-else ladder declare num1 number:= 10; num2 number:= 20;  begin  if num1 < num2 then dbms_output.put_line('num1 small');  ELSEIF num1 = num2 then dbms_output.put_line('both equal');  ELSE dbms_output.put_line('num2 greater'); end if;  dbms_output.put_line('after end if'); end; 

Output:-

num1 small
after end if

Conclusion

PL/SQL conditional statements are essential for effective procedural programming in Oracle databases. The IF THEN and IF THEN ELSE statements provide straightforward decision-making, while NESTED-IF-THEN supports complex nested logic. The IF THEN ELSIF-THEN-ELSE ladder allows handling multiple conditions in a structured manner.



Next Article
Math Functions in PL/SQL
author
devanshuagarwal
Improve
Article Tags :
  • Databases
  • DBMS
  • SQL
  • SQL-PL/SQL

Similar Reads

  • Math Functions in PL/SQL
    In PL/SQL, mathematical functions play a important role in performing calculations and manipulating numeric data. These functions allow us to execute a wide range of mathematical operations from basic arithmetic to complex computations within our PL/SQL code. In this article, we will learn about Mat
    4 min read
  • PL/SQL MIN() Function
    PL/SQL means Procedural Language / relational Structured Query Language, the extended language of Oracle. It is primarily used to manage and manipulate databases. One of the most frequently utilized SQL functions is the MIN() function. This powerful aggregate function is essential for finding the sm
    6 min read
  • PL/SQL MAX() Function
    The PL/SQL MAX() function is an essential aggregate function in Oracle databases, enabling users to efficiently determine the largest value in a dataset. Whether working with numerical data, dates, or strings, the MAX() function is flexible and widely applicable. In this article, we will provide a d
    4 min read
  • PL/SQL Exception Handling Division by Zero
    Handling runtime errors is crucial for database applications, and one common issue is the "divide by zero" error in PL/SQL. This error occurs when dividing any number by zero, resulting in undefined behavior and potential crashes. Effective handling involves identifying possible causes, implementing
    4 min read
  • PL/SQL HAVING Clause
    The PL/SQL HAVING clause is a powerful tool used in SQL for filtering records in groups defined by the GROUP BY clause. While the WHERE clause filters individual rows, the HAVING clause filters groups based on aggregate functions like SUM, COUNT, MIN, and MAX. This clause is essential when we want t
    5 min read
  • Cursors in PL/SQL
    A Cursor in PL/SQL is a pointer to a context area that stores the result set of a query. PL/SQL CursorsThe cursor is used to retrieve data one row at a time from the results set, unlike other SQL commands that operate on all rows at once. Cursors update table records in a singleton or row-by-row man
    3 min read
  • NOT IN vs NOT EXISTS in PL/SQL
    PL/SQL is a Procedural Language/Structured Query Language. It allows developers to create robust, modular, and reusable code for implementing data manipulation, and transaction control in databases. Two crucial operators in PL/SQL are NOT IN and NOT EXISTS, which are often used to filter data based
    5 min read
  • Index in PL/SQL
    PL/SQL, Oracle's extension to SQL, combines SQL with procedural programming features like loops, conditionals, and exception handling. It enables developers to create stored procedures, functions, triggers, and other database applications. As a block-structured language, PL/SQL allows seamless integ
    5 min read
  • Blocks in PL/SQL
    In PL/SQL, All statements are classified into units that is called Blocks. PL/SQL blocks can include variables, SQL statements, loops, constants, conditional statements and exception handling. Blocks can also build a function or a procedure or a package. The Declaration section: Code block start wit
    3 min read
  • PL/SQL LIMIT Clause
    The LIMIT clause in PL/SQL is a powerful tool designed to manage the amount of data retrieved from a query, making it particularly useful for handling large datasets. By specifying the maximum number of rows to be fetched, the LIMIT clause helps in optimizing both performance and memory usage. In th
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences