In Clause with NULL or IS NULL
You can use a combination of IN
and IS NULL
to include values and NULLs
.
This query returns rows where column
is either 1
, 2
, OR NULL
.
Swinging with subqueries in IN Clause
Executing more complex conditions? Don't panic. The IN
clause accepts subquery results, your gateway to complex filtering:
However, this is a NULL
-blind approach. To relieve NULL
values from their invisibility cloak, toss in an OR
with IS NULL
:
A piece, a slice, a coalesce
Ever dreamed of transforming NULL
to a tangible value? The COALESCE
function is all you need! It substitutes NULL
with a default value, enabling matching with the IN
clause:
Handling the NOT IN beast
Beware, the NOT IN
operator can get ferocious with NULL
. If NULL
sneaks into the list, the NOT IN
condition returns FALSE
. Ensure you send NULL
away before using NOT IN
:
Best practices in the realm of SQL
Level up your SQL game by:
- Ditching the
SELECT *
: Itemise the much-needed columns. - Grouping conditions using parentheses: It's all about the correct precedence.
- Polishing your SQL skills: Write queries that are clean, readable, and efficient.
- Digging deeper: Articles, documentations for thorough understanding of
NULL
handling. You can't complain about what you don't understand, right?
Was this article helpful?