Preamble
If a subquery returns at least one line, the SQL EXISTS condition is used with it and is said to be satisfied. The SELECT, INSERT, UPDATE, and DELETE operators all support its use.
Syntax of EXISTS condition in SQL
WHERE EXISTS ( subquery_id );
where:
- If a subquery returns at least one record in its result set, the EXISTS condition is satisfied and the EXISTS offer is evaluated as true. If the subquery returns no records, the EXISTS sentence is evaluated as false and the EXISTS condition is not satisfied.
Note:
SQL statements using the EXISTS condition are very inefficient because the subquery is run again for EVERY line in the external query table. The majority of queries that don’t use the EXISTS condition can be written more effectively.
Using the EXISTS Condition with the SELECT Operator
Let us start with an example that shows how to use the EXISTS condition with a SELECT operator.
In this example we have a table with the following data:
| ord_id | custom_id | ord_date |
| 1 | 7000 | 2019/06/18 |
| 2 | 5000 | 2019/06/18 |
| 3 | 8000 | 2019/06/19 |
| 4 | 4000 | 2019/06/20 |
| 5 | NULL | 2019/07/01 |
And the table with the following data:
| ord_id | custom_id | ord_date |
| 1 | 7000 | 2019/06/18 |
| 2 | 5000 | 2019/06/18 |
| 3 | 8000 | 2019/06/19 |
| 4 | 4000 | 2019/06/20 |
| 5 | NULL | 2019/07/01 |
Now let’s find all the records from the customers table, where there is at least one record in the orders table with the same custom_id. Perform the following SELECT query:
SELECT *
FROM customs
WHERE EXISTS
(SELECT *
FROM ords
WHERE customs.custom_id = ords.custom_id);
Four entries will be selected. Here are the results that you should get:
| custom_id | f_name | l_name | fav_website |
| 4000 | Justin | Bieber | google.com |
| 5000 | Selena | Gomez | bing.com |
| 7000 | Tom | Cruise | oracle.com |
| 8000 | Johnny | Depp | NULL |
In this example, there are 4 entries in the customers table that have the value custom_id in the orders table.
EXISTS condition with UPDATE operator
Let’s take a look at an instance of the UPDATE operator that makes use of the EXISTS condition.
In this illustration, the product table contains the information below:
| prod_id | prod_name | cat_id |
| 1 | Pear | 50 |
| 2 | Banana | 50 |
| 3 | Orange | 50 |
| 4 | Apple | 50 |
| 5 | Bread | 75 |
| 6 | Sliced Ham | 25 |
| 7 | Kleenex | NULL |
And a table named sum_data with the following data:
| prod_id | current_cat |
| 1 | 10 |
| 2 | 10 |
| 3 | 10 |
| 4 | 10 |
| 5 | 10 |
Let’s now add values from the products table to the sum_data table. Type the following SQL query:
UPDATE sum_data
SET current_cat = (SELECT cat_id)
FROM prods
WHERE prods.prod_id = sum_data.prod_id)
WHERE EXISTS (SELECT cat_id)
FROM prods
WHERE prods.prod_id = sum_data.prod_id);
5 records will be updated. Select data from sum_data table again:
SELECT *
FROM sum_data;
Here are the results you’ll get:
| prod_id | current_cat |
| 1 | 50 |
| 2 | 50 |
| 3 | 50 |
| 4 | 50 |
| 5 | 75 |
| 8 | 10 |
In this case, where prod_id values match, the current_cat field in the sum_data table will be updated with cat_id information from the products table. The sum_data table’s first five records were updated.
If the EXISTS condition wasn’t turned on, the current_category field in the sixth row of the summary_data table would be changed to NULL because there is no record in the products table with the product_id = 8 value.
EXISTS Condition with DELETE Operator
Let’s take a look at a DELETE operator example that makes use of the EXISTS condition.
In this example, we have a table with the following data:
|
custom_id
|
f_name
|
l_name
|
fav_website
|
|---|---|---|---|
|
4000
|
Justin
|
Bieber
|
google.com
|
|
5000
|
Selena
|
Gomez
|
bing.com
|
|
6000
|
Mila
|
Kunis
|
yahoo.com
|
|
7000
|
Tom
|
Cruise
|
oracle.com
|
|
8000
|
Johnny
|
Depp
|
NULL
|
|
9000
|
Russell
|
Crowe
|
google.com
|
And the table with the following data:
|
ord_id
|
custom_id
|
ord_date
|
|---|---|---|
|
1
|
7000
|
2019/06/18
|
|
2
|
5000
|
2019/06/18
|
|
3
|
8000
|
2019/06/19
|
|
4
|
4000
|
2019/06/20
|
|
5
|
NULL
|
2019/07/01
|
Enter the following DELETE operator:
DELETE FROM ords
WHERE EXISTS
(SELECT *
FROM customs
WHERE customs.custom_id = ords.custom_id
AND customs.l_name = ‘Bieber’);
The deletion of 1 record. Re-select the information from the order table:
SELECT * FROM ords;
here are the results you should get:
|
ord_id
|
custom_id
|
ord_date
|
|---|---|---|
|
1
|
7000
|
2019/06/18
|
|
2
|
5000
|
2019/06/18
|
|
3
|
8000
|
2019/06/19
|
|
5
|
NULL
|
2019/07/01
|
In the case where the customer table contains a record with the l_name “Bieber” and the same custom_id value across both tables, the example deletes all records from the order table. In this example the record for ord_id = 4 has been deleted.
NOT with EXISTS condition
Finally, the NOT EXISTS condition can be produced by combining the NOT condition with the EXISTS condition. Let’s take a look at a SQL example that demonstrates how to use the NOT EXISTS condition.
In this example we have a table with the following data:
|
custom_id
|
f_name
|
l_name
|
fav_website
|
|---|---|---|---|
|
4000
|
Justin
|
Bieber
|
google.com
|
|
5000
|
Selena
|
Gomez
|
bing.com
|
|
6000
|
Mila
|
Kunis
|
yahoo.com
|
|
7000
|
Tom
|
Cruise
|
oracle.com
|
|
8000
|
Johnny
|
Depp
|
NULL
|
|
9000
|
Russell
|
Crowe
|
google.com
|
And the table with the following data:
|
ord_id
|
custom_id
|
ord_date
|
|---|---|---|
|
1
|
7000
|
2019/06/18
|
|
2
|
5000
|
2019/06/18
|
|
3
|
8000
|
2019/06/19
|
|
4
|
4000
|
2019/06/20
|
|
5
|
NULL
|
2019/07/01
|
Type the following SQL query:
SELECT *
FROM customs
WHERE DOES NOT EXIST
(SELECT *
FROM ords
WHERE customs.custom_id = ords.custom_id);
There will be two winners. These are the outcomes you’ll get:
|
custom_id
|
f_name
|
l_name
|
fav_website
|
|---|---|---|---|
|
6000
|
Mila
|
Kunis
|
yahoo.com
|
|
9000
|
Russell
|
Crowe
|
google.com
|
If there are no entries in the ords table for this custom_id, this example will return all entries from the customs table.
The SQL EXISTS clause
About Enteros
Enteros offers a patented database performance management SaaS platform. It finds the root causes of complex database scalability and performance problems that affect business across a growing number of cloud, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Governing Cloud Economics at Scale: Enteros Cost Attribution and FinOps Intelligence for BFSI and Technology Organizations
- 25 January 2026
- Database Performance Management
Introduction Cloud adoption has become foundational for both BFSI institutions and technology-driven enterprises. Banks, insurers, fintechs, SaaS providers, and digital platforms now depend on cloud-native architectures to deliver real-time services, enable AI-driven innovation, ensure regulatory compliance, and scale globally. Yet as cloud usage accelerates, so does a critical challenge: governing cloud economics at scale. Despite … Continue reading “Governing Cloud Economics at Scale: Enteros Cost Attribution and FinOps Intelligence for BFSI and Technology Organizations”
Turning Telecom Performance into Revenue: Enteros Approach to Database Optimization and RevOps Efficiency
Introduction The telecom industry is operating in one of the most demanding digital environments in the world. Explosive data growth, 5G rollout, IoT expansion, cloud-native services, and digital customer channels have fundamentally transformed how telecom operators deliver services and generate revenue. Behind every call, data session, billing transaction, service activation, roaming event, and customer interaction … Continue reading “Turning Telecom Performance into Revenue: Enteros Approach to Database Optimization and RevOps Efficiency”
Scaling AI Without Overspend: How Enteros Brings Financial Clarity to AI Platforms
- 22 January 2026
- Database Performance Management
Introduction Artificial intelligence is no longer experimental. Across industries, AI platforms now power core business functions—recommendation engines, fraud detection, predictive analytics, conversational interfaces, autonomous decision systems, and generative AI applications. But as AI adoption accelerates, a critical problem is emerging just as fast: AI is expensive—and most organizations don’t fully understand why. Read more”Indian Country” … Continue reading “Scaling AI Without Overspend: How Enteros Brings Financial Clarity to AI Platforms”
AI-Native Database Performance Management for Real Estate Technology Enterprises with Enteros
Introduction Real estate has rapidly evolved into a technology-driven industry. From digital property marketplaces and listing platforms to smart building systems, valuation engines, CRM platforms, and AI-powered analytics, modern real estate enterprises run on data-intensive technology stacks. At the center of this transformation lies a critical foundation: databases. Every property search, pricing update, lease transaction, … Continue reading “AI-Native Database Performance Management for Real Estate Technology Enterprises with Enteros”