tags:

views:

110

answers:

2

a) If a query/subquery doesn’t find any matching rows, then it either returns NULL or no value at all, thus not even a NULL value. Based on what criteria does a query/subquery return a NULL and when doesn’t it return any results, not even a NULL value?

b) I assume a scalar subquery will always return NULL, when no matching rows are found? I assume most-outer scalar query also returns NULL if no rows are found?

c)

SELECT FirstName, LastName, YEAR(BirthDate)
FROM Persons
WHERE YEAR(BirthDate) IN (SELECT YearReleased FROM Albums);
  • If subquery finds no results, is then a WHERE clause of an outer query translated into WHERE YEAR(BirthDate) IN (null); ?

  • If instead WHERE clause is translated into WHERE YEAR(BirthDate) IN(); then shouldn’t that be an error condition, since how can YEAR(BirthDate) value be compared to nothing?

thanx

+3  A: 

The subquery would only ever return NULL when YearReleased was NULL, otherwise there would be an empty recordset, making it the IN () case you mentioned.

It's very important to distinguish between the two as they mean entirely different things. NULL indicates that there was something to be SELECTed, although that value indicates a "lack of value" so to speak. An empty recordset indicates that there was nothing to be selected that matched the criteria specified.

EDIT: updated to show example results

alt text

First two queries are just to show what's in the two tables. Third query is your query and the fourth query just shows that it produces an equivalent result (no rows) if you replace the subquery with a NULL. Last query is just to show that the subquery itself just returns a big list of NULLs.

Daniel DiPaolo
“The subquery would only ever return NULL when YearReleased was NULL, otherwise there would be an empty recordset, making it the IN () case you mentioned.”In other words, if Albums table contains only rows ( say 10 rows) with their YearReleased field set to NULL, then subquery should produce a result set of 10 rows and WHERE clause of an outer query should be translated into WHERE YEAR(BirthDate) IN (null)?!
AspOnMyNet
See the edit, but yeah that's the where clause would essentially become `WHERE YEAR(BirthDate) IN (NULL)`.
Daniel DiPaolo
I already marked your post as an answer, but if you find the time --> Assuming Albums table contains no rows, then SELECT MAX(CDPrice) FROM Albums; query will still produce a result set (a single row), since return value ( NULL ) of a MAX function is considered a row?!
AspOnMyNet
+1  A: 

a. If there are no matching rows, then the result set will always be empty. There isn't any special handling for the NULL value.

b. That's not true. If there are no matching rows, then the result set is always empty by definition. The result of a scalar function is not a result set so it will either be NULL or another value.

c.1. If the subquery doesn't return any rows then the "IN" expression will always return false. The set will not be NULL.

c.2. It is valid to compare YEAR(BirthDate) with an empty set. It will just always return false.

David
A)“c.1. If the subquery doesn't return any rows then the "IN" expression will always return false. The set will not be NULL.”I assume if Albums table contains only rows ( say 10 rows) with their YearReleased field set to NULL, then subquery should produce a result set of 10 rows and WHERE clause of an outer query should be translated into WHERE YEAR(BirthDate) IN (null);?!B) Assuming Albums table contains no rows, then SELECT MAX(CDPrice)FROM Albums; query will still produce a result set (a single row), since return value ( NULL ) of a MAX function is considered a row?!
AspOnMyNet