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

PostgreSQL - NULLIF() Function

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.

Syntax:

NULLIF(expression1, expression2)
  • If expression1 equals expression2, the function returns NULL.
  • If expression1 does not equal expression2, the function returns expression1.

Examples:

  1. 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).

  2. 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.

Notes:

  • 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.

  1. How to use NULLIF() function in PostgreSQL:

    • The NULLIF() function returns NULL if two expressions are equal; otherwise, it returns the first expression.
    SELECT NULLIF(column1, column2) AS result
    FROM your_table;
    
  2. Comparing values with NULLIF() in PostgreSQL:

    • Compare values using NULLIF() to handle cases where two values are equal.
    SELECT column1, NULLIF(column1, 0) AS result
    FROM your_table;
    
  3. Using NULLIF() to handle NULL values in PostgreSQL:

    • Use NULLIF() to replace NULL values with a default value.
    SELECT column1, NULLIF(column2, 'default_value') AS result
    FROM your_table;
    
  4. NULLIF() vs. CASE statement in PostgreSQL:

    • Compare 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;
    
  5. Using NULLIF() with different data types in PostgreSQL:

    • Apply NULLIF() to handle comparisons between different data types.
    SELECT column1, NULLIF(column2::text, 'default_value') AS result
    FROM your_table;
    
  6. NULLIF() in conjunction with other functions in PostgreSQL:

    • Combine NULLIF() with other functions for more complex expressions.
    SELECT column1, NULLIF(UPPER(column2), 'DEFAULT') AS result
    FROM your_table;
    
  7. Handling NULLIF() with aggregate functions in PostgreSQL:

    • Use NULLIF() with aggregate functions to handle NULL values during calculations.
    SELECT AVG(NULLIF(column1, 0)) AS average_value
    FROM your_table;
    
  8. NULLIF() and arithmetic operations in PostgreSQL:

    • Employ NULLIF() in arithmetic operations to prevent division by zero errors.
    SELECT column1, column2, column1 / NULLIF(column2, 0) AS result
    FROM your_table;
    
  9. Using NULLIF() with SELECT statements in PostgreSQL:

    • Incorporate NULLIF() in SELECT statements to customize result sets.
    SELECT column1, NULLIF(column2, column3) AS result
    FROM your_table;
    
  10. NULLIF() in WHERE and HAVING clauses in PostgreSQL:

    • Utilize NULLIF() in WHERE and HAVING clauses for conditional filtering.
    SELECT column1, column2
    FROM your_table
    WHERE NULLIF(column2, 0) IS NOT NULL;
    
  11. NULLIF() with JOIN operations in PostgreSQL:

    • Apply 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);
    
  12. Using NULLIF() with subqueries in PostgreSQL:

    • Integrate NULLIF() into subqueries for more advanced calculations.
    SELECT column1, (SELECT NULLIF(column2, 0) FROM other_table WHERE condition) AS result
    FROM your_table;
    
  13. NULLIF() and foreign key relationships in PostgreSQL:

    • Use 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
    );
    
  14. NULLIF() and unique constraints in PostgreSQL:

    • Be cautious when using NULLIF() with unique constraints to ensure uniqueness.
    CREATE TABLE your_table (
       column1 INT,
       column2 INT UNIQUE NULLIF(column1, 0)
    );
    
  15. NULLIF() with GROUP BY and ORDER BY in PostgreSQL:

    • Apply 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;