EXISTS

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Tests whether a subquery returns any rows. Returns a boolean result.

Syntax 

1EXISTS ( <subquery> )

Arguments 

Required 

  • <subquery>: An arbitrary SELECT statement. The subquery is evaluated to determine whether it returns any rows. It can refer to variables from the surrounding query, which act as constants during any one evaluation of the subquery.

Returns 

Returns true if the subquery returns at least one row, and false if the subquery returns no rows.

Considerations 

  • Because the result depends only on whether rows are returned—not on the row contents—the specific columns selected don’t affect the result. A common coding convention is to write all EXISTS expressions in the form EXISTS (SELECT 1 WHERE ...). Exceptions to this convention include subqueries that use INTERSECT or EXCEPT.

Examples 

Correlated Existence Check 

Find rows in one table that have a matching row in another. This example is similar to an inner join on col2, but it produces at most one output row for each tab1 row, even if there are several matching tab2 rows.

1SELECT col1
2FROM tab1
3WHERE EXISTS (SELECT 1 FROM tab2 WHERE col2 = tab1.col2);

Related Documentation