15.2. EXISTS and INNER JOINs

The [NOT] EXISTS predicate is almost always used with a correlated subquery. Very often the subquery can be “flattened” into a JOIN, which will frequently run faster than the original query. Our sample query can be converted into:

SELECT P1.emp_name, ' has the same birthday as a famous person!'
  FROM Personnel AS P1, Celebrities AS C1
 WHERE P1.birthday = C1.birthday;

The advantage of the JOIN version is that it allows us to show columns from both tables. We should make the query more informative by rewriting it:

SELECT P1.emp_name, ' has the same birthday as ', C1.emp_name
  FROM Personnel AS P1, Celebrities AS C1
 WHERE P1.birthday = C1.birthday;

This new query could be written with an EXISTS() predicate, but that is ...

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.