Joins · Problem 43 of 118

Products with Category and Supplier

EasyJoinsShop data

For products 1 to 6, return product_id, the product name, the category name and the supplier name, sorted by product_id.

Hint 1

categories.name and suppliers.name are both called name, so give them aliases in the select list.

Reference solution

SELECT p.product_id,
       p.name,
       c.name AS category,
       s.name AS supplier
FROM products AS p
JOIN categories AS c ON c.category_id = p.category_id
JOIN suppliers AS s ON s.supplier_id = p.supplier_id
WHERE p.product_id BETWEEN 1 AND 6
ORDER BY p.product_id;

Chain joins one after another: each JOIN ... ON adds one more table, and each ON links the new table to one you already have. Always join on the matching key pair (supplier_id = supplier_id). Joining on the wrong pair of columns still runs, and returns plausible-looking nonsense.