JOIN two tables

INNER JOIN returns only matching rows. LEFT JOIN keeps every row from the first table, even without a match.

SQL · JOIN two tablesBeginner
-- Orders with the customer name
SELECT o.id, o.total, c.name
FROM orders AS o
INNER JOIN customers AS c ON c.id = o.customer_id;

-- All customers, including those who never ordered
SELECT c.name, COUNT(o.id) AS orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;
joinmysqlbasics