Learn Without Walls
← Back to SQL Labs
← Previous Lab Lab 6 of 10 — JOINs Next Lab →

Lab 06 — JOINs: Combining Tables

The most powerful concept in SQL. Link customers, orders, and products together into one result set.

⏳ Loading SQL engine...
📋 AVAILABLE TABLES: products, customers, ordersproducts(id, name, category, price, stock) customers(id, name, city, email, member_since) orders(id, customer_id, product_id, quantity, order_date, total) Key relationships: orders.customer_id --> customers.id orders.product_id --> products.id

📖 Concept Recap: JOINs

👀 Worked Example (read-only)

Full order history joining all three tables at once.

SELECT o.id AS order_id,
       c.name AS customer,
       c.city,
       p.name AS product,
       p.category,
       o.quantity,
       o.total,
       o.order_date
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN products p ON o.product_id = p.id
ORDER BY o.order_date;
✏ Guided

Exercise 1 — Basic INNER JOIN

Show each order with the customer name and product name. Fill in the blanks: the correct column names and JOIN conditions.

💡 Hint: o = orders alias, c = customers alias, p = products alias. The linking columns are customer_id and product_id.
💪 Independent

Exercise 2 — LEFT JOIN: All Customers Including Those with No Orders

Show all customers with their total amount spent. Customers who have never placed an order should appear with 0 total (use COALESCE). Sort by total spent descending.

💡 Hint: Start FROM customers c LEFT JOIN orders o ON c.id = o.customer_id, then GROUP BY and use COALESCE(SUM(o.total), 0).
🔥 Challenge

Exercise 3 — Products That Have Never Been Ordered

Use a LEFT JOIN from products to orders, then filter WHERE orders.id IS NULL to find products with no order history. These are dead-stock items.

💡 Hint: FROM products p LEFT JOIN orders o ON p.id = o.product_id WHERE o.id IS NULL
🏆 Mini Project

Mini Project — Complete Order Intelligence Report

As a business analyst, write 4 JOIN queries to understand purchasing behaviour fully:

  1. Full order history: order ID, customer name, product name, category, quantity, total, date.
  2. Customer purchase summary: name, city, number of orders, total spent — sorted by total DESC.
  3. Product popularity: product name, category, times ordered, total quantity sold.
  4. Which customers bought which categories: customer name, category, total spent in that category.

🏁 Lab 06 Complete!

You can now combine multiple tables using INNER JOIN and LEFT JOIN, use table aliases, and find rows with no matching records using the IS NULL trick.

Next up: queries inside queries.

Continue to Lab 07 — Subqueries →