0% found this document useful (0 votes)
5 views4 pages

Exercise Joins

The document outlines a database schema consisting of four tables: customers, orders, products, and order_details, along with a series of exercises designed to practice SQL queries. The exercises cover various operations such as retrieving customer names, product details, order counts, and total amounts spent, using different types of joins and aggregations. It includes tasks for both two-table and multi-table queries, focusing on customer orders and product sales.

Uploaded by

Dhruv Sompura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Exercise Joins

The document outlines a database schema consisting of four tables: customers, orders, products, and order_details, along with a series of exercises designed to practice SQL queries. The exercises cover various operations such as retrieving customer names, product details, order counts, and total amounts spent, using different types of joins and aggregations. It includes tasks for both two-table and multi-table queries, focusing on customer orders and product sales.

Uploaded by

Dhruv Sompura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Database Schema

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:

customer_id customer_name city

1 John Doe New York

2 Jane Smith Chicago

3 Michael Clark Miami

orders Table:

order_id customer_id order_date

101 1 2024-01-15

102 2 2024-02-18

103 1 2024-03-12

products Table:

product_id product_name price

201 Laptop 1000

202 Mobile 500

203 Tablet 700

order_details Table:

order_id product_id quantity

101 201 2

102 202 1

103 201 1

103 203 1
Part 1: 10 Exercises on Two Tables

Exercise 1: Find all orders with customer names

Explanation: This query retrieves the customer names and their respective order dates.

Exercise 2: Retrieve all products ordered by a specific customer (John Doe)

Explanation: This query retrieves products ordered by John Doe using two inner joins.

Exercise 3: Find customers who have placed orders

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 5: List all orders placed on or after February 1, 2024

Explanation: This query filters orders placed after a specific date.

Exercise 6: Show product details for orders made by customer "Jane Smith"

Explanation: This retrieves product names and prices for Jane Smith's orders.

Exercise 7: Get the order count for each customer

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.

Exercise 9: Retrieve customers who ordered more than 1 product

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.

Part 2: 10 Exercises on Two or More Tables


Exercise 1: Find the total amount spent by each customer

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.

Exercise 3: Find the average quantity of each product ordered

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.

Exercise 5: List customers who ordered a specific product (e.g., 'Laptop')

Explanation: This query lists customers who ordered the 'Laptop' product.

Exercise 6: Find the most frequently ordered product

Explanation: This query finds the product ordered the most frequently.

Exercise 7: Retrieve orders made by employees for each customer

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.

You might also like