SQL 50 Practical Questions Answers
SQL 50 Practical Questions Answers
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
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
Question 2
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
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
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
Question 4
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
Question 5
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
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
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
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
Question 9
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
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
Find orders where the employee and customer are from the same city.
Tables:
Customers
CustomerID CustomerName City
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Question 33
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
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
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
Question 36
Find all products ordered by customers from a specific country (e.g., 'Kenya').
Tables:
Customers
CustomerID CustomerName Country
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
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
Question 40
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
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
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
Question 45
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
Question 46
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
Question 47
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
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