Pshop Queries
Pshop Queries
1. List all the orders sent to Supplier 2 Its interesting to see the orders which have been sent to a single supplier SELECT * FROM supplyorder WHERE supplierid = 2 2. How many Intel Cpus where ordered from Computer Shop 1 between 2007-11-10 AND 2007-11-22? For statistical purposes, we could be interested to know the quantity ordered of a certain type of product of a single brand SELECT sum(shoporder.quantity) as quantity FROM (SELECT * FROM product WHERE type = 'CPU' AND brAND = 'Intel') e JOIN shoporder ON e.productid = shoporder.productid WHERE shoporder.shopid = 1 AND shoporder.date >= '2007-11-10' AND shoporder.date <= '2007-11-22' 3. How many orders did Computer Shop 1 made? A statistical query, to see the number of orders made by a single shop SELECT count(DISTINCT ordercode) AS n umber FROM shoporder WHERE shopid = 1 4. List out the name and the fax number of the supplier who supplies the product Logitech K9 The importance of the query relies on its practicality because it immediately identifies which is the supplier of a certain product. SELECT name, fax FROM supplier WHERE supplierid = (SELECT supplierid FROM product WHERE name = 'Logitech K9') 5. List out from which supplier do we have to order the products in shop order number 6 A very important query that, for a given shop order, prints out the suppliers that an employee has to contact SELECT e.name, supplier.* FROM supplier JOIN (product JOIN shoporder ON product.productid = shoporder.productid) e ON supplier.supplierid = e.supplierid WHERE e.ordercode = 6 6. When will the supplies for the products ordered in the shop order number 3 arrive? Its an interesting query because a client is always interested in the arrival date of the
products he ordered SELECT e.name, supplyorder.date FROM supplyorder JOIN ( SELECT product.productid, product.name FROM product JOIN shoporder ON product.productid = shoporder.productid WHERE shoporder.ordercode = 3) e ON supplyorder.productid = e.productid WHERE supplyorder.date >= current_date 7. Print out the availability of products that have been ordered in Shop Order 8 Useful to check if the products ordered in are available in the warehouse SELECT product.name, product.quantity FROM product JOIN shoporder ON product.productid = shoporder.productid WHERE shoporder.ordercode=8 8. List all the orders received form Computer Shop 1 Another statistical query, it retrieves the orders received from a given shop SELECT * FROM shoporder WHERE shopid=1 9. All the CPU's are now supplied by Supplier 2 A maintenance query, useful to change the supplier of a given type of product UPDATE product SET supplierID=(SELECT supplierID FROM Supplier WHERE name='Supplier2') WHERE type='CPU'; 10. List the last 5 orders managed by Mr. Paolo Rossi A statistical query, a manager may be interested in the last orders managed by a given employee SELECT s.orderCode, s.date FROM shopOrder s JOIN employee e ON s.employeeID = e.employeeID WHERE e.name ='Paolo' AND e.surname ='Rossi' UNION SELECT s.orderCode, s.date FROM supplyOrder s JOIN employee e ON s.employeeID = e.employeeID WHERE e.name ='Paolo' AND e.surname ='Rossi' ORDER BY date DESC LIMIT 5 11. Assign to employee "Alessio Ferrari" the last Supply Order assigned to "Paolo Rossi" It may be useful to assign an already assigned order to another employee UPDATE SupplyOrder SET employeeID=(SELECT employeeID FROM Employee WHERE name='Alessio' and surname='Ferrari') WHERE orderCode=(SELECT DISTINCT Max(orderCode) FROM SupplyOrder WHERE employeeID=(SELECT employeeID
FROM Employee WHERE name='Paolo' and surname='Rossi') ); 12. Insert a new product, supplied by "Supplier3", called "SLED 10.3", a software made by "Novell". The direction has decided to have an initial quantity of this product which must be the half of the quantity of the product "Windows XP Professional SP2" Nothing to add. INSERT INTO product(name, type, brand, quantity, supplierID) VALUES ('SLED 10.3','Software','Novell',(SELECT quantity / 2 FROM Product WHERE name='Windows XP Professional SP2'), (SELECT supplierID FROM Supplier WHERE name='Supplier3')); 13. Print out the products which 'ComputerShop1' has never ordered, ordered by Brand This statistical query may be interesting for the managers of our Computer Shop, it retrieves the products which have never be sold by a shop SELECT * FROM Product where (productID) not in (SELECT productID FROM ShopOrder WHERE shopID=(SELECT shopID FROM Shop WHERE name='ComputerShop 1')) ORDER BY brand 14. We want to see in which orders do the keyboards of brand Logitech appear A statistical query that could interest who decides which products should be in the warehouse (SELECT DISTINCT Product.productID, ShopOrder.orderCode FROM Product JOIN ShopOrder ON ShopOrder.productID = Product.productID WHERE type='Keyboard' AND brand='Logitech' ORDER BY Product.productID) UNION (SELECT DISTINCT Product.productID, SupplyOrder.orderCode FROM Product JOIN SupplyOrder ON SupplyOrder.productID = Product.productID WHERE type='Keyboard' AND brand='Logitech' ORDER BY Product.productID); 15. Due to a strike, the supply orders inserted after 3 days ago will suffer a two days delay No comments to be added. UPDATE SupplyOrder SET arrivalDate = (arrivalDate + 2) WHERE date >= (current_date - 3) AND date <= current_date;
16. How many orders did every employee managed up to now? This query is useful to check the efficiency of an employee (SELECT employeeID,Count(employeeID) FROM (SELECT DISTINCT orderCode,employeeID FROM ShopOrder) as tmp_table1 GROUP BY employeeID) UNION (SELECT employeeID,Count(employeeID) FROM (SELECT DISTINCT orderCode,employeeID FROM SupplyOrder) as tmp_table2 GROUP BY employeeID); -- We want to know, for every line of every order made by the shops, -- which is the mean of the quantity of product ordered up to now SELECT Avg(quantity) FROM ShopOrder 17. Print out the ID from the employee who managed the last shop order from ComputerShop 1' With this query we can know who have managed the last order of a given shop SELECT employeeid FROM shoporder o JOIN shop s ON o.shopid = s.shopid WHERE s.name = 'ComputerShop 1' ORDER BY o.ordercode DESC LIMIT 1 18. Print out the employee who managed the order number 4 With this query we can know who had managed a given order SELECT employee.* FROM employee JOIN shoporder ON employee.employeeid = shoporder.employeeid WHERE shoporder.ordercode = 15 UNION SELECT employee.* FROM employee JOIN supplyorder ON employee.employeeid = supplyorder.employeeid WHERE supplyorder.ordercode = 15 19. Print out the last shop which ordered the Amd Turion 64 X2 TL-60 SELECT * FROM shop JOIN shoporder ON shop.shopid = shoporder.shopid WHERE shoporder.productid = (SELECT productid FROM product WHERE name = 'Amd Turion 64 X2 TL- 60' ORDER BY date DESC LIMIT 1) 20. List all the products from a given brand, present in the store Another statistical query that can be useful for who deals with the suppliers or with the brands SELECT * FROM product WHERE brand = 'Intel'
21. We want to know, for every line of every order made by the shops, which is the average of the quantity of product ordered up to now SELECT Avg(quantity) FROM ShopOrder 22. How many opened orders did we have up to now? One of the most important query, it displays the number of unmanaged orders received SELECT Count(orderCode) FROM (SELECT orderCode FROM ShopOrder WHERE delivered=false GROUP BY orderCode) e UNION SELECT Count(orderCode) FROM (SELECT orderCode FROM SupplyOrder WHERE arrived=false GROUP BY orderCode) e 23. We want to see the Supplier name instead of the Supplier ID, in the table Product A practical query that could answer a request made by the managers of the Warehouse CREATE OR REPLACE VIEW ProductExtended As SELECT Product.ProductID,Product.name,Product.type,Product.brand,Product.quantity, Supplier.name AS supplier FROM Product JOIN Supplier On Product.supplierID = Supplier.supplierID; 24. We want to have a report which contains the orderCode of every order made, the date of the order, the name and the surname of the employee who managed it, and if it has been dispatched or not (either delivered or arrived) Doesnt need additional comments CREATE OR REPLACE VIEW OrderExtended AS (SELECT DISTINCT ShopOrder.orderCode,ShopOrder.date,Employee.name,Employee.surname,ShopOrder.deliv ered AS dispatched FROM ShopOrder JOIN Employee ON Employee.employeeID = ShopOrder.employeeID) UNION (SELECT DISTINCT SupplyOrder.orderCode,SupplyOrder.date,Employee.name,Employee.surname,SupplyOrder. arrived AS dispatched FROM SupplyOrder JOIN Employee ON Employee.employeeID = SupplyOrder.employeeID) 25. List all the products that are never ordered Very useful query to discover non-interesting products which nobody ordered SELECT * FROM product WHERE NOT EXISTS (SELECT * FROM shoporder WHERE shoporder.productid = product.productid 26. List all the ordered products which will arrive in the next days
This query may respond to organizational requirements (i.e.: organize the empty space in the warehouse) select * from product where product.productid = any( select productid from supplyorder where arrivaldate >= current_date)