SQL EXISTS Get Certified
Learn how to test whether a subquery returns at least one row using the powerful EXISTS operator.
What is the EXISTS Operator?
The EXISTS operator returns true as soon as the subquery produces one or more rows. If the subquery returns no rows at all, EXISTS returns false.
It is almost always used with a correlated subquery (a subquery that refers to a column from the outer query). Because only existence matters, the conventional form inside the subquery is SELECT 1.
Basic EXISTS Syntax
The classic pattern finds parent rows that have at least one related child row.
What this code does: Returns only those customers who have placed at least one order. Aarav and Kabir appear in the result; Meera and Riya do not.
WITH customers(customer_id, customer_name) AS (
VALUES
(1, 'Aarav'),
(2, 'Meera'),
(3, 'Kabir'),
(4, 'Riya')
),
orders(order_id, customer_id, amount) AS (
VALUES
(101, 1, 900),
(102, 1, 1200),
(103, 3, 700)
)
SELECT customer_id, customer_name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
)
ORDER BY customer_id;
EXISTS with Extra Conditions
The subquery can contain any additional filters. Only the existence of a matching row that also satisfies those filters is tested.
What this code does: Returns customers who have made at least one purchase greater than 1000. Nisha and Tara qualify; Rohan and Dev do not.
WITH customers(customer_id, customer_name) AS (
VALUES
(1, 'Nisha'),
(2, 'Rohan'),
(3, 'Tara'),
(4, 'Dev')
),
purchases(purchase_id, customer_id, amount) AS (
VALUES
(201, 1, 450),
(202, 1, 1500),
(203, 2, 600),
(204, 3, 2200)
)
SELECT customer_id, customer_name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM purchases AS p
WHERE p.customer_id = c.customer_id
AND p.amount > 1000
)
ORDER BY customer_id;
EXISTS with Employee–Project Relationship
The same pattern works for any parent–child relationship.
What this code does: Returns only employees who are assigned to at least one project. Anaya and Vikram appear; Mira and Karan do not.
WITH employees(employee_id, employee_name, department) AS (
VALUES
(1, 'Anaya', 'Design'),
(2, 'Vikram', 'Engineering'),
(3, 'Mira', 'Marketing'),
(4, 'Karan', 'Finance')
),
projects(project_id, employee_id, project_name) AS (
VALUES
(501, 1, 'Website Redesign'),
(502, 2, 'Mobile App'),
(503, 2, 'API Upgrade')
)
SELECT employee_id, employee_name, department
FROM employees AS e
WHERE EXISTS (
SELECT 1
FROM projects AS p
WHERE p.employee_id = e.employee_id
)
ORDER BY employee_id;
NOT EXISTS
NOT EXISTS is the logical opposite: it returns true when the subquery produces zero rows. It is the standard way to find “orphan” records.
What this code does: Returns customers who have never placed an order. Neha and Dev appear in the result; Ishita and Arjun do not.
WITH customers(customer_id, customer_name) AS (
VALUES
(1, 'Ishita'),
(2, 'Arjun'),
(3, 'Neha'),
(4, 'Dev')
),
orders(order_id, customer_id, amount) AS (
VALUES
(301, 1, 800),
(302, 2, 1200)
)
SELECT customer_id, customer_name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
)
ORDER BY customer_id;
EXISTS with Multiple Conditions
Any combination of conditions can be placed inside the subquery.
What this code does: Returns products that have been sold in a quantity of 10 or more. Only Mouse qualifies.
WITH products(product_id, product_name, category) AS (
VALUES
(1, 'Keyboard', 'Accessories'),
(2, 'Monitor', 'Displays'),
(3, 'Mouse', 'Accessories'),
(4, 'Webcam', 'Accessories')
),
sales(sale_id, product_id, quantity) AS (
VALUES
(401, 1, 5),
(402, 2, 2),
(403, 3, 12)
)
SELECT product_id, product_name
FROM products AS p
WHERE EXISTS (
SELECT 1
FROM sales AS s
WHERE s.product_id = p.product_id
AND s.quantity >= 10
)
ORDER BY product_id;
EXISTS across Different Entity Types
EXISTS works equally well when the related table contains completely different information.
What this code does: Returns authors who have published at least one book. Aditi Rao and Meera Das appear; Kunal Shah and Ravi Sen do not.
WITH authors(author_id, author_name) AS (
VALUES
(1, 'Aditi Rao'),
(2, 'Kunal Shah'),
(3, 'Meera Das'),
(4, 'Ravi Sen')
),
books(book_id, author_id, title) AS (
VALUES
(601, 1, 'Learning SQL'),
(602, 3, 'Database Basics')
)
SELECT author_id, author_name
FROM authors AS a
WHERE EXISTS (
SELECT 1
FROM books AS b
WHERE b.author_id = a.author_id
)
ORDER BY author_id;
EXISTS with Date Filtering
Date conditions inside the subquery are a frequent real-world requirement.
What this code does: Returns customers who made at least one visit on or after 1 August 2026. Only Tina qualifies.
WITH customers(customer_id, customer_name) AS (
VALUES
(1, 'Sana'),
(2, 'Ravi'),
(3, 'Tina'),
(4, 'Mohan')
),
visits(visit_id, customer_id, visit_date) AS (
VALUES
(701, 1, '2026-07-10'),
(702, 2, '2026-06-15'),
(703, 3, '2026-08-05')
)
SELECT customer_id, customer_name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM visits AS v
WHERE v.customer_id = c.customer_id
AND v.visit_date >= '2026-08-01'
)
ORDER BY customer_id;
EXISTS inside UPDATE
EXISTS can control which rows are modified by an UPDATE statement.
What this code does: Sets the status of an employee to “Bonus Eligible” only when a matching bonus record exists. Aarav and Kabir are updated; Meera stays “Regular”.
DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS bonuses;
CREATE TABLE employees (
employee_id INTEGER,
employee_name TEXT,
status TEXT
);
CREATE TABLE bonuses (
bonus_id INTEGER,
employee_id INTEGER,
amount INTEGER
);
INSERT INTO employees VALUES
(1, 'Aarav', 'Regular'),
(2, 'Meera', 'Regular'),
(3, 'Kabir', 'Regular');
INSERT INTO bonuses VALUES
(801, 1, 5000),
(802, 3, 3000);
UPDATE employees
SET status = 'Bonus Eligible'
WHERE EXISTS (
SELECT 1
FROM bonuses AS b
WHERE b.employee_id = employees.employee_id
);
SELECT employee_id, employee_name, status
FROM employees
ORDER BY employee_id;
EXISTS inside DELETE
The same technique works with DELETE when you need to remove rows that have a related record.
What this code does: Deletes customers whose IDs appear in the inactive_accounts table. Pooja and Dev are removed; Nikhil and Tanya remain.
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS inactive_accounts;
CREATE TABLE customers (
customer_id INTEGER,
customer_name TEXT
);
CREATE TABLE inactive_accounts (
customer_id INTEGER
);
INSERT INTO customers VALUES
(1, 'Nikhil'),
(2, 'Pooja'),
(3, 'Tanya'),
(4, 'Dev');
INSERT INTO inactive_accounts VALUES
(2),
(4);
DELETE FROM customers
WHERE EXISTS (
SELECT 1
FROM inactive_accounts AS i
WHERE i.customer_id = customers.customer_id
);
SELECT customer_id, customer_name
FROM customers
ORDER BY customer_id;
EXISTS vs IN
EXISTS vs NOT EXISTS
Advantages of EXISTS
- Expresses “does a related row exist?” in the cleanest possible way.
- Works naturally with correlated subqueries.
- Can stop as soon as the first matching row is found (often efficient).
- Supports both positive (EXISTS) and negative (NOT EXISTS) logic.
- Can be used in SELECT, UPDATE and DELETE statements.
Best Practices
- Prefer
SELECT 1(orSELECT NULL) inside the EXISTS subquery – the actual value is never used. - Always use clear table aliases so the correlation is easy to read.
- Use NOT EXISTS when you need rows that lack a related match.
- When the only question is existence, EXISTS is usually clearer (and sometimes faster) than IN.
- Keep the correlated condition in the WHERE clause of the subquery, not in the outer query.
The EXISTS operator answers a simple but powerful question:
“Does at least one related row exist?”
Together with NOT EXISTS it is the standard way to find records that do or do not have related data.
Click Test My Code on any example to run it live, or
Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.