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 - IS NULL operator

The IS NULL operator in PostgreSQL is used to test for null values. In relational databases, a null value indicates that a specific piece of data is absent or unknown. It is important to note that a null value is different from an empty string or a zero value.

Syntax:

To check if a column or expression yields a NULL value, you use the following syntax:

column_name IS NULL

To check if a column or expression yields a non-NULL value, you can use:

column_name IS NOT NULL

Examples:

  1. Selecting rows with a NULL value in a specific column: Suppose you have a table students and you want to find all students who don't have an assigned classroom.

    SELECT * FROM students
    WHERE classroom IS NULL;
    
  2. Selecting rows without a NULL value in a specific column: To find all students who have an assigned classroom:

    SELECT * FROM students
    WHERE classroom IS NOT NULL;
    
  3. Using with other conditions: If you want to combine the IS NULL or IS NOT NULL check with other conditions, you can use the AND or OR operators.

    SELECT * FROM students
    WHERE classroom IS NOT NULL AND grade = 'A';
    

Important Points:

  • NULL is not the same as an empty string ('') or zero. It represents the absence of data.

  • When comparing using the equality (=) or inequality (<> or !=) operators with a NULL value, the result is always NULL and not a boolean TRUE or FALSE. For instance, the expression (NULL = NULL) does not yield TRUE; instead, it yields NULL. This is why the special IS NULL and IS NOT NULL operators exist for checking null values.

  • In logical operations, PostgreSQL treats NULL as an "unknown" value. This means, for instance, that TRUE AND NULL yields NULL (because the outcome is unknown), while FALSE AND NULL yields FALSE (because the AND operation will be false regardless of the unknown value).

  • Functions and expressions that involve NULL values typically return NULL. For instance, any arithmetic operation with a NULL will yield NULL.

  1. How to use IS NULL in PostgreSQL:

    • The IS NULL condition is used to check if a value is NULL.
    SELECT column_name FROM table_name WHERE column_name IS NULL;
    
  2. Checking for NULL values with IS NULL in PostgreSQL:

    • Use IS NULL to filter rows with NULL values.
    SELECT * FROM example_table WHERE nullable_column IS NULL;
    
  3. Using IS NULL with WHERE clause in PostgreSQL:

    • Apply IS NULL within the WHERE clause.
    SELECT * FROM employees WHERE manager_id IS NULL;
    
  4. IS NULL vs. = NULL in PostgreSQL:

    • Use IS NULL instead of = NULL for NULL comparisons.
    SELECT * FROM table_name WHERE column_name IS NULL;
    
  5. Handling NULL values in comparisons with IS NULL in PostgreSQL:

    • Be cautious when comparing NULL values; use IS NULL for accurate results.
    SELECT * FROM example_table WHERE nullable_column IS NULL OR nullable_column = 42;
    
  6. Using IS NULL in conjunction with other operators in PostgreSQL:

    • Combine IS NULL with other operators for complex conditions.
    SELECT * FROM products WHERE stock_quantity < 10 AND expiration_date IS NULL;
    
  7. Using IS NULL in SELECT statements in PostgreSQL:

    • Include IS NULL in the SELECT statement to display NULL values.
    SELECT column_name, other_column, (column_name IS NULL) AS is_null_check FROM example_table;
    
  8. IS NULL with JOIN operations in PostgreSQL:

    • Utilize IS NULL in JOIN conditions for matching or excluding NULL values.
    SELECT * FROM orders
    LEFT JOIN customers ON orders.customer_id = customers.customer_id
    WHERE customers.customer_id IS NULL;
    
  9. IS NULL in subqueries in PostgreSQL:

    • Use IS NULL in subqueries for filtering results.
    SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location IS NULL);
    
  10. Negating conditions with IS NOT NULL in PostgreSQL:

    • Use IS NOT NULL to filter non-NULL values.
    SELECT * FROM example_table WHERE non_nullable_column IS NOT NULL;
    
  11. IS NULL and aggregate functions in PostgreSQL:

    • Consider NULL values when using aggregate functions.
    SELECT AVG(salary) FROM employees WHERE manager_id IS NULL;
    
  12. IS NULL with OR and AND conditions in PostgreSQL:

    • Combine IS NULL with OR and AND for flexible conditions.
    SELECT * FROM example_table WHERE (column1 IS NULL AND column2 = 'value') OR column3 IS NULL;
    
  13. Using IS NULL with index scans in PostgreSQL:

    • Be aware that IS NULL conditions might not use indexes efficiently.
    CREATE INDEX idx_nullable_column ON example_table(nullable_column);
    SELECT * FROM example_table WHERE nullable_column IS NULL; -- Index might not be used efficiently
    
  14. IS NULL in combination with EXISTS in PostgreSQL:

    • Use IS NULL with EXISTS to check for the absence of records.
    SELECT * FROM employees
    WHERE NOT EXISTS (SELECT 1 FROM employee_reviews WHERE employee_id = employees.employee_id);
    
  15. IS NULL with GROUP BY and HAVING clauses in PostgreSQL:

    • Incorporate IS NULL in GROUP BY and HAVING clauses for aggregations.
    SELECT department_id, COUNT(*) FROM employees
    GROUP BY department_id
    HAVING COUNT(*) > 1 AND MAX(manager_id) IS NULL;
    
  16. IS NULL in stored procedures and functions in PostgreSQL:

    • Implement IS NULL conditions in stored procedures and functions as needed.
    CREATE OR REPLACE FUNCTION example_function()
    RETURNS TABLE (column1 INTEGER, column2 VARCHAR)
    AS $$
    BEGIN
       RETURN QUERY SELECT * FROM example_table WHERE column1 IS NULL;
    END;
    $$ LANGUAGE plpgsql;