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
The Business Benefits of Real-Time Database Performance Analytics in Cloud-Native Environments
- 9 June 2026
- Database Performance Management
In today’s digital economy, speed, reliability, and scalability are no longer optional—they are fundamental business requirements. Organizations across industries are rapidly adopting cloud-native architectures to support digital transformation initiatives, improve operational agility, and deliver exceptional customer experiences. Cloud-native environments enable businesses to deploy applications faster, scale dynamically, and innovate more efficiently. However, these benefits also … Continue reading “The Business Benefits of Real-Time Database Performance Analytics in Cloud-Native Environments”
How to Modernize Retail Cost Management with Enteros Database Performance Intelligence and Cost Estimation
- 8 June 2026
- Database Performance Management
Introduction The retail industry is undergoing a significant digital transformation as organizations invest heavily in ecommerce platforms, omnichannel experiences, customer engagement technologies, and cloud-native infrastructures. While these innovations help retailers improve customer experiences and accelerate growth, they also create new challenges related to operational costs, infrastructure efficiency, and technology performance. Modern retail ecosystems now support: … Continue reading “How to Modernize Retail Cost Management with Enteros Database Performance Intelligence and Cost Estimation”
Why Proactive Database Monitoring Is Essential for Enterprise Application Reliability
In today’s digital-first business environment, enterprise applications are the backbone of critical operations. From customer-facing platforms and financial systems to healthcare applications and SaaS products, organizations rely on applications to deliver seamless experiences, support business processes, and drive revenue growth. As customer expectations continue to rise, application reliability has become a key business differentiator. Users … Continue reading “Why Proactive Database Monitoring Is Essential for Enterprise Application Reliability”
How to Improve Fashion Ecommerce Performance with Enteros Database Analytics and AIOps Automation
Introduction The fashion industry has evolved into one of the most technology-driven sectors in the global economy. Modern fashion brands, retailers, and ecommerce platforms rely heavily on digital technologies to engage customers, manage inventory, personalize shopping experiences, and drive revenue growth. Today’s fashion ecommerce ecosystems support: Online storefronts Mobile shopping applications Inventory management systems Product … Continue reading “How to Improve Fashion Ecommerce Performance with Enteros Database Analytics and AIOps Automation”