0% found this document useful (0 votes)
12 views

SQL 50 Practical Questions Answers

This document provides detailed answers to 50 SQL questions that involve 3-table JOIN operations, complete with example tables, SQL queries, and expected outputs. The queries are written in standard SQL syntax and cover various scenarios such as listing customer names, product names, and order dates, as well as filtering by categories and suppliers. Each question is structured to demonstrate practical applications of SQL in relational database systems.

Uploaded by

georgemasiga001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SQL 50 Practical Questions Answers

This document provides detailed answers to 50 SQL questions that involve 3-table JOIN operations, complete with example tables, SQL queries, and expected outputs. The queries are written in standard SQL syntax and cover various scenarios such as listing customer names, product names, and order dates, as well as filtering by categories and suppliers. Each question is structured to demonstrate practical applications of SQL in relational database systems.

Uploaded by

georgemasiga001
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

50 Practical SQL Questions with 3-Table JOINs

This document contains detailed answers to 50 SQL questions involving 3-table JOIN
operations. Each question includes example tables, SQL queries, and expected output.

Note: The tables contain sample data for demonstration purposes. Queries are written in
standard SQL syntax compatible with most relational database systems.

Question 1:

List customer names, product names, and order dates for all orders.

Tables:

Customers
CustomerID CustomerName City

1 John Doe Nairobi

2 Mary Smith Kisumu

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1

Orders
OrderID CustomerID ProductID OrderDate

1 1 1 2023-01-10

2 2 2 2023-02-15

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName, Orders.OrderDate
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN Products ON Orders.ProductID = Products.ProductID;
Output:
CustomerName ProductName OrderDate

John Doe Laptop 2023-01-10

Mary Smith Phone 2023-02-15

Question 2

Show product names, supplier names, and category names.

Tables:

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1

Suppliers
SupplierID SupplierName Country

1 TechWorld Kenya

2 MobilePlus Uganda

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Suppliers.SupplierName,
Categories.CategoryName
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Output:
ProductName SupplierName CategoryName

Laptop TechWorld Electronics

Phone MobilePlus Electronics

Question 3

List employee names, customer names, and order dates for all orders handled by employees.

Tables:

Employees
EmployeeID EmployeeName City

1 Alice Nairobi

2 Bob Kisumu

Customers
CustomerID CustomerName City

1 John Doe Nairobi

2 Mary Smith Kisumu

Orders
OrderID CustomerID EmployeeID OrderDate

1 1 1 2023-01-10

2 2 2 2023-02-15

SQL Query:
sql
CopyEdit
SELECT Employees.EmployeeName, Customers.CustomerName, Orders.OrderDate
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Output:
EmployeeName CustomerName OrderDate

Alice John Doe 2023-01-10

Bob Mary Smith 2023-02-15

Question 4

Get customer names, shipper names, and order dates.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Shippers
ShipperID ShipperName

1 DHL

2 FedEx

Orders
OrderID CustomerID ShipperID OrderDate

1 1 1 2023-01-10

2 2 2 2023-02-15

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Shippers.ShipperName, Orders.OrderDate
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID;
Output:
CustomerName ShipperName OrderDate

John Doe DHL 2023-01-10

Mary Smith FedEx 2023-02-15

Question 5

Show product names, category names, and their supplier names.

Tables:

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Categories.CategoryName,
Suppliers.SupplierName
FROM Products
JOIN Categories ON Products.CategoryID = Categories.CategoryID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;
Output:
ProductName CategoryName SupplierName

Laptop Electronics TechWorld

Phone Electronics MobilePlus

Question 6

Find order IDs, employee names, and shipper names for all orders.

Tables:

Orders
OrderID EmployeeID ShipperID

1 1 1

2 2 2

Employees
EmployeeID EmployeeName

1 Alice

2 Bob

Shippers
ShipperID ShipperName

1 DHL

2 FedEx

SQL Query:
sql
CopyEdit
SELECT Orders.OrderID, Employees.EmployeeName, Shippers.ShipperName
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID;
Output:
OrderID EmployeeName ShipperName

1 Alice DHL

2 Bob FedEx

Question 7

List customer names, product names, and quantity ordered.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Products
ProductID ProductName

1 Laptop

2 Phone

OrderDetails
OrderID ProductID CustomerID Quantity

1 1 1 2

