-- 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;
-- 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;
No comments:
Post a Comment