50 SQL Queries
50 SQL Queries
4. Show all staff members with their positions and email addresses
SELECT Name, Position, Email FROM Staff;
12. Show all products and the suppliers who supplied them
SELECT P.Name, S.CompanyName FROM Product P JOIN Supplied_By SB ON
P.ProductID = SB.ProductID JOIN Supplier S ON SB.SupplierID = S.SupplierID;
15. Get the names of staff and the stores they work in
SELECT Stf.Name, Str.Location FROM Staff_Store SS JOIN Staff Stf ON SS.StaffID
= Stf.StaffID JOIN Store Str ON SS.StoreID = Str.StoreID;
16. Retrieve all purchases along with product names and prices
SELECT C.Name, P.Name AS ProductName, P.Price FROM Purchases PU JOIN
Customer C ON PU.CustomerID = C.CustomerID JOIN Product P ON PU.ProductID
= P.ProductID;
17. Show all suppliers with their supplied product names and supply dates
SELECT S.CompanyName, P.Name AS ProductName, SB.SupplyDate FROM
Supplied_By SB JOIN Supplier S ON SB.SupplierID = S.SupplierID JOIN Product P
ON SB.ProductID = P.ProductID;
18. List customers and the staff who handled them, along with the handle type
SELECT C.Name AS Customer, S.Name AS Staff, HB.HandleType FROM
Handled_By HB JOIN Customer C ON HB.CustomerID = C.CustomerID JOIN Staff S
ON HB.StaffID = S.StaffID;
21. Get customer name, product name, and purchase date for every transaction
SELECT C.Name AS Customer, P.Name AS Product, PU.PurchaseDate FROM
Purchases PU JOIN Customer C ON PU.CustomerID = C.CustomerID JOIN Product
P ON PU.ProductID = P.ProductID;
22. Find all stores and the products they manage with the management period
SELECT S.Location, P.Name, M.StartDate, M.EndDate FROM Manages M JOIN
Store S ON M.StoreID = S.StoreID JOIN Product P ON M.ProductID = P.ProductID;
24. List stores and the total quantity of products supplied to them
SELECT S.StoreID, S.Location, SUM(SB.SupplyQuantity) AS TotalSupplied FROM
Store S JOIN Available_In AI ON S.StoreID = AI.StoreID JOIN Supplied_By SB ON
AI.ProductID = SB.ProductID GROUP BY S.StoreID;
25. Show all suppliers that have supplied more than 100 units of a product
SELECT SupplierID, ProductID FROM Supplied_By WHERE SupplyQuantity > 100;
28. Get the total quantity of each product available in all stores
SELECT ProductID, SUM(Quantity) AS TotalAvailable FROM Product GROUP BY
ProductID;
38. Show suppliers who have supplied all products in the chosen category
SELECT DISTINCT SupplierID FROM Supplied_By WHERE ProductID IN (SELECT
ProductID FROM Product WHERE Category = 'Confectionery') GROUP BY
SupplierID HAVING COUNT(DISTINCT ProductID) = (SELECT COUNT(*) FROM
Product WHERE Category = 'Confectionery');
39. Get the names of staff who handled more than 5 customers
SELECT S.Name FROM Handled_By HB JOIN Staff S ON HB.StaffID = S.StaffID
GROUP BY HB.StaffID HAVING COUNT(DISTINCT HB.CustomerID) > 5;
45. Show the names of all customers who purchased a specific product (e.g.,
ProductID = 1)
SELECT C.Name FROM Purchases P JOIN Customer C ON P.CustomerID =
C.CustomerID WHERE P.ProductID = 1;
49. List all products with their current availability in each store
SELECT P.Name, S.Location, AI.StockLevel FROM Available_In AI JOIN Product P
ON AI.ProductID = P.ProductID JOIN Store S ON AI.StoreID = S.StoreID;
50. Determine which store has the most diverse product availability
SELECT StoreID, COUNT(DISTINCT ProductID) AS ProductVariety FROM
Available_In GROUP BY StoreID ORDER BY ProductVariety DESC LIMIT 1;