Rewriting The Scalar Subquery
This runs the subquery once per customer row:
SELECT c.id, c.name,
(SELECT count(*) FROM orders o WHERE o.customer_id = c.id) AS n_orders
FROM customers c;
Customers with no orders must still appear, with n_orders = 0. Which rewrite produces exactly the same result set?
Sign in to answer questions and track your progress
Sign In