Explain Codes LogoExplain Codes Logo

Sql is null and = null

sql
null-values
sql-queries
database-design
Alex KataevbyAlex Kataev·Nov 23, 2024
TLDR

In SQL, utilize IS NULL for identifying rows burdened with NULL values, for NULL is akin to a void, a marker for absent or unsuitable data. The comparison = NULL is improper as NULL isn't equivalent to anything, including another NULL. Thus, adhere to IS NULL when dealing with queries involving NULL.

Correct:

SELECT * FROM your_table WHERE your_column IS NULL;

Incorrect would yield no results:

SELECT * FROM your_table WHERE your_column = NULL;

Take heed, NULL calls for special treatment using IS NULL.

Ground rules for NULL in SQL

NULL: A maverick in SQL universe

In the realm of SQL, NULL signifies an absence of value. It's a state indicating "unknown value", "no value given", or "value doesn’t apply". Since it represents a lack of value, any standard comparison like =, <>, or < is a no-no with NULL.

NULL: The game changer in SQL operations

Indulge NULL in any SQL operation, and voila! The outcome is deemed unknown. This holds true for arithmetic operations, string mashups, and comparisons with other values. Here's a lil' sampler to bring out this peculiarity:

Incorrect - Expecting no results just like my dating app 😟

SELECT 'Match Found' WHERE NULL = NULL;

Correct - Finally, a match! 😌

SELECT 'Match Found' WHERE NULL IS NULL;

Avoid these RED FLAGS with NULL

When using NULL with aggregate functions, proceed with caution my friend:

  • COUNT(column_name) – It's a ‘no’ on counting rows if column_name is NULL.
  • SUM and AVG – They ‘shun’ NULL values, which could sneakily alter the overall value.

The curious case of NULL in SQL

NULL: Schrodinger's cat of SQL

In SQL queries, treat NULL like Schrodinger's cat—it doesn't exist or not exist, until put under the observant eye of the correct predicate like IS NULL or IS NOT NULL.

NULL’s got conditions in SQL

When NULL enters the realm of comparisons: It conjures up unknown results:

--NULL doesn't play 'Not Equal to' hence, no show in WHERE clause (probably off attending a wizardry convention 🎩✨) SELECT 'This won’t appear' WHERE NULL <> NULL; --NULL combined with any value, even itself, doesn't end in a happy union (😭💔) SELECT 'Neither will this' WHERE NULL = 1;

Acing the NULL game in SQL

  • Use COALESCE or ISNULL to substitute NULLs with a ‘plan B’.
  • Employ IS NOT NULL as your go-to to ensure that a column wears a non-NULL hat.

NULL’s DAY OUT: IS NULL and = NULL

Conditional queries in SQL: Time for an encounter with NULL

Make your peace with how to look for NULL when setting up conditional SQL queries. Using IS NULL in a condition is like deploying your special search dog to look for no-value sneaks:

-- Fetch rows that are Batman (i.e., hiding ‘description’ just like BatCave) SELECT * FROM products WHERE description IS NULL;

NULL: A special guest star in OUTER JOINs

This is NULL’s star turn. It makes a guest appearance in OUTER JOIN operations, when NULL can be the missing person in a column of a table sans a match:

-- Find customers who are on a shopping detox SELECT customers.name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id WHERE orders.order_id IS NULL;

NULL and Indexing: A SQL thriller

Mind your step! Indexes pretty much ghost NULL values, which could sneakily alter query performance:

-- An index on 'email' will not help. It’s busy friendzoning this query SELECT * FROM users WHERE email IS NULL;