What does an INNER JOIN between orders and customers on orders.customer_id = customers.id return?
Options
- Every order and every customer, whether they match or not
- Only orders that have a matching customer, with columns of both (Correct option)
- Only the customers, without any of the data from the orders
- Only the number of orders that exist in the orders table
Answer
Only the rows where an order has a matching customer, combining columns from both tables
Why
A JOIN combines rows from two tables using a matching rule, so you can see order data and customer data in the same row.
INNER JOIN keeps only pairs that match on both sides. Orders without a customer, or customers without orders, are left out.
Use LEFT JOIN when you want to keep all rows from the first table.
SELECT orders.id, customers.name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;Remember
INNER JOIN = only matching pairs.