Questions tagged [sql-null]

In the SQL query languages of relational databases, NULL is a special value for "unknown". Use that tag for questions concerning the SQL NULL value. Please also use the "ansi-sql" tag if your question is about the SQL standard or a tag that indicates which database you are using.

In , “NULL” stands for the “unknown” value.

In a , it has a special , so that the following holds:

  • NULL AND X is the same as NULL
  • NULL OR X is the same as X
  • NOT NULL is the same as NULL

Also, most s and , when operating on a NULL (unknown) , will produce a NULL result.

That holds for the in particular, so the X = NULL will not produce TRUE or FALSE, but NULL.

Use x IS NULL to test for NULL-ness and X IS NOT DISTINCT FROM Y to test if X and Y are either both NULL or both not NULL and equal.

database deviates from the SQL standard by treating empty strings as NULL, so don't rely on that if you want to write SQL.

Two guidelines for proper use of NULL:

  • Define your database columns as NOT NULL (which is not the default) wherever possible.

    This improves the quality of your data and makes your queries simpler and consequently faster, since they don't have to deal with the oddities of NULL.

    It is trivial to change a column from to , but not vice versa!

  • Use NULL for unknown values, not for values that are known to be absent, infinite values and the like. That will make your SQL intuitively do the right thing.

    For example, a missing comment had better be an empty string than a NULL, so that string operations work as expected.

    Infinite values are better represented by infinity (if your SQL dialect supports that) or values beyond the normal range, so that comparisons have the intended result.

242 questions
-3
votes
1 answer

SQL Server 2022: Latest Update CU6 - Why does Last_Value does not work when there is no Partition?

Can anyone explain why Last_Value does not take the last value (which is last as per order by) for all rows ? select * from mytableone Initial Data (Please click on link) Query Used: select t.*, LAST_VALUE(PAST_DUE_COL) OVER (Order by ID) as…
-3
votes
3 answers

How to do LEFT JOIN a ON b = c, but if joined table is NULL, join on x instead in (Postgres) SQL?

Given I have two SQL tables: movies ---------------- | id | title | ---------------- | 1 | Matrix | | 2 | Titanic | ---------------- directors ----------------------- | id | name | movieid | ----------------------- | 1 | Steve | 1 …
corgrath
  • 11,673
  • 15
  • 68
  • 99
1 2 3
16
17