50 SQL Queries
1. Retrieve all customer names and their contact numbers
SELECT Name, ContactNumber FROM Customer;
2. List all products with their name, category, and price
SELECT Name, Category, Price FROM Product;
3. Get all suppliers located in a specific city (e.g., 'Manila')
SELECT * FROM Supplier WHERE Address LIKE '%Vinzons%';
4. Show all staff members with their positions and email addresses
SELECT Name, Position, Email FROM Staff;
5. Retrieve all store locations and capacities
SELECT Location, Capacity FROM Store;
6. Find the total number of customers
SELECT COUNT(*) AS TotalCustomers FROM Customer;
7. Count how many products are in the chosen category
SELECT COUNT(*) AS ElectronicsCount FROM Product WHERE Category = 'Nuts';
8. Display all purchases made on a specific date
SELECT * FROM Purchases WHERE PurchaseDate = '2025-04-01';
9. List all suppliers and the number of products they supply
SELECT SupplierID, COUNT(*) AS ProductsSupplied FROM Supplied_By GROUP
BY SupplierID;
10. Find all staff who have no contact number
SELECT * FROM Staff WHERE ContactNumber IS NULL;
11. Get the names of customers who purchased a product
SELECT DISTINCT [Link] FROM Customer C JOIN Purchases P ON
[Link] = [Link];
12. Show all products and the suppliers who supplied them
SELECT [Link], [Link] FROM Product P JOIN Supplied_By SB ON
[Link] = [Link] JOIN Supplier S ON [Link] = [Link];
13. List the staff who handled each customer
SELECT [Link] AS CustomerName, [Link] AS StaffName FROM Handled_By
HB JOIN Customer C ON [Link] = [Link] JOIN Staff S ON
[Link] = [Link];
14. Find the products available in each store
SELECT [Link], [Link] FROM Available_In AI JOIN Product P ON
[Link] = [Link] JOIN Store S ON [Link] = [Link];
15. Get the names of staff and the stores they work in
SELECT [Link], [Link] FROM Staff_Store SS JOIN Staff Stf ON [Link]
= [Link] JOIN Store Str ON [Link] = [Link];
16. Retrieve all purchases along with product names and prices
SELECT [Link], [Link] AS ProductName, [Link] FROM Purchases PU JOIN
Customer C ON [Link] = [Link] JOIN Product P ON [Link]
= [Link];
17. Show all suppliers with their supplied product names and supply dates
SELECT [Link], [Link] AS ProductName, [Link] FROM
Supplied_By SB JOIN Supplier S ON [Link] = [Link] JOIN Product P
ON [Link] = [Link];
18. List customers and the staff who handled them, along with the handle type
SELECT [Link] AS Customer, [Link] AS Staff, [Link] FROM
Handled_By HB JOIN Customer C ON [Link] = [Link] JOIN Staff S
ON [Link] = [Link];
19. Get store locations where a specific product is available
SELECT [Link] FROM Available_In AI JOIN Store S ON [Link] = [Link]
WHERE [Link] = 1;
20. Show product details and their store stock levels
SELECT [Link], [Link], [Link] FROM Available_In AI JOIN Product P
ON [Link] = [Link] JOIN Store S ON [Link] = [Link];
21. Get customer name, product name, and purchase date for every transaction
SELECT [Link] AS Customer, [Link] AS Product, [Link] FROM
Purchases PU JOIN Customer C ON [Link] = [Link] JOIN Product
P ON [Link] = [Link];
22. Find all stores and the products they manage with the management period
SELECT [Link], [Link], [Link], [Link] FROM Manages M JOIN
Store S ON [Link] = [Link] JOIN Product P ON [Link] = [Link];
23. Show staff members who are managing multiple stores
SELECT StaffID, COUNT(*) AS StoreCount FROM Staff_Store GROUP BY StaffID
HAVING COUNT(*) > 1;
24. List stores and the total quantity of products supplied to them
SELECT [Link], [Link], SUM([Link]) AS TotalSupplied FROM
Store S JOIN Available_In AI ON [Link] = [Link] JOIN Supplied_By SB ON
[Link] = [Link] GROUP BY [Link];
25. Show all suppliers that have supplied more than 100 units of a product
SELECT SupplierID, ProductID FROM Supplied_By WHERE SupplyQuantity > 100;
26. Count the number of products per category
SELECT Category, COUNT(*) AS ProductCount FROM Product GROUP BY
Category;
27. Find the average price of products in each category
SELECT Category, AVG(Price) AS AvgPrice FROM Product GROUP BY Category;
28. Get the total quantity of each product available in all stores
SELECT ProductID, SUM(Quantity) AS TotalAvailable FROM Product GROUP BY
ProductID;
29. Find the highest-priced product in each category
SELECT Category, MAX(Price) AS MaxPrice FROM Product GROUP BY Category;
30. Count the number of purchases made by each customer
SELECT CustomerID, COUNT(*) AS PurchaseCount FROM Purchases GROUP BY
CustomerID;
31. Calculate the total supply quantity provided by each
supplier
SELECT SupplierID, SUM(SupplyQuantity) AS TotalSupplied FROM
Supplied_By GROUP BY SupplierID;
32. Find the store with the highest capacity
SELECT * FROM Store ORDER BY Capacity DESC LIMIT 1;
33. Show the total number of staff per position
SELECT Position, COUNT(*) AS StaffCount FROM Staff GROUP BY
Position;
34. List the average number of products managed per store
SELECT StoreID, AVG(ProductCount) FROM (SELECT StoreID, COUNT(ProductID)
AS ProductCount FROM Manages GROUP BY StoreID) AS Sub GROUP BY
StoreID;
35. Display the total revenue per product
SELECT [Link], [Link], COUNT([Link]) * [Link] AS Revenue FROM
Purchases PU JOIN Product P ON [Link] = [Link] GROUP BY
[Link];
36. Find customers who have never made a purchase
SELECT * FROM Customer WHERE CustomerID NOT IN (SELECT DISTINCT
CustomerID FROM Purchases);
37. List products not available in any store
SELECT * FROM Product WHERE ProductID NOT IN (SELECT DISTINCT
ProductID FROM Available_In);
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 [Link] FROM Handled_By HB JOIN Staff S ON [Link] = [Link]
GROUP BY [Link] HAVING COUNT(DISTINCT [Link]) > 5;
40. List stores where the stock level is below "Low"
SELECT * FROM Available_In WHERE StockLevel = 'Low';
41. Find products managed by more than one store
SELECT ProductID FROM Manages GROUP BY ProductID HAVING
COUNT(DISTINCT StoreID) > 1;
42. Display products that have never been supplied by any supplier
SELECT * FROM Product WHERE ProductID NOT IN (SELECT DISTINCT
ProductID FROM Supplied_By);
43. Retrieve staff who are not assigned to any store
SELECT * FROM Staff WHERE StaffID NOT IN (SELECT DISTINCT StaffID FROM
Staff_Store);
44. List customers handled by a specific staff member (e.g., StaffID = 2)
SELECT [Link] FROM Handled_By HB JOIN Customer C ON [Link] =
[Link] WHERE [Link] = 2;
45. Show the names of all customers who purchased a specific product (e.g.,
ProductID = 1)
SELECT [Link] FROM Purchases P JOIN Customer C ON [Link] =
[Link] WHERE [Link] = 1;
46. Get the top 3 most purchased products
SELECT ProductID, COUNT(*) AS PurchaseCount FROM Purchases GROUP BY
ProductID ORDER BY PurchaseCount DESC LIMIT 3;
47. Find the most active customer based on purchase count
ELECT CustomerID, COUNT(*) AS TotalPurchases FROM Purchases GROUP BY
CustomerID ORDER BY TotalPurchases DESC LIMIT 1;
48. Show the latest purchase made by each customer
SELECT CustomerID, MAX(PurchaseDate) AS LatestPurchase FROM Purchases
GROUP BY CustomerID;
49. List all products with their current availability in each store
SELECT [Link], [Link], [Link] FROM Available_In AI JOIN Product P
ON [Link] = [Link] JOIN Store S ON [Link] = [Link];
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;