Sub topic 8
EXISTS and NOT EXISTS in Oracle SQL
This chapter assumes that you know SELECT, WHERE, joins, aggregate functions, and the Oracle HR sample schema. You will use that knowledge to test whether related rows exist without returning their values.
What EXISTS tests
EXISTS is a condition that is true when its subquery returns at least one row. The database does not use the selected values inside the subquery. For that reason, SELECT 1 is a common convention.
The outer query supplies a row to the subquery through a correlated condition. In this example, Oracle checks each department and asks whether at least one employee belongs to it:
SELECT d.department_id, d.department_name
FROM departments d
WHERE EXISTS (
SELECT 1
FROM employees e
WHERE e.department_id = d.department_id
)
ORDER BY d.department_id;The inner query refers to d.department_id, a column from the outer query. This makes it a correlated subquery. For each department, EXISTS becomes true as soon as Oracle finds a matching employee.
The selected expression does not affect the result:
-- These conditions have the same meaning:
WHERE EXISTS (SELECT 1 FROM employees e
WHERE e.department_id = d.department_id)
WHERE EXISTS (SELECT e.last_name FROM employees e
WHERE e.department_id = d.department_id)EXISTS is useful when you need to test a relationship but do not need columns from the matching table.
What NOT EXISTS tests
NOT EXISTS is true when the subquery returns no rows. This query finds departments without employees:
SELECT d.department_id, d.department_name
FROM departments d
WHERE NOT EXISTS (
SELECT 1
FROM employees e
WHERE e.department_id = d.department_id
)
ORDER BY d.department_id;The condition is evaluated independently for each department.
EXISTS compared with IN
You can often express an EXISTS condition with IN:
SELECT d.department_name
FROM departments d
WHERE d.department_id IN (
SELECT e.department_id
FROM employees e
);IN compares a value with a set of returned values. EXISTS tests whether a matching row can be found. EXISTS is often clearer when the relationship uses several columns or when you only care whether a row exists.
For example, list employees who manage at least one other employee:
SELECT e.employee_id, e.last_name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM employees report
WHERE report.manager_id = e.employee_id
);NULL and NOT IN
NOT EXISTS is usually safer than NOT IN when the subquery column can contain NULL. A NULL means unknown, not a value that is different from every other value.
For example, employees who manage nobody can be found safely with NOT EXISTS:
SELECT e.employee_id, e.last_name
FROM employees e
WHERE NOT EXISTS (
SELECT 1
FROM employees report
WHERE report.manager_id = e.employee_id
);Oracle's optimizer may transform EXISTS, IN, and joins into similar execution plans. Choose the form that states the business rule most clearly, then inspect the plan when performance matters.
Exercise
Write a query that returns departments having at least one employee whose salary is greater than 10000.
Show answer
SELECT d.departmentname FROM departments d WHERE EXISTS ( SELECT 1 FROM employees e WHERE e.departmentid = d.department_id AND e.salary > 10000 );