15.1. EXISTS and NULLs

A NULL might not be a value, but it does exist in SQL. This is often a problem for a new SQL programmer who is having trouble with NULLs and how they behave.

Think of them as being like a brown paper bag—you know that something is inside because you lifted it, but you do not know exactly what that something is. For example, we want to find all the employees who were not born on the same day as a famous person. This can be answered with the negation of the original query, like this:

SELECT P1.emp_name, ' was born on a day without a famous person!'
  FROM Personnel AS P1
 WHERE NOT EXISTS
       (SELECT *
          FROM Celebrities AS C1
         WHERE P1.birthday = C1.birthday);

But assume that among the celebrities we have a movie star who will not ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.