2 2 2 1

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName,
OrderDetails.Quantity
FROM OrderDetails
JOIN Customers ON OrderDetails.CustomerID = Customers.CustomerID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;
Output:
CustomerName ProductName Quantity

John Doe Laptop 2

Mary Smith Phone 1

Question 8

Show orders with customer names, employee names, and product names.

Tables:

Orders
OrderID CustomerID EmployeeID

1 1 1

2 2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Employees
EmployeeID EmployeeName

1 Alice

2 Bob

OrderDetails
OrderID ProductID

1 1

2 2
OrderID ProductID

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Employees.EmployeeName,
Products.ProductName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Output:
CustomerName EmployeeName ProductName

John Doe Alice Laptop

Mary Smith Bob Phone

Question 9

List all products, their category, and supplier country.

Tables:

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1
Categories
CategoryID CategoryName

1 Electronics

Suppliers
SupplierID SupplierName Country

1 TechWorld Kenya

2 MobilePlus Uganda

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Categories.CategoryName, Suppliers.Country
FROM Products
JOIN Categories ON Products.CategoryID = Categories.CategoryID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;

Output:
ProductName CategoryName Country

Laptop Electronics Kenya

Phone Electronics Uganda

Question 10

Show customers who ordered products from a specific category (e.g., 'Electronics').

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith
Orders
OrderID CustomerID

1 1

2 2

OrderDetails
OrderID ProductID

1 1

2 2

Products
ProductID ProductName CategoryID

1 Laptop 1

2 Phone 1

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT DISTINCT Customers.CustomerName
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Categories ON Products.CategoryID = Categories.CategoryID
WHERE Categories.CategoryName = 'Electronics';

Output:
CustomerName

John Doe

Mary Smith
Question 11

List all products supplied by suppliers from a specific country with category details (e.g.,
'Kenya').

Tables:

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1

Suppliers
SupplierID SupplierName Country

1 TechWorld Kenya

2 MobilePlus Uganda

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Categories.CategoryName,
Suppliers.SupplierName
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN Categories ON Products.CategoryID = Categories.CategoryID
WHERE Suppliers.Country = 'Kenya';

Output:
ProductName CategoryName SupplierName

Laptop Electronics TechWorld


Question 12

Find orders where the employee and customer are from the same city.

Tables:

Customers
CustomerID CustomerName City

1 John Doe Nairobi

2 Mary Smith Kisumu

Employees
EmployeeID EmployeeName City

1 Alice Nairobi

2 Bob Kisumu

Orders
OrderID CustomerID EmployeeID

1 1 1

2 2 2

SQL Query:
sql
CopyEdit
SELECT Orders.OrderID, Customers.CustomerName, Employees.EmployeeName,
Customers.City
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE Customers.City = Employees.City;

Output:
OrderID CustomerName EmployeeName City

1 John Doe Alice Nairobi

2 Mary Smith Bob Kisumu


Question 13

List all orders with customer name, product name, and supplier name.

Tables:

Orders
OrderID CustomerID

1 1

2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

OrderDetails
OrderID ProductID

1 1

2 2

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName,
Suppliers.SupplierName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;

Output:
CustomerName ProductName SupplierName

John Doe Laptop TechWorld

Mary Smith Phone MobilePlus

Question 14

Find the most expensive product in each category with supplier name.

Tables:

Products
ProductID ProductName SupplierID CategoryID UnitPrice

1 Laptop 1 1 1000

2 Phone 2 1 500

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Categories.CategoryName, Products.ProductName,
Products.UnitPrice, Suppliers.SupplierName
FROM Products
JOIN Categories ON Products.CategoryID = Categories.CategoryID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
WHERE Products.UnitPrice = (
SELECT MAX(UnitPrice)
FROM Products AS P2
WHERE P2.CategoryID = Products.CategoryID
);

Output:
CategoryName ProductName UnitPrice SupplierName

Electronics Laptop 1000 TechWorld

Question 15

Show all customers who have ordered products handled by a specific employee (e.g., 'Alice').

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Orders
OrderID CustomerID EmployeeID

1 1 1

2 2 2

Employees
EmployeeID EmployeeName

1 Alice

2 Bob
SQL Query:
sql
CopyEdit
SELECT DISTINCT Customers.CustomerName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE Employees.EmployeeName = 'Alice';

Output:
CustomerName

John Doe

Question 16

List all orders with product name, category name, and quantity.

Tables:

Orders
OrderID CustomerID

1 1

