You are on page 1of 81
BUSINESS CLUB Workshop Series 9.19 Nicole Lee xe e(ems a an] Leaming Objective 1 Identify potential operations issues of a large company 2. Understand what relational databases are * — Distinguishbetween tables, records, fields, and field values 3. Be able to write queries in SQL to answer questions 4. _ Understand basic database concepts lke normalization and entity relationships 5. _ Apply data to decisions in supply chain management General Mills Supply Chain You're a supply chain analyst at General Mills, one of the top manufacturers of consumer foods. General Mills sells its products to retail stores. You are given a dataset with , 5 zie! Although General Mills is a US Company, your boss wants you to find ways of within and (ebro cng cds You must figure out which suppiers and shippers the General IVils Selle cos on Database Terms 1 What are databases? * Colections of info organized by logical structure of that info 2. Database management software * Software to create, store, organize, and retrieve data from a (eleitclociic = Proprietary: Oracle, Access, SQL Server LIE @) 0-1 BCU (GC -anO 10 (SOME NO. ¢ Database administrator (DBA) = Person responsible for dev. & ops of a database Some Practical Industrial Advice a8 stores its data in some kind of relational database (for very good reason) * As an analyst, you wil want to pull data from that database = Building and designing a database; inserting new values — not omanvies} * Knowing how to write a SQL query will put you at a huge advantage relative to your peers = Don't be beholden to the DBA (who is mainly focused on keeping the database up) General Mills Dataset Tablename Records Customers 91 Categories 8 izle alts Employees 10 shows # of OrderDetails 518 Orders 196 records or are) SM ala) Products 77 Shippers 3 table Suppliers 29 Restore Database What is in the Customers Table? SELECT * FROM Customers; ‘supplier ns Gumbo Mix andma's Boysenberry Spreng Northwoods Cranberry Seuce emo 0 mi botes 36 boxes 12-11 pigs. 2-Dersere 18-500 9 pkgs. 22-200 m ars You try it Get all the rows from the Products table SELECT * FROM Products; Wie] ROA VAAUVclaIe liam enecra SELECT i neO) UI Products; What if we want Price and ProductID? SELECT Price FROM Products; Let's find the cheap products (say price < $10) SELECT * FROM Products WHERE Price < 10; You try it Get all rows in Products table where Price is greater liga OMe Uc MIO S 0 SELECT * FROM Products WHERE Price >= 30; How about products less than $10 but greater than $5? SELECT * FROM Products WHERE Price <10 AND Price > 5; SELECT FROM Products WHERE Price Price Price Price aol Price > 10 aan} < 10 AND Price > 5 < 10 OR Price > 25 <> 100 > 2 OR Price = 1 (Price > 2 AND Price < 10) OR Price = 1 How many products have a eeeuscmin'- aI Or SELECT COUNT (*) FROM Products WHERE Price < 10; SQL Statement: select count(*) from Products where Price < 10; Run SQL » Result Number of Records: 4 cea SELECT COUNT (*) AS NumProducts FROM Products WHERE Price < 10; Number of Records: 1 What's the total dollar Value of merchandise? SELECT SUM (Price) FROM Products; Here's something interesting — the OU ave lCW oe a ont 8 omg ABS olVos CREATE TABLE cheapstuff AS SELECT * FROM Products WHERE Price < 10; Tablename Records Customers 91 Categories 8 Employees 10 OrderDetails Orders Products Shippers Suppliers cheapstuff Restore Database You try it Make a table of all expensive products (say Price > $50) CREATE TABLE DearStuff rae) SELECT * FROM Products WHERE Price > 50; Can we list products in price order? SELECT * FROM Products ORDER BY Price DESC; DESCending means highest value first be. Can we create a new field based on price that lists whether a product is cheap or not? SELECT Price, CASE WHEN Price < 10 THEN ‘*t’ ELSE ‘ff’ END AS Cheap FROM Products SQL Statement: ‘SELECT Price, CASE WHEN Price < 10 then 't' ELSE * END as Cheap FROM Products; Run SOL Result: Number of Records: 77 You try tt Make a table with a field showing Mia slam @ 1 ema O=Niaca eee N16) ‘OH cocif elcome oree Cece iare price less than $15. SELE Or ete) sh AND T CT Price, WHEN Price > 5 Price < 10 HEN *t’ ELSE ‘f’ END AS midrange FROM I= Ole he keners) WHERE Price < 15; What's the breakdown between cheap and expensive products? First, let’s create a table with the cheap classification CREATE TABLE ProductPriceClass AS SELECT Price, CASE WHEN Price < 10 THEN ‘t’ ELSE ‘f’ END AS Cheap FROM Products; We now have a table that looks lke this: oe Tan BS eT eg SELECT Cheap, COUNT (*) AS ProductCount FROM ProductPriceClass Cheap SQL Statement: SELECT Cheap , COUNT(*) as ProductCount FROM ProductPriceClass GROUP BY Cheap; Result: Number of Records: 2 ProductCount You try it How many products at each price? SELECT Price, COUNT (*) as NumProducts FROM Products GROUP BY Price; Related Question: How many different prices do we have in the inventory? SELECT DISTINCT (Price) FROM Products; tement: SELECT Price FROM Products; un SOU = Result: Number of Records: SELECT DISTINCT(Ptice) FROM Procucts; 62 Another way to count uniques Another way to count uniques SELECT COUNT (*) FROM ( Sosa Omk DISTINCT (PRI CE) FROM Products ) Now for something quite different We actually have an [H/o] enta]0 os Tablename Customers Categories Employees OrderDetails Orders. Restore Database How are they related? + Each customer can have multiple orders * Each order can have multiple products * But each customer, order, and product should be unique Cie aie l-lt-ler=\s— SoA omlOn = Uniquely identify entities (Ike orders and customers) = Document relationships between entities ca - 6 emia Mer |=0) . ’ an Waray af Recor: 77 Producti Producthame 7 Ch rang ‘Ariseed Sirun ef soto Caan Seasoning Chet rivets Gumbo Mic Granema's Baysenaerry Spread + Orgeric Dried Fears yeso Cabrales ‘boxes x 20 bags 26 12 ox nates 2-550 ml bates 48-6 ee prs Siboxes 2-8 orjore 2-1 Ub kes. 2-2 oejre S509 9 peat ke De Rasa (e)e) elma cara -Oasal® aN ee customers and orders. One-to-many relationship? * Some customers have more than one order = How can we tell? omrci a eul COUNT (DISTINCT (CustomerID) ) FROM Orders; © SELECT COUNT(*) FROM Orders; = What's the distrution of orders per customer? opel CustomerID, COUNT (*) AS NumOrders FROM Orders GROUP BY CustomerId e) RDER BY NumOrders DI ral ep) io “ What if we want to see orders by Customer Country? * Problem: “Country” isn't in the Order table. If it were, we could Just do a “GROUP BY" + Solution: We can do a .A join lets us combine columns from cifferent tables, as needed. = Great discussion / visualization of different kinds of joins: soln . FROM A {LEFT, RIGHT, INNER, OUTER} JOIN B ON A.primary_ key = B.foreign_key - everything in A, only rec'd in B that match A= arial oN ane Naaee Nae Ames lean)(eg) “has to be in both : everything in both tables, regardless What do we want? * For each order, get the Country of the associated Customer SELECT o.OrderID, c.Country FROM Orders AS jo LEFT JOIN Customers AS [c ON o.CustomerID = c.CustomerID aoa 0.0 FRO oan ECT rderID, c.Country Orders AS o T JOIN Customers AS c ON o.CustomerID = c.CustomerlD; Note same # of records in Orders as in our LEFT JOIN query i i What if | don’t match CustomerlD? (don't run this at home) SELECT o.OrderID, c.Country FROM Orders/AS o LEFT JOIN’Customers AS c O)\ ea SeLeCT Order, Country FROM Orders AS © LEFT JOIN Customers AS ¢ ont Run SQL» per of Records| 17836. Tablename customers Categories Employees OrderDetails Orders Products, shippefs ‘Suspliers Restore Database What if a customer record is missing? Let's delete the customer with the lowest ID that made an order DELTE FROM Customers WHERE CustomerID = 2; Now let's re-run our LEFT JOIN slightly modified area What if we only want records where we have a customer? Let's go back to our original question: Geographic breakdown of orders Your Database: € SELECT Country, (Sse Sa Gaal rN ores ee) FROM (SELECT . % CeO ee: aoa ORDER BY NumOrders DESC; ‘SOL Statement SHLecr cumy, CONIC) A pum arte Garer fun sa.» (csp tne SQL Statemans, an lis SQ! ts 288 te rs Questions 1. What distinct countries are the suppliers from? 2. For each supplier, what is the average price of the products they ee 3. Who is the best shipper by nurnber of orders shipped? 4. What is the quantity of each product sold? 5. Give us the name of each product as well as the quantity sold of the product. Answer 1 1. What distinct countries are the suppliers from? SELECT DISTINCT (Country) FROM Suppliers Answer 2 2 For each supplier, what is the average price of the products they = IH SELECT SupplierID , AVG (Price) FROM Products GROUP BY SupplierID Answer 3 3, Who is the best shipper by number of orders shipped? SELECT ShipperID , COUNT (*) AS NumOrdersByShipperID FROM ORDERS GROUP BY ShipperID Answer 4 4. What is the quantity of each product sold? CREATE TABLE ProductQuant AS SELECT Product ID ,SUM(Quantity) AS TotalQuantSold FROM OrderDetails GROUP BY ProductID Answer 5 4. Give us the name of each product as well as the quantity sold of the product? SELECT b.ProductName , a.TotalQuantSold FROM TEST AS a LEFT JOIN Products AS b ON a.ProductID = b.ProductID Acknowledgement Segana enon

You might also like