0
0
PostgreSQLquery~5 mins

IS DISTINCT FROM for NULL-safe comparison in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does IS DISTINCT FROM do in PostgreSQL?

IS DISTINCT FROM compares two values and returns true if they are different, including when one is NULL and the other is not. It treats NULL as a comparable value.

Click to reveal answer
beginner
How is IS DISTINCT FROM different from the usual = operator when comparing NULL values?

The usual = operator returns NULL (unknown) when comparing NULL to anything, even NULL. IS DISTINCT FROM returns false if both values are NULL, and true if only one is NULL.

Click to reveal answer
beginner
Write a simple SQL query using IS DISTINCT FROM to find rows where column a differs from column b, including NULL differences.
SELECT * FROM table_name WHERE a IS DISTINCT FROM b;
Click to reveal answer
beginner
Why is IS DISTINCT FROM useful in real-life database queries?

It helps to compare values safely when NULL values are involved, avoiding unexpected NULL results and making logic clearer and more reliable.

Click to reveal answer
beginner
What will the expression NULL IS DISTINCT FROM NULL return?

It returns false because both sides are NULL, so they are not distinct.

Click to reveal answer
What does a IS DISTINCT FROM b return if a = NULL and b = 5?
Atrue
Bfalse
CNULL
DError
What is the result of 5 IS DISTINCT FROM 5?
Afalse
Btrue
CNULL
DError
Which operator treats NULL as a comparable value?
A=
BIS DISTINCT FROM
C<>
DIS NULL
If both values are NULL, what does a IS DISTINCT FROM b return?
ANULL
Btrue
Cfalse
DError
Why might you prefer IS DISTINCT FROM over = in queries?
AIt only works with numbers
BIt is faster
CIt returns <code>NULL</code> for all comparisons
DIt handles <code>NULL</code> comparisons safely
Explain how IS DISTINCT FROM works when comparing two values, especially with NULLs.
Think about how normal = operator treats NULL and how this is different.
You got /4 concepts.
    Describe a situation where using IS DISTINCT FROM is better than using = in a SQL query.
    Consider comparing two columns where some values might be missing.
    You got /4 concepts.