Why Does a SQL LEFT JOIN Return More Rows Than the Left Table?

Asked 6 days ago 84 views

0

I expected this query to return one row per customer, but customers with several orders show up multiple times. Is that just how a one-to-many relationship behaves in a LEFT JOIN, or could the join condition be wrong?

SELECT
    customers.customer_id,
    customers.name,
    orders.order_id
FROM customers
LEFT JOIN orders
    ON orders.customer_id = customers.customer_id;

-- A customer with multiple matching orders appears once per order.

When you need one row per customer, how do you decide whether to aggregate the orders, filter them, or change the query? I’m also curious about a reliable way to spot which matches are increasing the LEFT JOIN row counts.

0 Answers


Write Your Answer