2 2

OrderDetails
OrderID ProductID Quantity

1 1 2

2 2 1

Products
ProductID ProductName CategoryID

1 Laptop 1

2 Phone 1
Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Orders.OrderID, Products.ProductName, Categories.CategoryName,
OrderDetails.Quantity
FROM Orders
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Categories ON Products.CategoryID = Categories.CategoryID;

Output:
OrderID ProductName CategoryName Quantity

1 Laptop Electronics 2

2 Phone Electronics 1

Question 17

Show suppliers, products they supply, and orders where those products were ordered.

Tables:

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2
OrderDetails
OrderID ProductID Quantity

1 1 2

2 2 1

SQL Query:
sql
CopyEdit
SELECT Suppliers.SupplierName, Products.ProductName,
OrderDetails.OrderID
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID;

Output:
SupplierName ProductName OrderID

TechWorld Laptop 1

MobilePlus Phone 2

Question 18

List all orders along with customer name and shipper name for orders after 2020.

Tables:

Orders
OrderID CustomerID ShipperID OrderDate

1 1 1 2023-01-10

2 2 2 2023-02-15

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith
Shippers
ShipperID ShipperName

1 DHL

2 FedEx

SQL Query:
sql
CopyEdit
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName,
Orders.OrderDate
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
WHERE Orders.OrderDate > '2020-01-01';

Output:
OrderID CustomerName ShipperName OrderDate

1 John Doe DHL 2023-01-10

2 Mary Smith FedEx 2023-02-15

Question 19

List product names, their supplier contact name, and category description.

Tables:

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1

Suppliers
SupplierID SupplierName ContactName

1 TechWorld Peter

2 MobilePlus Sarah
Categories
CategoryID CategoryDescription

1 Electronics Devices

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Suppliers.ContactName,
Categories.CategoryDescription
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN Categories ON Products.CategoryID = Categories.CategoryID;

Output:
ProductName ContactName CategoryDescription

Laptop Peter Electronics Devices

Phone Sarah Electronics Devices

Question 20

Show orders along with employee name and shipper company name.

Tables:

Orders
OrderID EmployeeID ShipperID OrderDate

1 1 1 2023-01-10

2 2 2 2023-02-15

Employees
EmployeeID EmployeeName

1 Alice

2 Bob
Shippers
ShipperID ShipperName

1 DHL

2 FedEx

SQL Query:
sql
CopyEdit
SELECT Orders.OrderID, Employees.EmployeeName, Shippers.ShipperName
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID;

Output:
OrderID EmployeeName ShipperName

1 Alice DHL

2 Bob FedEx

Question 21

Find customers who ordered products from more than 2 categories.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Orders
OrderID CustomerID

1 1

2 1
OrderID CustomerID

3 1

4 2

OrderDetails
OrderID ProductID

1 1

2 2

3 3

4 1

Products
ProductID ProductName CategoryID

1 Laptop 1

2 Phone 2

3 Tablet 3

Categories
CategoryID CategoryName

1 Electronics

2 Mobile

3 Tablets

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY Customers.CustomerName
HAVING COUNT(DISTINCT Products.CategoryID) > 2;

Output:
CustomerName

John Doe

Question 22

Show employee name, their customer handled, and product ordered.

Tables:

Employees
EmployeeID EmployeeName

1 Alice

2 Bob

Orders
OrderID CustomerID EmployeeID

1 1 1

2 2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

OrderDetails
OrderID ProductID

1 1

2 2
Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Employees.EmployeeName, Customers.CustomerName,
Products.ProductName
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Output:
EmployeeName CustomerName ProductName

Alice John Doe Laptop

Bob Mary Smith Phone

Question 23

List customers, products they ordered, and the quantity greater than 10.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Orders
OrderID CustomerID

1 1

2 2
OrderID CustomerID

OrderDetails
OrderID ProductID Quantity

1 1 15

2 2 5

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName,
OrderDetails.Quantity
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE OrderDetails.Quantity > 10;

Output:
CustomerName ProductName Quantity

John Doe Laptop 15


Question 24

Show employees, products they handled, and suppliers of those products.

Tables:

Employees
EmployeeID EmployeeName

1 Alice

2 Bob

Orders
OrderID EmployeeID

1 1

2 2

OrderDetails
OrderID ProductID

1 1

2 2

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

