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.
Syntax:
Format 1:
IF condition THEN
-- code or statements to be executed
END IF;
Format 2:
IF condition THEN
-- code or statements to be executed
ELSE
-- code or statements to be executed
END IF;
Format 3:
IF condition THEN
-- code or statements to be executed
ELSE IF THEN
-- code or statements to be executed
ELSE
-- code or statements to be executed
END IF;
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';
ELSE
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:
DO $$
DECLARE
variable1 integer := 98;
variable2 integer := 100;
BEGIN
IF variable1 > variable2 THEN
RAISE NOTICE 'variable1 is greater than b';
ELSIF variable1 < variable2 THEN
RAISE NOTICE 'variable1 is less than b';
ELSE
RAISE NOTICE 'variable1 is equal to b';
END IF;
END $$;
First, we will check the condition if variable1 is greater than variable2. If this condition is false, we will move to the else-if block to check if variable1 is less than variable2. If both conditions are false, we will raise a message with a notice saying that both variables are equal.
Output:
It will be as follows when variable 1 and variable 2 have 98 and 100 values, respectively.
Example #4
Let us now see an example where these conditions are most often used, that is, using query results for condition specification. We have one table in my database named educational_platforms whose description is as follows using the below command-
Code:
\d educational_platforms;
Output:
You can create one if it’s not there. Let us see the contents of the table.
Code:
select * from educational_platforms;
Output:
If an entry with psql technology exists, we have to update the client count of that entry to 100; else, insert the record with psql technology. Here are the statements that will do so.
Code:
DO $$
BEGIN
IF EXISTS (SELECT FROM educational_platforms WHERE technology='psql') THEN
UPDATE educational_platforms SET clientcount=101 WHERE technology='psql';
ELSE
insert into educational_platforms values (3,'psql',80,101,'RDBMS','');
END IF;
END $$;
Output:
Let us recheck the contents of the table.
Code:
select * from educational_platforms;
Output:
Conclusion
We can use if, if-else, and nested if-else statements in our PostgreSQL database at our convenience to achieve our use-case requirement. We can use these conditional statements in functions, stored procedures, variables, query statements, or inside the loop statements wherever we want to check the conditions and execute some statements based on the output of that condition.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL if else” was beneficial to you. You can view EDUCBA’s recommended articles for more information.