Rod Waldhoff's Weblog  

< Monday, 8 December 2003 >
An SQL Annoyance #

SQL isn't consistent under row/column transposition. For example:

select 3 + NULL

yields NULL. Yet,

create table TEMP ( NUM number );
insert into TEMP values ( 3 );
insert into TEMP values ( NULL );
select sum(NUM) from TEMP

yields 3 (since NULL valued rows are ignored by aggregate functions).

This inconsistency is all the more annoying since both:

select sum(NUM) from TEMP where NUM is not NULL


select sum(coalesce(NUM,0)) from TEMP

would yield the same result under an "aggregation of NULL is NULL" rule. Yet under the "aggregation function ignores NULL" rule, creating a single, efficient, cross-database query the yields NULL if there's a NULL row and the SUM otherwise is awkward at best.