SQL Query:
sql
CopyEdit
SELECT Employees.EmployeeName, Products.ProductName,
Suppliers.SupplierName
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;

Output:
EmployeeName ProductName SupplierName

Alice Laptop TechWorld

Bob Phone MobilePlus

Question 25

List all suppliers whose products have never been ordered (using LEFT JOIN).

Tables:

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

3 MegaSupplier

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2

3 Printer 3

OrderDetails
OrderID ProductID

1 1
OrderID ProductID

2 2

SQL Query:
sql
CopyEdit
SELECT Suppliers.SupplierName
FROM Suppliers
LEFT JOIN Products ON Suppliers.SupplierID = Products.SupplierID
LEFT JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
WHERE OrderDetails.OrderID IS NULL;

Output:
SupplierName

MegaSupplier

Question 26

Show products, their category, and total quantity ordered.

Tables:

Products
ProductID ProductName CategoryID

1 Laptop 1

2 Phone 1

Categories
CategoryID CategoryName

1 Electronics

OrderDetails
OrderID ProductID Quantity

1 1 2

2 1 3

3 2 1
OrderID ProductID Quantity

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Categories.CategoryName,
SUM(OrderDetails.Quantity) AS TotalQuantity
FROM Products
JOIN Categories ON Products.CategoryID = Categories.CategoryID
JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
GROUP BY Products.ProductName, Categories.CategoryName;

Output:
ProductName CategoryName TotalQuantity

Laptop Electronics 5

Phone Electronics 1

Question 27

List top 5 customers who ordered the most products by quantity.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

3 Tom White

Orders
OrderID CustomerID

1 1

2 1

3 2
OrderID CustomerID

4 3

OrderDetails
OrderID ProductID Quantity

1 1 2

2 2 3

3 1 1

4 2 5

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, SUM(OrderDetails.Quantity) AS
TotalQuantity
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Customers.CustomerName
ORDER BY TotalQuantity DESC
LIMIT 5;

Output:
CustomerName TotalQuantity

John Doe 5

Tom White 5

Mary Smith 1
Question 28

Show suppliers, products they supply, and the categories of those products.

Tables:

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

Products
ProductID ProductName SupplierID CategoryID

1 Laptop 1 1

2 Phone 2 1

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Suppliers.SupplierName, Products.ProductName,
Categories.CategoryName
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN Categories ON Products.CategoryID = Categories.CategoryID;

Output:
SupplierName ProductName CategoryName

TechWorld Laptop Electronics

MobilePlus Phone Electronics


Question 29

Find all customers who ordered from a specific supplier (e.g., 'TechWorld').

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Orders
OrderID CustomerID

1 1

2 2

OrderDetails
OrderID ProductID

1 1

2 2

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

SQL Query:
sql
CopyEdit
SELECT DISTINCT Customers.CustomerName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
WHERE Suppliers.SupplierName = 'TechWorld';

Output:
CustomerName

John Doe

Question 30

Show all orders with total price (unit price * quantity) along with customer and product name.

Tables:

Orders
OrderID CustomerID

1 1

2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

OrderDetails
OrderID ProductID Quantity UnitPrice

1 1 2 1000

