---
schema_version: "1"
id: 7bc6d8410c5fd8182927c96ed30f577d7a2b6bbb45169fd2746aa9b27f6a20cc
title: Subquery Comparisons
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/data-cloud/data-cloud-query-guide/references/data-cloud-sql-reference/scalar-func/subquery-comparison.md
canonical_url: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/subquery-comparison.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: dfce5a69e683a26a7637ba34eb45b20387b61d353f1b0eaeed87d61801f72b45
products:
  - Data 360 Query Guide
  - Data 360 Developer Documentation
---
# Subquery Comparisons

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

This section describes the SQL-compliant subquery expressions. All of the expression forms documented in this section return Boolean results.

Subquery comparisons executes until at least one row is returned, not to completion. For this reason, avoid writing a subquery with side effects.

## `EXISTS`

```sql
EXISTS ( <subquery> )
```

The argument of EXISTS is an arbitrary `SELECT` statement, or subquery. The subquery is evaluated to determine if it returns any rows. If it returns at least one row, the result of EXISTS is “true”. If the subquery returns no rows, the result of EXISTS is “false”.

The subquery can refer to variables from the surrounding query, which act as constants during any one evaluation of the subquery.

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.

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.

```sql
SELECT col1
FROM tab1
WHERE EXISTS (SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
```

## `IN`

```sql
<expression> IN ( <subquery> )
```

The right-hand side is a parenthesized subquery, that returns exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of IN is true if any equal subquery row is found. The result is false if no equal row is found, including if the subquery returns no rows.

If the left-hand expression returns `NULL`, or if there are no equal right-hand values and at least one right-hand row returns `NULL`, the result of the IN construct is `NULL`.

## `NOT IN`

```sql
<expression> NOT IN ( <subquery> )
```

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of NOT IN is true if only unequal subquery rows are found, including the case if the subquery returns no rows. The result is false if an equal row is found.

If the left-hand expression returns `NULL`, or if there are no equal right-hand values and at least one right-hand row returns `NULL`, the result of the NOT IN construct is `NULL`.

## `ANY`/`SOME`

```sql
<expression> <operator> ANY ( <subquery> )
<expression> <operator> SOME ( <subquery> )
```

The right-hand side is a parenthesized subquery that returns exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given `<operator>`, which returns a Boolean result. The result of ANY is true if any true result is obtained. The result is false if no true result is found, including the case if the subquery returns no rows.

SOME is a synonym for ANY. IN is equivalent to `= ANY`.

If there are no successes and at least one right-hand row yields `NULL` for the operator's result, the result of the ANY construct is `NULL`.

## `ALL`

```sql
<expression> <operator> ALL ( <subquery> )
```

The right-hand side is a parenthesized subquery, which returns exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given `<operator>`, which returns a Boolean result. The result of ALL is true if all rows yield true, including the case if the subquery returns no rows. The result is false if any false result is found. The result is `NULL` if no comparison with a subquery row returns false and at least one comparison returns `NULL`.

NOT IN is equivalent to `<> ALL`.

**Tip:** To avoid unexpected results, filter out `NULL` values in your subquery (for example, `WHERE column IS NOT NULL`) when using `ALL` or `NOT IN`. A single `NULL` can cause the entire expression to return `NULL` instead of `TRUE`.
