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; these statements are 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 code 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 fulfilment 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. The nesting if inside else of another if is also possible. This is a completely customisable structure and 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 and then, using the format 1 mentioned above; we will write the statements in such a way that when the variable1 is less than or greater than or equal to variable2, then 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, as 55 is less than 75, the message saying variable1 is less than variable2 is displayed. This was achieved only by using the simple if statement.
Example #2
Let us now consider the same example as of example 1 but using the format2 for performing the operations. Format 2 contains a simple if, and if the condition evaluates to false, then 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 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. Even when both the variables are equal, then the message will be displayed saying variable1 is not greater than variable2, which means it can be small or equal. This problem is overcome using the format3 of if-else, where we can do nesting of 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 $$;
Firstly, variable1 is greater than variable2 condition will be checked and if not then it will go to else if block, where less than condition will be checked and if both conditions evaluate to false then message with a notice saying both are equal, will be raised.
Output:
It will be as follows when variable1 and variable2 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 check the contents of the table again.
Code:
select * from educational_platforms;
Output:
Conclusion
We can use if, if-else and nested if-else statements in our PostgreSQL database as per 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, based on the output of that condition, execute some statements.
Recommended Articles
This is a guide to PostgreSQL if-else. Here we discuss the introduction to PostgreSQL if-else along with respective examples for better understanding. You may also have a look at the following articles to learn more –