How to handle NULL in SQL

preview_player
Показать описание
The NULL value represents missing or not applicable information. But using it introduces three-valued logic. This can lead to surprising outcomes when handled incorrectly.

This session covered the problems using NULL brings and how to avoid these. Highlights include:

- 00:30 - The problem with val = null
- 05:30 - Demo of problems and quirks with null
- 10:50 - Nulls in aggregate functions
- 16:30 - Nulls in check constraints
- 18:40 - Nulls in ORDER BY
- 20:50 - Why does null exist?
- 22:10 - Examples of not applicable/missing information and ways to change the schema to avoid null
- 32:40 - Handling end dates for currently active rows - null vs separate tables vs magic values
- 41:00 - The problem with magic values
- 45:00 - Situations where nulls can still appear when all columns are mandatory: outer joins, new columns, and window functions
- 47:30 - Demo of nulls in outer joins
- 48:45 - Nulls returned by window functions
- 50:00 - Mapping nulls to non-null values
- 55:40 - How to include nulls when searching for rows less than some value
- 1:00:30 - Summary

Рекомендации по теме
Комментарии
Автор

I prefer thinking of Unknown as Null and like the zero in multiplication it is absorbant for a lot of comparator operators (=, <>, <, <=, >, >=). So instead of having 4 states (true, false, null, and unknown) to consider, when I evaluate a Boolean expression I use only 3 states (true, false and null). And NULL when is the final value evaluates to FALSE in a WHERE predicate or in a WHEN and IF statement. Also, it mirrors the uses of the empty set (∅) when you use null with the operator AND (equivalent to intersect operator (⋂) for sets, not to confuse with INTERSECT used between multiple SELECT statement) and the operator OR (equivalent to union operator (⋃) for sets ). So, (NULL AND Y) ≡ NULL and (NULL OR Y) ≡ Y. The same as (∅ ⋂ Y) ≡ ∅ and (∅ ⋃ Y) ≡ Y. Or as (0 × Y) ≡ 0 and (0 + Y) ≡ Y.

dominiquefortin
Автор

Good Video, The ONLY thing it did not cover with respect to NULLS is the Behaviour of NULLS wrt to GROUP BY Clause.

POWERTOKNOW
join shbcf.ru