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 StatementThe 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:
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 $$
DECLARE
result TEXT;
BEGIN
IF num > 0 THEN
result := 'Positive';
ELSIF num < 0 THEN
result := 'Negative';
ELSE
result := 'Zero';
END IF;
RETURN result;
END;
$$ LANGUAGE plpgsql;
Output:
You can call this function with different values to see the results:
SELECT check_number(10); -- Output: PositiveExample 2: IF Statement in a SQL Query
SELECT check_number(-5); -- Output: Negative
SELECT check_number(0); -- Output: Zero
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 StatementsYou can also nest IF statements for more complex logic. Here’s an example:
CREATE OR REPLACE FUNCTION categorize_score(score INT)
RETURNS TEXT AS $$
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;
$$ LANGUAGE plpgsql;
Output:
You can test this function as follows:
SELECT categorize_score(85); -- Output: BBest Practices for Using IF Statements
SELECT categorize_score(92); -- Output: A
SELECT categorize_score(67); -- Output: D
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4