Exercise Joins
Exercise Joins
We'll be using the following tables to construct queries. The customers, orders, products, and
order_details tables will serve as the basis of these exercises.
customers Table:
orders Table:
101 1 2024-01-15
102 2 2024-02-18
103 1 2024-03-12
products Table:
order_details Table:
101 201 2
102 202 1
103 201 1
103 203 1
Part 1: 10 Exercises on Two Tables
Explanation: This query retrieves the customer names and their respective order dates.
Explanation: This query retrieves products ordered by John Doe using two inner joins.
Explanation: This query selects distinct customer names from the customers table who have placed
orders.
Exercise 4: List all orders and include customers who haven't ordered
Explanation: The LEFT JOIN ensures all customers are listed, even if they haven’t placed any orders.
Exercise 6: Show product details for orders made by customer "Jane Smith"
Explanation: This retrieves product names and prices for Jane Smith's orders.
Explanation: This query returns the number of orders each customer has made.
Exercise 8: List all customers and the total number of products they ordered
Explanation: This query sums the quantities of all products ordered by each customer.
Explanation: The HAVING clause filters out customers who ordered 1 or fewer products.
Exercise 10: Retrieve customers and their orders with order dates sorted in descending order
Explanation: This query sorts the result by order dates in descending order.
Explanation: This query calculates the total amount each customer spent.
Exercise 2: List customers who have purchased products priced above $700
Explanation: This query returns customers who have ordered products priced above $700.
Explanation: This query calculates the average quantity of each product ordered.
Exercise 4: Retrieve the total quantity and amount spent on each product
Explanation: This query calculates both the total quantity and total amount spent for each product.
Explanation: This query lists customers who ordered the 'Laptop' product.
Explanation: This query finds the product ordered the most frequently.
Explanation: This query shows which employees handled orders for each customer.
Exercise 8: List all products and the total number of customers who purchased them
Explanation: This query lists all products and how many unique customers bought them.
Exercise 9: Find customers who ordered more than 2 different products
Explanation: This query filters customers who have ordered more than two different products.
Exercise 10: Find customers who placed more than 3 orders and total amount spent
Query:
Explanation: This query returns customers who placed more than three orders along with the total
amount they spent.