0% found this document useful (0 votes)
8 views7 pages

SQL_Solutions_40_Exercises

The document contains SQL solutions for 40 exercise questions, covering various database queries such as retrieving employee names and titles, finding products and suppliers, and analyzing customer orders. Each question is accompanied by a corresponding SQL query that demonstrates how to extract the required information from a database. The exercises include operations like filtering, grouping, and joining tables to obtain insights from the data.

Uploaded by

vinhkooit777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
8 views7 pages

SQL_Solutions_40_Exercises

The document contains SQL solutions for 40 exercise questions, covering various database queries such as retrieving employee names and titles, finding products and suppliers, and analyzing customer orders. Each question is accompanied by a corresponding SQL query that demonstrates how to extract the required information from a database. The exercises include operations like filtering, grouping, and joining tables to obtain insights from the data.

Uploaded by

vinhkooit777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

SQL Solutions for 40 Exercise Questions

1. Retrieve all employees’ full names (first and last names) and their titles.
SELECT FirstName, LastName, Title
FROM Employees;

2. Find the product names and their suppliers.


SELECT ProductName, CompanyName
FROM Products
JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;

3. List all customers who are from Germany.


SELECT CustomerID, CompanyName, Country
FROM Customers
WHERE Country = 'Germany';

4. Retrieve orders with freight costs greater than $100.


SELECT OrderID, Freight
FROM Orders
WHERE Freight > 100;

5. Get the total sales for each product, sorted by highest to lowest sales.
SELECT ProductID, SUM(UnitPrice * Quantity) AS TotalSales
FROM OrderDetails
GROUP BY ProductID
ORDER BY TotalSales DESC;

6. Find the customers who have placed more than 5 orders.


SELECT CustomerID, COUNT(OrderID) AS TotalOrders
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) > 5;
7. List the names of employees and their hire dates who were hired in the
year 1993.
SELECT FirstName, LastName, HireDate
FROM Employees
WHERE YEAR(HireDate) = 1993;

8. Retrieve all products with unit prices between $10 and $30.
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice BETWEEN 10 AND 30;

9. Get the total number of products in each category.


SELECT CategoryName, COUNT(ProductID) AS TotalProducts
FROM Categories
JOIN Products ON Categories.CategoryID = Products.CategoryID
GROUP BY CategoryName;

10. List the suppliers that supply beverages.


SELECT s.CompanyName
FROM Suppliers s
JOIN Products p ON s.SupplierID = p.SupplierID
JOIN Categories c ON p.CategoryID = c.CategoryID
WHERE c.CategoryName = 'Beverages';

11. Retrieve orders placed in 1997.


SELECT OrderID, OrderDate
FROM Orders
WHERE YEAR(OrderDate) = 1997;

12. Find customers from the UK who have placed orders.


SELECT DISTINCT c.CustomerID, c.CompanyName
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE c.Country = 'UK';
13. Retrieve the average freight cost of all orders.
SELECT AVG(Freight) AS AverageFreight
FROM Orders;

14. Find products that have been sold in quantities greater than 100 units.
SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM OrderDetails
GROUP BY ProductID
HAVING SUM(Quantity) > 100;

15. List employees who have not been assigned a supervisor.


SELECT FirstName, LastName
FROM Employees
WHERE ReportsTo IS NULL;

16. Retrieve the name and job title of the employee who manages the
most employees.
SELECT e.FirstName, e.LastName, e.Title, COUNT(emp.EmployeeID) AS TotalEmployees
FROM Employees e
JOIN Employees emp ON e.EmployeeID = emp.ReportsTo
GROUP BY e.FirstName, e.LastName, e.Title
ORDER BY TotalEmployees DESC
LIMIT 1;

17. Find the most expensive product in the database.


SELECT ProductName, UnitPrice
FROM Products
ORDER BY UnitPrice DESC
LIMIT 1;

18. Get the total sales amount for each order.


SELECT OrderID, SUM(UnitPrice * Quantity) AS TotalSales
FROM OrderDetails
GROUP BY OrderID;
19. List products that are not discontinued.
SELECT ProductName
FROM Products
WHERE Discontinued = 0;

20. Retrieve the total number of employees in each city.


SELECT City, COUNT(EmployeeID) AS TotalEmployees
FROM Employees
GROUP BY City;

