22

EXISTS() Predicate

The EXISTS predicate is very natural. It is a test for a nonempty set (read: table). If there are any rows in its subquery, it is TRUE; otherwise, it is FALSE. This predicate does not give an UNKNOWN result. The syntax is:

<exists predicate> ::= [NOT] EXISTS <table subquery>

It is worth mentioning that a <table subquery> is always inside parentheses to avoid problems in the grammar during parsing.

In SQL-89, the rules stated that the subquery had to have a SELECT clause with one column or an asterisk ()). If the SELECT ) option was used, the database engine would (in theory) pick one column and use it. This fiction was needed because SQL-89 defined subqueries as having only one column. Things are better today.

Some early SQL ...

Get Joe Celko's SQL for Smarties, 4th 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.