March 6th, 2009

Nulls in a Subselect

Posted by admin in N. Subselects

Nulls in a subselect can produce unexpected results.

When nulls are in a comparison of a subselect, no rows will be returned, because NULL means that there is no value. When this happens, no rows are returned because the operation evaluated to NULL, not TRUE or FALSE.

To avoid this, test the value of the subselect with WHERE value IS NOT NULL or HAVING value IS NOT NULL.

This test is not necessary if NOT NULL is specified in the table definition of this column.

                    Format:      SELECT select_columns
                                         FROM tables
                                      WHERE value operator
                                                  (SELECT select_columns
                                                      FROM tables
                                                   WHERE conditions
                                                        AND column IS NOT NULL);

Comments are closed.

Sorry, the comment form is closed at this time.