Open In App

PL/SQL Operators

Last Updated : 05 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The PL/SQL language offers various operators for data manipulation and logical processing. There are several types of these operators which include arithmetic operators, relational operators, comparison operators, and logical operators. In this guide, we will learn about the various PL/SQL operators with the help of examples and so on.

PL/SQL Operators

PL/SQL operators are used to operate on variables, constants, or expressions in PL/SQL blocks. They enable us to process data through arithmetic, relational or logical operations that is calculations, comparisons, or logical decisions. Operators are essential in PL/SQL for performing calculations and making decisions, from simple arithmetic to complex logic.

Types of PL/SQL Operators

PL/SQL offers several types of operators, each serving a unique purpose:

1. Arithmetic Operators

Arithmetic operators in PL/SQL are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.

Operator

Description

Example

+

Addition

10+5

-

Subtraction

10-5

*

Multiplication

10*5

/

Division

10/5

MOD

Modulus

1MOD(10,3)

Example:

DECLARE
    num1 NUMBER := 20;
    num2 NUMBER := 4;
    result NUMBER;
BEGIN
    result := num1 + num2;
    DBMS_OUTPUT.PUT_LINE('Addition Result: ' || result);
    result := num1 - num2;
    DBMS_OUTPUT.PUT_LINE('Subtraction Result: ' || result);
    result := num1 * num2;
    DBMS_OUTPUT.PUT_LINE('Multiplication Result: ' || result);
    result := num1 / num2;
    DBMS_OUTPUT.PUT_LINE('Division Result: ' || result);
    result := MOD(num1, num2);
    DBMS_OUTPUT.PUT_LINE('Modulus Result: ' || result);
END;

2. Relational Operators

Comparison operators compare two values and return a Boolean result (TRUE, FALSE, or NULL). These operators are commonly used in conditional statements such as IF and LOOP.

Operator

Description

Example

=

Equal to

x=y

!= or <>

Not Equal to

x!=y or x<>y

>

Greater than

x>y

<

Less than

x<y

>=

Greater than or equal to

x>=y

<=

Less than or equal to

x<=y

Example:

DECLARE
    age1 NUMBER := 25;
    age2 NUMBER := 30;
BEGIN
    IF age1 <> age2 THEN
        DBMS_OUTPUT.PUT_LINE('Ages are not equal.');
    END IF;
    IF age1 < age2 THEN
        DBMS_OUTPUT.PUT_LINE('Age1 is less than Age2.');
    END IF;
END;

3. Logical Operators

Logical operators are used to combine or negate conditions, and they evaluate to a Boolean value. These operators are often used in IF, CASE, and LOOP statements.

Operator

Description

Examples

AND

Returns TRUE if both conditions are TRUE

x > 5 AND y < 10

OR

Returns TRUE if at least one condition is TRUE

x > 5 OR y < 10

NOT

Negates a condition

NOT (x > 5)

Example:

DECLARE
    score1 NUMBER := 80;
    score2 NUMBER := 70;
BEGIN
    IF score1 >= 75 AND score2 >= 75 THEN
        DBMS_OUTPUT.PUT_LINE('Both scores are above average.');
    ELSIF score1 >= 75 OR score2 >= 75 THEN
        DBMS_OUTPUT.PUT_LINE('At least one score is above average.');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Both scores are below average.');
    END IF;
END;

4. Comparison Operators

These operators are used to compare values and return TRUE or FALSE based on the condition.

Operator

Description

Examples

BETWEEN

Checks if a value is within a range

x BETWEEN 10 AND 20

LIKE

Matches a value against a pattern

name LIKE 'A%' (names starting with 'A')

IN

Checks if a value exists in a list

x IN (1, 2, 3)

IS NULL

Checks if a value is NULL

x IS NULL

Example:

DECLARE
student_name VARCHAR2(50) := 'Alice';
student_age NUMBER := 19;
BEGIN
IF student_name LIKE 'A%' THEN
DBMS_OUTPUT.PUT_LINE('Student name starts with "A".');
END IF;

IF student_age BETWEEN 18 AND 22 THEN
DBMS_OUTPUT.PUT_LINE('Student is in college age range.');
END IF;
END;

Conclusion

PL/SQL operators are powerful tools for building conditions, performing calculations, and manipulating data effectively. Handling arithmetic operations, logical decisions, or complex comparisons, understanding these operators is essential for efficient PL/SQL programming. Mastery of these operators can significantly improve the robustness and flexibility of your database applications.


Next Article
Article Tags :

Similar Reads