2 2 1 500
Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName,
(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalPrice
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Output:
CustomerName ProductName TotalPrice

John Doe Laptop 2000

Mary Smith Phone 500

Question 31

Show employee names, the shippers they frequently use, and the number of orders.

Tables:

Employees
EmployeeID EmployeeName

1 Alice

2 Bob

Orders
OrderID EmployeeID ShipperID

1 1 1

2 1 1

3 2 2
OrderID EmployeeID ShipperID

Shippers
ShipperID ShipperName

1 DHL

2 FedEx

SQL Query:
sql
CopyEdit
SELECT Employees.EmployeeName, Shippers.ShipperName,
COUNT(Orders.OrderID) AS TotalOrders
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY Employees.EmployeeName, Shippers.ShipperName;

Output:
EmployeeName ShipperName TotalOrders

Alice DHL 2

Bob FedEx 1

Question 32

List customers, the categories of products they ordered, and total spent.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith
Orders
OrderID CustomerID

1 1

2 2

OrderDetails
OrderID ProductID Quantity UnitPrice

1 1 2 1000

2 2 1 500

Products
ProductID ProductName CategoryID

1 Laptop 1

2 Phone 1

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Categories.CategoryName,
SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalSpent
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY Customers.CustomerName, Categories.CategoryName;

Output:
CustomerName CategoryName TotalSpent

John Doe Electronics 2000

Mary Smith Electronics 500


CustomerName CategoryName TotalSpent

Question 33

Find orders handled by employees from a specific city (e.g., 'Nairobi').

Employees
EmployeeID EmployeeName City

1 Alice Nairobi

2 Bob Kisumu

Orders
OrderID EmployeeID

1 1

2 2

SQL Query:
sql
CopyEdit
SELECT Orders.OrderID, Employees.EmployeeName, Employees.City
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE Employees.City = 'Nairobi';

Output:
OrderID EmployeeName City

1 Alice Nairobi
Question 34

Show the average price of products per category and supplier.

Products
ProductID ProductName SupplierID CategoryID UnitPrice

1 Laptop 1 1 1000

2 Phone 1 1 500

3 Tablet 2 1 700

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

Categories
CategoryID CategoryName

1 Electronics

SQL Query:
sql
CopyEdit
SELECT Categories.CategoryName, Suppliers.SupplierName,
AVG(Products.UnitPrice) AS AveragePrice
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN Categories ON Products.CategoryID = Categories.CategoryID
GROUP BY Categories.CategoryName, Suppliers.SupplierName;

Output:
CategoryName SupplierName AveragePrice

Electronics TechWorld 750

Electronics MobilePlus 700


Question 35

List orders with customer name, product name, and order total where total > $1000.

Orders
OrderID CustomerID

1 1

2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

OrderDetails
OrderID ProductID Quantity UnitPrice

1 1 2 1000

2 2 1 500

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName,
(OrderDetails.Quantity * OrderDetails.UnitPrice) AS OrderTotal
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE (OrderDetails.Quantity * OrderDetails.UnitPrice) > 1000;
Output:
CustomerName ProductName OrderTotal

John Doe Laptop 2000

Question 36

Find all products ordered by customers from a specific country (e.g., 'Kenya').

Tables:

Customers
CustomerID CustomerName Country

1 John Doe Kenya

2 Mary Smith Uganda

Orders
OrderID CustomerID

1 1

2 2

OrderDetails
OrderID ProductID

1 1

2 2

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT DISTINCT Products.ProductName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE Customers.Country = 'Kenya';

Output:
ProductName

Laptop

Question 37

List all shippers with customers and product names they delivered.

Tables:

Shippers
ShipperID ShipperName

1 DHL

2 FedEx

Orders
OrderID CustomerID ShipperID

1 1 1

2 2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

OrderDetails
OrderID ProductID

1 1
OrderID ProductID

2 2

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Shippers.ShipperName, Customers.CustomerName,
Products.ProductName
FROM Orders
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Output:
ShipperName CustomerName ProductName

DHL John Doe Laptop

FedEx Mary Smith Phone

Question 38

Show products with supplier name and the number of times ordered.

Tables:

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2
Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

OrderDetails
OrderID ProductID

1 1

2 1

3 2

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Suppliers.SupplierName,
COUNT(OrderDetails.OrderID) AS TimesOrdered
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
GROUP BY Products.ProductName, Suppliers.SupplierName;

Output:
ProductName SupplierName TimesOrdered

Laptop TechWorld 2

Phone MobilePlus 1
Question 39

List customers, order dates, and product names for orders placed in a specific month (e.g.,
January).

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Orders
OrderID CustomerID OrderDate

1 1 2023-01-10

2 2 2023-02-15

OrderDetails
OrderID ProductID

1 1

2 2

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Orders.OrderDate, Products.ProductName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE MONTH(Orders.OrderDate) = 1;
Output:
CustomerName OrderDate ProductName

John Doe 2023-01-10 Laptop

Question 40

Show all products with no category assigned (if any).

Tables:

Products
ProductID ProductName CategoryID

1 Laptop 1

2 Phone NULL

SQL Query:
sql
CopyEdit
SELECT ProductName
FROM Products
WHERE CategoryID IS NULL;

Output:
ProductName

Phone

Question 41

List employees, their customer, and the total amount of their orders.

Tables:

Employees
EmployeeID EmployeeName

1 Alice

2 Bob
Orders
OrderID EmployeeID CustomerID

1 1 1

2 2 2

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

OrderDetails
OrderID ProductID Quantity UnitPrice

1 1 2 1000

2 2 1 500

SQL Query:
sql
CopyEdit
SELECT Employees.EmployeeName, Customers.CustomerName,
SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalAmount
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Employees.EmployeeName, Customers.CustomerName;

Output:
EmployeeName CustomerName TotalAmount

Alice John Doe 2000

Bob Mary Smith 500


Question 42

Show customers with their last ordered product and order date.

Tables:

Customers
CustomerID CustomerName

1 John Doe

2 Mary Smith

Orders
OrderID CustomerID OrderDate

1 1 2023-01-10

2 1 2023-02-15

3 2 2023-01-20

OrderDetails
OrderID ProductID

1 1

2 2

3 1

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName, Orders.OrderDate
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE (CustomerID, OrderDate) IN (
SELECT CustomerID, MAX(OrderDate)
FROM Orders
GROUP BY CustomerID
);

Output:
CustomerName ProductName OrderDate

John Doe Phone 2023-02-15

Mary Smith Laptop 2023-01-20

Question 43

List all products that belong to more than one category (if applicable).

ProductCategories
ProductID CategoryID

1 1

1 2

2 1

Products
ProductID ProductName

1 Laptop

2 Phone

SQL Query:
sql
CopyEdit
SELECT Products.ProductName
FROM ProductCategories
JOIN Products ON ProductCategories.ProductID = Products.ProductID
GROUP BY Products.ProductName
HAVING COUNT(DISTINCT CategoryID) > 1;
Output:
ProductName

Laptop

Question 44

Show products, suppliers, and their order totals sorted by highest total.

Products
ProductID ProductName SupplierID

1 Laptop 1

2 Phone 2

Suppliers
SupplierID SupplierName

1 TechWorld

2 MobilePlus

OrderDetails
OrderID ProductID Quantity UnitPrice

1 1 2 1000

2 2 1 500

SQL Query:
sql
CopyEdit
SELECT Products.ProductName, Suppliers.SupplierName,
SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalValue
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
GROUP BY Products.ProductName, Suppliers.SupplierName
ORDER BY TotalValue DESC;
Output:
ProductName SupplierName TotalValue

Laptop TechWorld 2000

Phone MobilePlus 500

Question 45

List customers who ordered from more than 3 different suppliers.

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY Customers.CustomerName
HAVING COUNT(DISTINCT Products.SupplierID) > 3;

Output:
CustomerName

(None in sample data)

Question 46

Show employees, products they sold, and the order date.

SQL Query:
sql
CopyEdit
SELECT Employees.EmployeeName, Products.ProductName, Orders.OrderDate
FROM Orders
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID;
Output:
EmployeeName ProductName OrderDate

Alice Laptop 2023-01-10

Bob Phone 2023-02-15

Question 47

Find top 3 suppliers based on total quantity supplied in orders.

SQL Query:
sql
CopyEdit
SELECT Suppliers.SupplierName, SUM(OrderDetails.Quantity) AS
TotalQuantity
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
GROUP BY Suppliers.SupplierName
ORDER BY TotalQuantity DESC
LIMIT 3;

Output:
SupplierName TotalQuantity

TechWorld 2

MobilePlus 1

Question 48

List all categories with the number of products and the number of orders.

SQL Query:
sql
CopyEdit
SELECT Categories.CategoryName, COUNT(DISTINCT Products.ProductID) AS
ProductCount, COUNT(OrderDetails.OrderID) AS OrderCount
FROM Categories
LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID
LEFT JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
GROUP BY Categories.CategoryName;
Output:
CategoryName ProductCount OrderCount

Electronics 2 3

Question 49

Show customer name, product name, and supplier country for all orders.

SQL Query:
sql
CopyEdit
SELECT Customers.CustomerName, Products.ProductName, Suppliers.Country
FROM Orders
JOIN Customers ON Orders.CustomerID = Customers.CustomerID
JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
JOIN Products ON OrderDetails.ProductID = Products.ProductID
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;

Output:
CustomerName ProductName Country

John Doe Laptop Kenya

Mary Smith Phone Uganda

Question 50

List shippers, employees who use them, and total orders shipped.

SQL Query:
sql
CopyEdit
SELECT Shippers.ShipperName, Employees.EmployeeName,
COUNT(Orders.OrderID) AS TotalOrders
FROM Orders
JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
GROUP BY Shippers.ShipperName, Employees.EmployeeName;
Output:
ShipperName EmployeeName TotalOrders

DHL Alice 1

FedEx Bob 1

You might also like