Time to practice. Below are exercises using these tables. Do not just read them. Open a SQL editor and write the queries yourself. Struggling is learning.
| Table | Columns |
|---|---|
| customers | customer_id (PK), first_name, last_name, email, city, created_at |
| orders | order_id (PK), customer_id (FK), order_date, total_amount, status |
| order_items | item_id (PK), order_id (FK), product_id (FK), quantity, unit_price |
| products | product_id (PK), product_name, category, price, stock_quantity |
| payments | payment_id (PK), order_id (FK), amount, payment_method, payment_status, payment_date |
| employees | employee_id (PK), name, department, manager_id (FK -> employees) |
Write a single query that shows: customer name, total orders, total amount spent, most recent order date, and their most expensive order amount. Include customers who have never ordered (show 0 for all counts). This combines LEFT JOIN, GROUP BY, and aggregate functions.
-- Hint: Start with this structure
SELECT
c.first_name,
c.last_name,
COUNT(o.order_id) AS total_orders,
COALESCE(SUM(o.total_amount), 0) AS total_spent,
MAX(o.order_date) AS last_order_date,
COALESCE(MAX(o.total_amount), 0) AS biggest_order
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_spent DESC;If you got stuck on any exercise, go back and re-read the specific lesson. Then try again without looking at any hints. The struggle is where the learning happens.
Key Point: Practice writing JOINs and subqueries by hand. Reading queries is not the same as writing them.