SQL > Advanced SQL > Exists

EXISTS is a Boolean operator used in a subquery to test whether the inner query returns any row. If it does, then the outer query proceeds. If not, the outer query does not execute, and the entire SQL statement returns nothing.

SQL EXISTS is a Boolean operator used with subqueries: it returns TRUE if the inner query returns at least one row, allowing the outer query to execute — and FALSE (returning nothing) if the inner query returns no rows.

Syntax

The syntax for EXISTS is:

SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS
(SELECT *
FROM "table_name2"
WHERE "condition");

Please note that instead of *, you can select one or more columns in the inner query. The effect will be identical.

Example

We use the following tables for our example.

Table Store_Information

 Store_Name  Sales  Txn_Date 
 Los Angeles 1500  Jan-05-1999 
 San Diego 250  Jan-07-1999 
 Los Angeles 300  Jan-08-1999 
 Boston 700  Jan-08-1999 

Table Geography

 Region_Name  Store_Name 
 East  Boston 
 East  New York 
 West  Los Angeles 
 West  San Diego 

The following SQL query,

SELECT SUM(Sales) FROM Store_Information
WHERE EXISTS
(SELECT * FROM Geography
 WHERE Region_Name = 'West');

produces the result below:

SUM(Sales)
2750

At first, this may appear confusing, because the subquery includes the [region_name = 'West'] condition, yet the query summed up sales for stores in all regions. Upon closer inspection, we find that since the subquery returns more than zero row, the EXISTS condition is true, and the rows returned from the query "SELECT SUM(Sales) FROM Store_Information" become the final result.

Frequently Asked Questions

What does SQL EXISTS do?
SQL EXISTS is a Boolean operator that tests whether a subquery returns any rows. If the inner query returns at least one row, EXISTS evaluates to TRUE and the outer query proceeds. If the inner query returns no rows, EXISTS is FALSE and the entire statement returns no results.
What is the difference between SQL EXISTS and SQL IN?
EXISTS checks whether a subquery returns any rows (a TRUE/FALSE result). IN checks whether a specific value matches any value in a list or subquery. EXISTS is often faster for correlated subqueries with large data sets, while IN is intuitive for simple value matching against small lists.
Can EXISTS be used with NOT?
Yes. NOT EXISTS returns TRUE when the subquery returns zero rows, making it useful for finding records in one table that have no matching records in another — for example, customers who have placed no orders.
Does it matter what you SELECT in the EXISTS subquery?
No. EXISTS only cares whether the subquery returns any rows, not what values those rows contain. SELECT *, SELECT 1, or SELECT any_column all produce the same behavior. By convention, SELECT * or SELECT 1 is commonly used.

Next: SQL CASE

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact