PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The NULLIF()
function in PostgreSQL is a conditional expression that returns NULL
if two expressions are equal; otherwise, it returns the first expression. It can be especially useful for preventing division-by-zero errors or to transform specific values to NULL.
NULLIF(expression1, expression2)
expression1
equals expression2
, the function returns NULL
.expression1
does not equal expression2
, the function returns expression1
.Preventing Division-by-Zero Errors:
Suppose you want to divide two columns: numerator
and denominator
. To prevent division by zero, you can use the NULLIF()
function:
SELECT numerator / NULLIF(denominator, 0) AS result FROM table_name;
If denominator
is 0, the NULLIF()
function returns NULL
, and the division result is also NULL
(which avoids a division-by-zero error).
Transforming Specific Values to NULL:
Suppose you have a table of products with a column price
, and you've previously used a value of -1
to represent items with unknown prices. If you want to convert these -1
values to NULL
:
SELECT NULLIF(price, -1) AS adjusted_price FROM products;
Any row with a price
of -1
will have adjusted_price
set to NULL
, and all other prices remain unchanged.
Difference from CASE: While you can achieve similar results using the CASE
statement, NULLIF()
offers a more concise way to handle comparisons that result in NULL
values. For example, the following two expressions are equivalent:
NULLIF(expression1, expression2)
CASE WHEN expression1 = expression2 THEN NULL ELSE expression1 END
The NULLIF()
function is essentially a shorthand for this specific CASE
construct.
Data Types: The data types of expression1
and expression2
must be compatible.
In summary, the NULLIF()
function in PostgreSQL is a conditional function that returns NULL
when two expressions are equal. It offers a concise way to handle specific scenarios where you'd want to transform values or prevent errors in calculations.
How to use NULLIF() function in PostgreSQL:
NULLIF()
function returns NULL if two expressions are equal; otherwise, it returns the first expression.SELECT NULLIF(column1, column2) AS result FROM your_table;
Comparing values with NULLIF() in PostgreSQL:
NULLIF()
to handle cases where two values are equal.SELECT column1, NULLIF(column1, 0) AS result FROM your_table;
Using NULLIF() to handle NULL values in PostgreSQL:
NULLIF()
to replace NULL values with a default value.SELECT column1, NULLIF(column2, 'default_value') AS result FROM your_table;
NULLIF() vs. CASE statement in PostgreSQL:
NULLIF()
with the CASE
statement for conditional expressions.SELECT column1, NULLIF(column2, 0) AS result_nullif, CASE WHEN column2 = 0 THEN NULL ELSE column2 END AS result_case FROM your_table;
Using NULLIF() with different data types in PostgreSQL:
NULLIF()
to handle comparisons between different data types.SELECT column1, NULLIF(column2::text, 'default_value') AS result FROM your_table;
NULLIF() in conjunction with other functions in PostgreSQL:
NULLIF()
with other functions for more complex expressions.SELECT column1, NULLIF(UPPER(column2), 'DEFAULT') AS result FROM your_table;
Handling NULLIF() with aggregate functions in PostgreSQL:
NULLIF()
with aggregate functions to handle NULL values during calculations.SELECT AVG(NULLIF(column1, 0)) AS average_value FROM your_table;
NULLIF() and arithmetic operations in PostgreSQL:
NULLIF()
in arithmetic operations to prevent division by zero errors.SELECT column1, column2, column1 / NULLIF(column2, 0) AS result FROM your_table;
Using NULLIF() with SELECT statements in PostgreSQL:
NULLIF()
in SELECT
statements to customize result sets.SELECT column1, NULLIF(column2, column3) AS result FROM your_table;
NULLIF() in WHERE and HAVING clauses in PostgreSQL:
NULLIF()
in WHERE
and HAVING
clauses for conditional filtering.SELECT column1, column2 FROM your_table WHERE NULLIF(column2, 0) IS NOT NULL;
NULLIF() with JOIN operations in PostgreSQL:
NULLIF()
in JOIN operations to handle NULL values in join conditions.SELECT t1.column1, t2.column2 FROM table1 t1 JOIN table2 t2 ON t1.column1 = NULLIF(t2.column2, 0);
Using NULLIF() with subqueries in PostgreSQL:
NULLIF()
into subqueries for more advanced calculations.SELECT column1, (SELECT NULLIF(column2, 0) FROM other_table WHERE condition) AS result FROM your_table;
NULLIF() and foreign key relationships in PostgreSQL:
NULLIF()
in foreign key relationships to handle NULL values in references.CREATE TABLE parent_table ( parent_id INT PRIMARY KEY ); CREATE TABLE child_table ( child_id INT PRIMARY KEY, parent_id INT REFERENCES parent_table(parent_id) ON DELETE SET NULL );
NULLIF() and unique constraints in PostgreSQL:
NULLIF()
with unique constraints to ensure uniqueness.CREATE TABLE your_table ( column1 INT, column2 INT UNIQUE NULLIF(column1, 0) );
NULLIF() with GROUP BY and ORDER BY in PostgreSQL:
NULLIF()
in conjunction with GROUP BY
and ORDER BY
for grouped calculations.SELECT column1, AVG(NULLIF(column2, 0)) AS average_value FROM your_table GROUP BY column1 ORDER BY column1;