intro
SQL EXISTS is a logical operator that adds considerable flexibility to your database queries. This article talks about the EXISTS operator, its use cases, and examples.
In this guide, we will look at how the SQL EXISTS
operators compares to other SQL constructs and when it is the optimal choice for your queries with various database management systems, such as MySQL, PostgreSQL, SQL Server, and Oracle using a tool like DbVisualizer.
What is EXISTS in SQL?
In SQL, the EXISTS
operator is a boolean operator that tests for the existence of records in a subquery. It returns:
This SQL EXISTS
operator can be used in various SQL statements like SELECT
, UPDATE
, INSERT
, and DELETE
to filter data based on whether certain conditions are met.
This seemingly simple functionality enables powerful query patterns, especially when working with complex data relationships.
SQL EXISTS: Operator Syntax
The basic syntax of the EXISTS
SQL operator is:
1
SELECT column1, column2, ...
2
FROM table_name
3
WHERE EXISTS (
4
SELECT column_name(s) or SELECT 1
5
FROM another_table
6
WHERE condition
7
);
In the syntax above:
SQL EXISTS: Example List
Sweet! Now without further ado, let’s get right into some examples of the SQL EXISTS
operator.
Keep it at the back of your mind that all demonstrations will be done in DbVisualizer—the database client with the highest user satisfaction in the market.
Also, note that the following SQL examples are database-agnostic, using standard ANSI SQL syntax that works across most major relational database management systems (RDBMS) like PostgreSQL, MySQL, SQL Server, and Oracle.
Example #1 - Using EXISTS To Fetch Customers Who have Placed Orders
We will create two tables:
To create and populate them, use this query:
1
CREATE TABLE customers (
2
customer_id INT PRIMARY KEY,
3
name VARCHAR(100)
4
);
5
6
CREATE TABLE orders (
7
order_id INT PRIMARY KEY,
8
customer_id INT,
9
order_date DATE,
10
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
11
);
12
13
INSERT INTO customers (customer_id, name) VALUES (1, 'Wenger'), (2, 'Flick'), (3, 'Erik');
14
INSERT INTO orders (order_id, customer_id, order_date) VALUES (101, 1, '2025-01-10'), (102, 3, '2025-02-15');

We want to retrieve the names of customers who have placed at least one order. To achieve this, you can use EXISTS
in SQL to check whether a corresponding order exists for each customer:
1
SELECT name
2
FROM customers c
3
WHERE EXISTS (
4
SELECT 1
5
FROM orders o
6
WHERE o.customer_id = c.customer_id
7
);
The result will be:

Since Wenger and Erik have placed orders, they appear in the output. Flick, who has no orders, is excluded.
Example #2 - Using NOT EXISTS To Fetch Customers w/o Orders
To find customers who have not placed any orders, you can use NOT EXISTS
. This query will return customers whose customer_id
does not exist in the orders
table:
1
SELECT name
2
FROM customers c
3
WHERE NOT EXISTS (
4
SELECT 1
5
FROM orders o
6
WHERE o.customer_id = c.customer_id
7
);
The result will be:

Example #3 - Using EXISTS with Correlated Subqueries to Retrieve Products with at Least One Sale
A correlated subquery is executed for each row in the outer query. This is particularly useful when checking for the presence of related data on a per-row basis.
We’ll create a products
table containing product details and a sales
table that records product sales:
1
CREATE TABLE products (
2
product_id INT PRIMARY KEY,
3
product_name VARCHAR(100)
4
);
5
6
CREATE TABLE sales (
7
sale_id INT PRIMARY KEY,
8
product_id INT,
9
FOREIGN KEY (product_id) REFERENCES products(product_id)
10
);
11
12
INSERT INTO products (product_id, product_name) VALUES (1, 'Laptop'), (2, 'Phone');
13
INSERT INTO sales (sale_id, product_id) VALUES (1001, 1);
Now, utilize the SQL EXISTS
operator to find products that have been sold:
1
SELECT p.product_name
2
FROM products p
3
WHERE EXISTS (
4
SELECT 1 FROM sales s WHERE s.product_id = p.product_id
5
);
This time, the result will be:

The output only includes 'VR Set' because it has an entry in the sales
table, whereas 'MacBook' does not.
Example #4 - Using EXISTS with the UPDATE Operation to Mark Customers as Active
We can also use EXISTS
in an UPDATE
statement to modify records based on the presence of related data. For example, marking customers as ‘Active’ if they have placed an order.
First, we add a status
column to the customers
table:
1
ALTER TABLE customers ADD COLUMN status VARCHAR(20);
Now, we update customers who have placed an order:
1
UPDATE customers c
2
SET status = 'Active'
3
WHERE EXISTS (
4
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
5
);
The query would produce this result:

Wenger and Erik are marked as 'Active' since they have placed orders, while Flick remains NULL
.
Example #5 - Using EXISTS in SQL with the DELETE Operation to Delete Customers Who have No Orders
We can use EXISTS
in a DELETE
statement to remove records that meet a certain condition.
In this example, let’s keep only customers who have placed orders remain in the customers
table:
1
DELETE FROM customers c
2
WHERE NOT EXISTS (
3
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
4
);

We can see in DbVisualizer that after executing the query, only customers who have placed orders remain in the customers
table.
¡Ahí está! You just learned how to effectively use the EXISTS
operator in SQL.
Conclusion
The SQL EXISTS
operator is a powerful tool in SQL that excels at checking for the presence of related records. When used appropriately, it can lead to more readable and efficient queries compared to alternatives like IN
.
Remember that while EXISTS
is useful, it is not always the best choice. Consider your specific use case, data volume, and performance requirements when deciding whether to use EXISTS
or an alternative approach. With proper implementation and understanding of its strengths and limitations, and a formidable database client like DbVisualizer, EXISTS
can be an invaluable tool in your SQL toolkit. Try DbVisualizer for free today!
FAQ
How does EXISTS differ from IN in SQL?
Does the SQL EXISTS operator check actual values inside the subquery?
No, EXISTS
only checks for the existence of rows, not the actual values. That is why it is common to use SELECT 1
or SELECT NULL
in the subquery, as the specific value does not matter.