Updated May 8, 2023
Introduction to PostgreSQL if else
Using the queries will give control of the database and allow the user to manipulate it effectively and strongly in any SQL or database language. Some statements help the user have better control over the queries and help in decision-making based on PostgreSQL conditions, called the control statements. One of the most crucial and powerful out of all of them is the if-else statement. This statement allows us to execute certain codes only when some condition is fulfilled. If not, then some other code might be executed.
We can use only if the statement if we want to execute certain statements on fulfillment of some condition, or we can use the second format where the control when the condition evaluates to true and when it evaluates to false is given can execute our statements accordingly. Nesting, if inside or else of another, is also possible. This completely customizable structure can be used as per our convenience and requirement.
Examples of PostgreSQL if else
Given below are the examples:
Example #1
In our first example, we will consider two variables. Then, using the format 1 mentioned above, we will write the statements so that when variable 1 is less than, greater than, or equal to variable2, an appropriate message with the notice will be raised after they are compared using comparing operators. Here, three separate if statements will be used to achieve our use-case requirement.
Code:
DO $$
DECLARE
variable1 integer := 55;
variable2 integer := 75;
BEGIN
IF variable1 > variable2 THEN
RAISE NOTICE 'variable1 is greater than variable2 ';
END IF;
IF variable1 < variable2 THEN
RAISE NOTICE 'variable1 is less than variable2 ';
END IF;
IF variable1 = variable2 THEN
RAISE NOTICE 'variable1 is exactly equal to variable2 ';
END IF;
END $$;
Output:
As you can see, because 55 is less than 75, the message “variable1 is less than variable2” was displayed using only a simple if statement.
Example #2
Let us now consider the same example as example 1 but use format2 to perform the operations. Format 2 contains a simple if, and if the condition evaluates to false, the else block will be executed. Considering two variables, variable1, and variable2, we will compare if variable 1 is greater than variable2 if the condition evaluates to true, then notice saying variable1 is greater than variable2 will be raised; if not, statements in the else block will get executed, and appropriate notice will be raised over there.
Code:
DO $$
DECLARE
variable1 integer := 49;
variable2 integer := 50;
BEGIN
IF variable1 > variable2 THEN
RAISE NOTICE 'variable1 is greater than variable2';
RAISE NOTICE 'variable1 is not greater than variable2';
END IF;
END $$;
Output:
As we can see here, we can’t check for the equal condition. If both variables are equal, we will display a message saying that “variable1 is not greater than variable2,” indicating that variable1 may be smaller or equal to variable2. We can overcome this issue by using the format3 of if-else, which allows us to nest other if-else or if statements inside the original ones up to any level, as per our convenience.
Example #3
Now, we will use format3 to implement the same use case scenario as of example1 and example2 and apply the most compact and appropriate solution for this use case of number comparison.
Code: