PostgreSQL IF Statement (original) (raw)

Last Updated : 15 Jul, 2025

**PostgreSQL IF statement is an essential tool for implementing conditional logic within SQL queries and stored procedures. It allows developers to execute different actions based on specific conditions and enhances the flexibility of database operations.

In this article, we will explore various **PostgreSQL conditional statements by focusing on practical **PostgreSQL IF examples that explain how to effectively utilize this powerful feature.

Introduction to PostgreSQL IF Statement

The **IF statement in **PostgreSQL is a conditional statement that allows us to execute certain actions based on whether a specified condition is **true or **false. It is widely used in **stored procedures and functions to control the flow of execution.

By using the IF statement, we can enhance the logic of our **SQL queries and ensure that specific tasks are performed only under certain conditions.

Why Use IF Statements in PostgreSQL?

**Syntax of PostgreSQL IF Statement

The basic syntax of the IF statement in PostgreSQL is as follows:

IF condition THEN
-- statements to execute if condition is true
ELSE
-- statements to execute if condition is false
END IF;

**Explanation:

Examples of Using IF Statement in PostgreSQL

Example 1: Simple IF Statement

Let's create a simple function to explain the use of the IF statement. This function will check if a number is **positive, negative, or zero.

CREATE OR REPLACE FUNCTION check_number(num INT)
RETURNS TEXT AS DECLAREresultTEXT;BEGINIFnum>0THENresult:=′Positive′;ELSIFnum<0THENresult:=′Negative′;ELSEresult:=′Zero′;ENDIF;RETURNresult;END;DECLARE
result TEXT;
BEGIN
IF num > 0 THEN
result := 'Positive';
ELSIF num < 0 THEN
result := 'Negative';
ELSE
result := 'Zero';
END IF;
RETURN result;
END;
DECLAREresultTEXT;BEGINIFnum>0THENresult:=Positive;ELSIFnum<0THENresult:=Negative;ELSEresult:=Zero;ENDIF;RETURNresult;END;
LANGUAGE plpgsql;

**Output:

You can call this function with different values to see the results:

SELECT check_number(10); -- Output: Positive
SELECT check_number(-5); -- Output: Negative
SELECT check_number(0); -- Output: Zero

Example 2: IF Statement in a SQL Query

You can also use the IF statement within a SQL query, particularly when working with the **CASE statement, which provides a more easy way to handle multiple conditions.

SELECT
employee_id,
salary,
CASE
WHEN salary > 50000 THEN 'High'
WHEN salary BETWEEN 30000 AND 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;

**Output:

employee_id salary salary_category
1 60000 High
2 40000 Medium
3 25000 Low

Example 3: Nested IF Statements

You can also nest IF statements for more complex logic. Here’s an example:

CREATE OR REPLACE FUNCTION categorize_score(score INT)
RETURNS TEXT AS DECLAREcategoryTEXT;BEGINIFscore>=90THENcategory:=′A′;ELSIFscore>=80THENcategory:=′B′;ELSIFscore>=70THENcategory:=′C′;ELSEcategory:=′D′;ENDIF;RETURNcategory;END;DECLARE
category TEXT;
BEGIN
IF score >= 90 THEN
category := 'A';
ELSIF score >= 80 THEN
category := 'B';
ELSIF score >= 70 THEN
category := 'C';
ELSE
category := 'D';
END IF;
RETURN category;
END;
DECLAREcategoryTEXT;BEGINIFscore>=90THENcategory:=A;ELSIFscore>=80THENcategory:=B;ELSIFscore>=70THENcategory:=C;ELSEcategory:=D;ENDIF;RETURNcategory;END;
LANGUAGE plpgsql;

**Output:

You can test this function as follows:

SELECT categorize_score(85); -- Output: B
SELECT categorize_score(92); -- Output: A
SELECT categorize_score(67); -- Output: D

Best Practices for Using IF Statements

Conclusion

In summary, the **PostgreSQL IF statement is a important element for introducing conditional logic into your SQL queries and functions. By mastering **PostgreSQL conditional statements, you can customize your database operations to handle various scenarios efficiently. The **PostgreSQL IF examples presented in this article explain the versatility of using IF in PostgreSQL and allowing you to build more complex and responsive applications.