EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL if else

PostgreSQL if else

Payal Udhani
Article byPayal Udhani
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated May 8, 2023

PostgreSQL if else

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.

ADVERTISEMENT
Popular Course in this category
POSTGRESQL Course Bundle - 5 Courses in 1 | 1 Mock Test

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PostgreSQL if else 1

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:

PostgreSQL if else 2JPG

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.

variable1 and variable2 will have 98 and 100 values

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:

\d educational_platforms;

You can create one if it’s not there. Let us see the contents of the table.

Code:

select * from educational_platforms;

Output:

PostgreSQL if else 5JPG

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:

PostgreSQL if else 6JPG

Let us recheck the contents of the table.

Code:

select * from educational_platforms;

Output:

from educational platform

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.

  1. PostgreSQL Triggers
  2. Guide to PostgreSQL NOW()
  3. PostgreSQL RANDOM | Examples
  4. Guide to PostgreSQL Queries
ADVERTISEMENT
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW