Why does Sql Sum of multiple columns containing nulls return incorrect result?

sql, sql-server, t-sql

Solution

What is the sum of `null` and a number, exactly? Note where the `9` comes from: the only row which has non-null `Column1` and `Column2`.

One viable solution has of course already been posted. But then, where's the fun in jumping right to the fix?

(copypasta'd at OP's request)

Problem

Table containing the following values: ``` Column1 Column2 1 NULL NULL 4 2 NULL NULL 5 3 6 ``` The following query: ``` SELECT SUM([Column1] + [Column2] ) FROM [myTable] ``` returns a value of `9` when it should be returning `21`. Why? How does it arrive at the value? I know the SUM can be corrected by adding `ISNULL` like so: ``` SELECT SUM(ISNULL([Column1], 0) + ISNULL([Column2], 0)) FROM [myTable] ``` but I would like to know the logic behind the value `9`

Original source