PostgreSQL ยท Joins & aggregations
Ask whether a matching row exists
EXISTS (subquery) is true when the subquery returns at least one row. It is false when it
returns none. A correlated subquery can refer to the current outer row.
SELECT c.id
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);The result contains at most one row for each customer, even if that customer has many orders. The subquery's selected values are usually irrelevant; the question is whether a row exists.