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
How Can Healthcare Organizations Reduce Cloud Database Costs With AIOps and FinOps
- 1 September 2026
- Database Performance Management
Healthcare organizations can reduce cloud database costs by combining AIOps with FinOps to identify inefficient workloads, detect anomalies, rightsize resources, and connect database performance with infrastructure spending. Effective healthcare cloud database cost optimization focuses on eliminating waste without compromising reliability. Enteros supports this approach through database observability, AI-powered SQL analysis, root cause analysis, performance intelligence, … Continue reading “How Can Healthcare Organizations Reduce Cloud Database Costs With AIOps and FinOps”
What Database Performance Challenges Do Hospitals Face in Cloud-Based Healthcare Systems?
Hospitals face database performance challenges such as slow SQL queries, growing healthcare data, EHR performance issues, database bottlenecks, resource contention, cloud complexity, and rising costs. Cloud-based healthcare systems require continuous database monitoring, workload optimization, anomaly detection, and root-cause analysis. AI-powered AIOps can help hospitals improve database performance, reliability, scalability, and cloud cost efficiency while supporting … Continue reading “What Database Performance Challenges Do Hospitals Face in Cloud-Based Healthcare Systems?”
How AIOps and FinOps Optimize Cloud Database Performance for Banking and BFSI
- 31 August 2026
- Database Performance Management
Introduction The banking and financial services industry is undergoing a significant transformation as institutions move critical workloads to cloud and hybrid-cloud environments. Digital banking applications, payment platforms, mobile banking, lending systems, insurance applications, fraud detection platforms, wealth management solutions, and financial analytics increasingly depend on scalable cloud infrastructure and high-performing databases. However, cloud adoption introduces … Continue reading “How AIOps and FinOps Optimize Cloud Database Performance for Banking and BFSI”
How to Optimize Media and Entertainment Database Performance with Enteros Database Software, AI-Powered Analytics, and Database Observability
- 30 August 2026
- Database Performance Management
Introduction The media and entertainment industry has become a digital-first business. Streaming services, digital publishing, online gaming content, advertising platforms, social experiences, content management systems, subscription platforms, and personalized recommendations all depend on databases. Modern media companies process enormous volumes of customer, content, advertising, transaction, and behavioral information. Read more”Indian Country” highlights Enteros and its … Continue reading “How to Optimize Media and Entertainment Database Performance with Enteros Database Software, AI-Powered Analytics, and Database Observability”