Sub topic 15
Choosing Between Subqueries and Joins
Assume you know basic SELECT, WHERE, GROUP BY, joins, and aggregate functions such as AVG and COUNT. This chapter focuses on choosing a clear technique. A subquery is a query nested inside another query. A join combines rows from two or more row sources.
Start with the question
Choose the form that matches the question:
- Use a join when you need columns from both row sources.
- Use
EXISTSwhen you only need to know whether a matching row exists. - Use a scalar subquery when one value belongs beside each outer row.
- Use an inline view or
WITHquery when you need to calculate a result set before using it. - Use
INwhen membership in a set expresses the requirement clearly.
For example, these queries return departments that have at least one employee earning more than 10,000:
-- EXISTS states the question directly.
SELECT d.department_name
FROM departments d
WHERE EXISTS (
SELECT 1
FROM employees e
WHERE e.department_id = d.department_id
AND e.salary > 10000
);
-- A join also works, but DISTINCT is needed if several employees match.
SELECT DISTINCT d.department_name
FROM departments d
JOIN employees e
ON e.department_id = d.department_id
WHERE e.salary > 10000;EXISTS avoids duplicate department rows because it tests existence rather than returning matching employee rows. The join is preferable if you also need employee columns.
Compare clarity and row behavior
A common mistake is choosing a join when the requirement is only a yes-or-no test. Another is choosing a scalar subquery when a join would return several useful columns.
| Requirement | Often clearest choice | Reason |
|---|---|---|
| Department has an employee | `EXISTS` | Tests presence without duplicates |
| Show department and employee data | `JOIN` | Returns columns from both tables |
| Show each employee with department average | `JOIN` with `WITH` | Calculates once and exposes the value |
| Compare with one company-wide value | Scalar subquery | Expresses one value beside the outer query |
This WITH query names an intermediate result and joins it to employees:
WITH dept_avg AS (
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
)
SELECT e.last_name,
e.salary,
a.avg_salary
FROM employees e
JOIN dept_avg a
ON a.department_id = e.department_id
WHERE e.salary > a.avg_salary;The same logic can use a correlated subquery:
SELECT e.last_name, e.salary
FROM employees e
WHERE e.salary > (
SELECT AVG(i.salary)
FROM employees i
WHERE i.department_id = e.department_id
);The correlated version is compact. The WITH version makes the intermediate data easier to inspect and reuse.
Performance and optimizer behavior
Do not assume that joins are always faster. Oracle can transform some subqueries into joins internally. This process is called subquery unnesting. The optimizer considers indexes, table statistics, predicates, grouping, and estimated row counts.
Write the clearest correct query first. Then inspect the execution plan when performance matters:
EXPLAIN PLAN FOR
SELECT d.department_name
FROM departments d
WHERE EXISTS (
SELECT 1
FROM employees e
WHERE e.department_id = d.department_id
);
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);A plan can show whether Oracle uses a nested loops join, hash join, semi-join, or another operation. A semi-join is useful for existence tests because it does not need to return every matching inner row.
Exercise
Which technique is clearest for listing departments that have no employees, without returning duplicate departments?
Show answer
Use NOT EXISTS with a correlated subquery. It tests that no matching employee row exists and avoids row multiplication.