Name three ways NULL breaks queries.
= NULL never matches (use IS NULL); NOT IN with a NULL in the subquery returns no rows; aggregates ignore NULLs, so COUNT(col) and AVG(col) differ from what people expect; NULL in arithmetic or concatenation produces NULL; a WHERE filter on the right side of a LEFT JOIN removes the NULL-extended rows.
= NULL never matches (use IS NULL); NOT IN with a NULL in the subquery returns no rows; aggregates ignore NULLs, so COUNT(col) and AVG(col) differ from what people expect; NULL in arithmetic or concatenation produces NULL; a WHERE filter on the right side of a LEFT JOIN removes the NULL-extended rows.
Comparisons
= NULLnever matches: use IS NULLNOT INwith a NULL returns no rows
Aggregates and arithmetic
COUNT(col)andAVG(col)skip NULLs- NULL in arithmetic gives NULL
LEFT JOIN
- A WHERE filter on the right side
- removes the NULL-extended rows
See the concept: Join fan-outWhy a join to a one-to-many table multiplies sums
| O101 | line 1 | ₹300 |
| O101 | P101 | FAILED |
| O101 | P102 | SUCCESS |
| line 1 | P101 | ₹300 |
| line 1 | P102 | ₹300 |
Wrong: the single ₹300 line is counted once per payment attempt.
Fix: aggregate payments to one row per order first
line 1, paid, ₹300, so SUM = ₹300
Key points
- three-valued logic
- NOT IN
- COUNT(*) vs COUNT(col)
- COALESCE