21. Find customers who have placed at least one order with a freight
charge over $50.
SELECT DISTINCT c.CustomerID, c.CompanyName
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.Freight > 50;

22. Get the total value of orders shipped to Germany.


SELECT SUM(od.UnitPrice * od.Quantity) AS TotalValue
FROM Orders o
JOIN OrderDetails od ON o.OrderID = od.OrderID
WHERE o.ShipCountry = 'Germany';

23. Find employees whose names start with the letter 'A'.
SELECT FirstName, LastName
FROM Employees
WHERE FirstName LIKE 'A%';

24. Retrieve the names of all suppliers located in the USA.


SELECT CompanyName
FROM Suppliers
WHERE Country = 'USA';

25. List products with a reorder level greater than 50.


SELECT ProductName, ReorderLevel
FROM Products
WHERE ReorderLevel > 50;
26. Get the average unit price of products in each category.
SELECT c.CategoryName, AVG(p.UnitPrice) AS AveragePrice
FROM Categories c
JOIN Products p ON c.CategoryID = p.CategoryID
GROUP BY c.CategoryName;

27. Find orders that were shipped to the same city as their customers.
SELECT o.OrderID, o.ShipCity, c.City
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE o.ShipCity = c.City;

28. Retrieve the employee with the most recent hire date.
SELECT FirstName, LastName, HireDate
FROM Employees
ORDER BY HireDate DESC
LIMIT 1;

29. Find orders that were not shipped to the same country as their
customers.
SELECT o.OrderID, o.ShipCountry, c.Country
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE o.ShipCountry != c.Country;

30. Get the names of customers who have never placed an order.
SELECT CompanyName
FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);

31. Find the most frequently ordered product.


SELECT ProductID, COUNT(*) AS OrderCount
FROM OrderDetails
GROUP BY ProductID
ORDER BY OrderCount DESC
LIMIT 1;
32. Retrieve the details of orders that were shipped late (shipped after
the required date).
SELECT OrderID, RequiredDate, ShippedDate
FROM Orders
WHERE ShippedDate > RequiredDate;

33. Find customers who have placed orders for over $10,000 in total.
SELECT c.CustomerID, c.CompanyName, SUM(od.UnitPrice * od.Quantity) AS TotalSpent
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
JOIN OrderDetails od ON o.OrderID = od.OrderID
GROUP BY c.CustomerID, c.CompanyName
HAVING SUM(od.UnitPrice * od.Quantity) > 10000;

34. List the products ordered by customer 'ALFKI'.


SELECT p.ProductName
FROM Orders o
JOIN OrderDetails od ON o.OrderID = od.OrderID
JOIN Products p ON od.ProductID = p.ProductID
WHERE o.CustomerID = 'ALFKI';

35. Get the number of orders placed by each customer in 1996.


SELECT CustomerID, COUNT(OrderID) AS OrderCount
FROM Orders
WHERE YEAR(OrderDate) = 1996
GROUP BY CustomerID;

36. Retrieve products that have been ordered more than 200 times in
total.
SELECT ProductName, SUM(Quantity) AS TotalQuantity
FROM OrderDetails
JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY ProductName
HAVING SUM(Quantity) > 200;
37. List employees who live in the same city where the company is
located (Seattle).
SELECT FirstName, LastName
FROM Employees
WHERE City = 'Seattle';

38. Retrieve customers who have ordered more than 5 different products.
SELECT o.CustomerID, c.CompanyName, COUNT(DISTINCT od.ProductID) AS ProductCount
FROM Orders o
JOIN OrderDetails od ON o.OrderID = od.OrderID
JOIN Customers c ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID, c.CompanyName
HAVING COUNT(DISTINCT od.ProductID) > 5;

39. Find products that have never been ordered.


SELECT ProductName
FROM Products
WHERE ProductID NOT IN (SELECT ProductID FROM OrderDetails);

40. Get the total sales made by each employee.


SELECT e.FirstName, e.LastName, SUM(od.UnitPrice * od.Quantity) AS TotalSales
FROM Employees e
JOIN Orders o ON e.EmployeeID = o.EmployeeID
JOIN OrderDetails od ON o.OrderID = od.OrderID
GROUP BY e.FirstName, e.LastName;

You might also like