Sub topic 12
Subquery or Join? Choosing the Clearer Form
Assume you know SELECT, WHERE, aggregate functions, GROUP BY, INNER JOIN, LEFT JOIN, NULL, and three-valued logic. This chapter focuses on choosing the clearest way to express the same question.
A subquery is a query nested inside another query. A join combines rows from two or more tables. Both can answer questions about related data, but they communicate different intentions.
Start with the question
Use a subquery when the inner query produces a value or tests whether related rows exist. Use a join when you need columns from the related table or need to combine rows directly.
This table summarizes the usual choices:
| Question | Clear starting point | Reason |
|---|---|---|
| Which customers have at least one order? | `EXISTS` | You only test for a match |
| Which customers have order details? | `JOIN` | You need columns from `orders` |
| Which employees earn above their department average? | CTE or derived table | You calculate a reusable result set |
| What is each customer's order count? | Join with grouping or scalar subquery | Both can produce one result per customer |
Use `EXISTS` when you only need a yes-or-no test
Suppose the result should contain each customer once, regardless of how many orders that customer has. EXISTS expresses that intention directly:
SELECT c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);The subquery checks whether at least one matching order exists. The value 1 is conventional because the selected value is not used.
A join can produce the same customer more than once:
SELECT c.name
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id;If a customer has three orders, this query returns three rows for that customer. You can add DISTINCT, but that hides the duplication instead of expressing the original existence test. Prefer EXISTS when existence is the real requirement.
NOT EXISTS is similarly useful for finding customers with no orders. It also avoids the NULL behavior that can make NOT IN surprising.
Use a join when related columns belong in the result
If you need the order identifier and total, a join is usually clearer:
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id;The join describes the rows that should be combined. It naturally returns one result row for each matching order.
A subquery in the SELECT list may be clearer when you need one calculated value per outer row:
SELECT c.name,
(
SELECT COUNT(*)
FROM orders AS o
WHERE o.customer_id = c.id
) AS order_count
FROM customers AS c;For large result sets, a grouped join may be easier for the database to optimize and easier to extend:
SELECT c.name, COUNT(o.id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;The LEFT JOIN keeps customers whose count is zero.
Compare plans when performance matters
A subquery and a join can be transformed into similar execution plans. SQL describes the result, while the database optimizer chooses the execution strategy. Do not assume that one syntax is always faster.
Check the actual plan with your database's plan command:
EXPLAIN
SELECT c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);Look for full table scans, inefficient joins, and missing indexes. An index on orders.customer_id often helps both this EXISTS query and a join. Test with realistic data before changing a query only for performance.
Exercise
You need the names of customers who have at least one order above 100, but you must return each customer only once. Should you use a join or EXISTS?
Show answer
Use EXISTS. It tests for a matching order without multiplying a customer row when that customer has several qualifying orders.