SQL_Solutions_40_Exercises
SQL_Solutions_40_Exercises
1. Retrieve all employees’ full names (first and last names) and their titles.
SELECT FirstName, LastName, Title
FROM Employees;
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;
8. Retrieve all products with unit prices between $10 and $30.
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice BETWEEN 10 AND 30;
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;
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;
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;
23. Find employees whose names start with the letter 'A'.
SELECT FirstName, LastName
FROM Employees
WHERE FirstName LIKE 'A%';
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);
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;
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;