-- Getting Fancier (Inner Joins Still)
SELECT first_name, last_name, order_date, amount
FROM customers
JOIN orders
ON customers.id = orders.customer_id
ORDER BY order_date;SELECT
first_name,
last_name,
SUM(amount) AS total_spent
FROM customers
JOIN orders
ON customers.id = orders.customer_id
GROUP BY orders.customer_id
ORDER BY total_spent DESC;Note: please see here for an animated visual of how left/right joins work.
-- LEFT JOINS
SELECT * FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id;SELECT first_name, last_name, order_date, amount
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id; SELECT
first_name,
last_name,
IFNULL(SUM(amount), 0) AS total_spent
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id
GROUP BY customers.id
ORDER BY total_spent;