I know this question might seem a little basic stuff but I want to make sure I get the right syntax because this line of code will run often:
I want to sum columns A, B and C of table “alphabet” for all rows which have an id included in my IN clause.
This is how I would do it but I’d like a confirmation if possible:
SELECT SUM(A + B + C) as "subtotal" FROM alphabet WHERE id IN ('1','5','378');
If
idis not a string, you shouldn’t quote those values.This should work, but may may have one non-obvious consequence. If any row has
Anot null and one of the others null, that row will drop out of the summation because a null plus anything else is null, and nulls are ignored by theSUMoperator. Thus it may be safer to write:This suffers from the same potential problem, but it is less likely to happen because an entire column would have to be null. There are various dialect specific ways to defend against that problem if you are still concerned. The most portable is the painfully verbose: