Skip to content

EXISTS

Define a condition that tests whether a subquery returns rows. Return true if the subquery returns at least one row.

[ NOT ] EXISTS (subquery)

NOT EXISTS means that the condition returns true if the subquery returns no rows.

Examples

premdb=# SELECT seasonid FROM season 
WHERE EXISTS(SELECT seasonid FROM match);
 seasonid 
----------
       1
       2
       3
       4
       5
       6
       7
...
premdb=# SELECT * FROM awayteam 
WHERE EXISTS(SELECT htid FROM hometeam WHERE htid=awayteam.atid);
 atid |  name   
------+---------
   51 | Arsenal
(1 row)