0% found this document useful (0 votes)
145 views

Northwind Queries pt3

queries

Uploaded by

AndersonOliveira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
145 views

Northwind Queries pt3

queries

Uploaded by

AndersonOliveira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
2104/2020 GeeksEngine.com Knowledge Base Safe Computing Hosting Matters Computer Help Webmaster Help Intemet Marketing Web Development WAMP & LAMP MS Access Database SQL MySQL Tutorials Prorequisites Database Design (6) Sample Database Basic Select (12) Single Row Fune (9) Muts-table Select Subqueres (9) Summarize D2ta (5) Manipulate Data (2+) Managing Tables (3) Problem Solving (7) Geekstngine is hosted by HostGator, MySQL Noxthwind Queries - Part 3 Selecioneo idioma Pomeroy GosleTradutor Custom Search MySQL Northwind Queries - Part 3 This is part 3 of the tutorial series - converting the popular Microsoft Access Northwind database to MySQL. queries. ‘These queries are originated from Access Northwind Traders application. Some of them are relatively complex aggregated queries with sub-queres. 11. Products Above Average Price This query shows how to use sub-query to get a single value (average unit price) that can be used in the outer-query. select distinct Produetnane, UnttPriee ‘Fron Products lihere UnitPrice > (select avg(untterice) fron Products) (der by untzPrice; Here is the query result, 25 records returned. Janie omasinecten a2 Jaueso manchega La Pastore | Worsheoods Cranberry Sauce a 12. Product Sales for 1997 ‘This query shows how to group categories and products by quarters and shows sales amount for each quarter. select distinct a,categoryNane, .roductnane, Fornat(sun(e.UnitPrice * <.quantity * (1 - <.Discount)), 2) as Productsales, coneat(‘Qtr *, quarter(d.ShippedDate)) 2s ShippedQuarter ‘fron Categortes a nner join Produets b on a.CategoryIO = b.categoryI0 nner Join Order petaiie ¢ on beProdvctID = eProductIO ‘inner Join Orders aon d.OrderID = c.Onders0 Where .Shippedbate between date(" 1997-01-81") and date("1997-12-32") Group by a.categoryNane, '.ProductNane, coneat ("Qtr ", quarter(4.shippeddate) onder by a.categoryNane, ‘b.producthane, Shippedguarters hitps:iwwn.geeksengine.comidatabase!problem:-solving/nartuind-querias-par-3 php 1“ 2104/2020 MySQL Nosthwind Queries - Part 3 Here Is the query result. 286 records returned. siawpeamuseter, nat 105.0 tet cha 78.00 ter —— Che 1Anso the — hms 2iizes0 the eee hang 27a0.a0 tte ae hang 228.00 te fee hens 20sL.so tte eererases Cheng 2028.25 ote — pertreuge ceeze su20 tee Jeevsxace mareceuse weece ppazaed ce 413. Category Sales for 1997 ‘This query shows sales figures by categories - mainly just aggregation with sub-query. The inner query aggregates to product level, and the outer query further aggregates the result set rom inner-query to category level. select category, formt(sia(Prowetsales), 2) a5 Cateporysales “ select distinct a.categoryiane, b. Proaucthane, ‘Format (sun(e.UnitPrice * c.quantity * (1 - c.Discount)), 2) as Productsales, oncat( "Gtr ©, quarter(d.ShippedDate)) 2s Shippedguarter fron Categories a6 2 nner join Products 25 b on a.CategorylD ~ b.Category10 liner join Order petails a5 € on bebroguct1D = e,ProductIO {nner join Orders. 3s on é.0rderID = e-Oederi0 uinere dShippeabate between date('1997-61-@1") and date('1997-12-31") troup by a.categorynane, .Proauctnane, conese( Ger", quarter(d.Shippeddate)) onder by a.categoryNane, b.Producthane, Shippedguarter ) asx [roup by Categorynane ‘onder by €ategorynane; Here is the query result, B records returned, aos Jevesases prea. Jeencecrions 13,010.22 Jixeina/Cerenls _11,S11.15 14. Quarterly Orders by Product This query shows how to convert order dates to the corresponding quarters. It also demonstrates how SUM function is, Used together with CASE statement to get sales for each quarter, where quarters are converted from OrderDate columa select a.Prosuctane, ‘4.conpanyNane, year(OederDate) as Ordervear, Fornat(sun(ease quarter(e.Orderoate) when °1" then b.UnisPricerb.Quantity*(2-b-Discount) else @ end), @) “ote 2°, Fornat(sun(case quarcer(e.orderoate) when "2 then b,UniePrice®b.Quantity*(s-b.Discount) else 6 end), @) “Gtr 2°, format(sungense quarcer(e-Orderoate) when "3 ‘then b.UniePnicerd-Quantsty*(s-b.Discount) else @ end), @) “Qe 3°, Fornat(sun(case quarzer(e.orderoate) when "a ‘then b.Uniepricerb-Quantity*(2-B-Discount) else @ end), 9) “Gtr 4” ‘fron Progucte. ‘inner josn Order_Detafls b on a.ProductiD = b.Praducti9| inner Join Orders c on €.OrderID = b.OrderiD inner join Costowers on d-Custoneri0 = CustomerID whore €.Orderoate between date("%957-01-01") and date(‘3997~12-31") group by a.ProductNane, ‘.cenpanytsne, year(OrderDate) onder by a.ProductNane, ¢.Companyane; hitps:iwwn.geeksengine.comidatabase!problem:-solving/nartuind-querias-par-3 php 218 2104/2020 MySQL Nosthwind Queries - Part 3 Here ts the query result. 947 records returned. etna [Compangten fatice micron —Juscente noreuo Taquecta orton Goase Cocina Tipise soe Micron Ratclesuaxe Canyon Grocery 15. Invoice [sesertear [oer ifote aJotr 3 | oe {A simple query to get detailed information for each sale so that invoice can be issued, select distinct b.Shiplane, b.shipadcress, bishipeity, Bi shipRegton, bi shippostalCoae, bishipcountey, bicustonersD, Cceompanytiane, eladaress, ecctty, e.negion, elbostaleode, leauntry, Concat(dsFinstNane, b.oraers0, blorderoste, b Requireabate, b.shippeadate, conpanytsne, e-preaucerD, Foreductnane, elunstirice, quantity, feibiscount, boresgne ‘fron Shippers a nner join Products # os F.ProduetIO fonder By b.Shiplane; inner jotn Orders b on a.ShippersD = b.Shipvia inner Join Costoners ¢ on e-CustonerI9 = 0-CustonerID inner Join Enployees d on d-tmployeeTO = .FxployeetD nner Join Order-betaiis «on b.OrderID = e.OrderIb ecraduct iD "*, dstastnane) as salesperson, f.UnitPnice * e.quantity * (1 - e.Discount) as ExtendedPrice, Here is the query eesut, 2,155 records returned, snupreseaicoae faiteeds Potcerkaote there Sex. 6? eelin a Teuiiiio Euparetsiavda, 42 14 Compcituciin 2222 Mexico D.Ps na Tey}iiio Fuparetsiavda, de 1a coustituciin 2222 xexico D.F. a Tesjille Baparedeiivda, de 1 Conetituciin 2222 México DLP. Jina TeujiLlo Enpaved ada. de La Cenarstacien 2222 Maaco DP Joon Texgilo Enporedes ada. de Ia Conatstarion 2222 Mexioo DoF. 16. Number of units in stock by category and supplier continent aan nsoat ‘This query shows that case statement is used in GROUP BY clause to lst the number of units in stack for each product category and supplier's continent, Note thet, i only s.Country (not the case statement) is used in the GROUP BY, Guplcated rows will exist for each product category and supplier continent. select e.category case when ‘hen Then else end $5" sun(o. Un fron Suppliers 5 inner Join Produ inner join Catep Wane as “Product Category", "feeauntry in (COCs Spain", "sweden*, ‘Genmany", "Norway", "Dentark’, Netherlands", "Fanand’,"Ttaly","France’) Europe s.country in (USA, "Canada" Brazil") ‘aneries” ‘Rebasacific! supplier Continent’ sitsinstock) 25 UnitsInstock jets p on p.SupplierIDes.SupphierID ies ¢ on e.CategoryT0up. category 0 hitps:iwwn.geeksengine.comidatabase!problem:-solving/narthuind-queras-par-3 php 3a 2104/2020 No portion may be reprocuced without my writen permission, Software and hardware names mentioned on this ste are registered trademarks oftheir respective companies. Shoulé any nght Be infringed, is totaly unintentional. "op m2 an-ermal and Twill promptly and glaaly recy i MySQL Nosthwind Queries - Part 3 group by e.categoryvane, ase uhen country in (COUR "Spade" "Skeden’, “Germany” "NorHay" ‘Dennark', "Netherlands", Finland’, Tealy", France") then "Europe" hen s.country tn (°USA', "Canada, then ‘arerdea” else ‘Asia-Pacific’ antl’) ends Here is the query result. 21 records returnes. [unitoiastane feverases asieracitic a2 Jeoncecriona acta tacntic a Jroxtecrions Perope 34 Jexetne/ereais race Here comes the end of this article series. hope you find it useful in your day-to-day jab of SQL coding! Don't forget to download the full Script which can be found at the beginning ofthe frst two parts ofthis article series. Other tutorials in this category 1. MySQL Nonthwind Queries - Part 1 2. MySQL No-thwind Queries - Part 2 3 x “ 4. How to Fll Gaps in Sales Dat 1% 5, How to Calculate Totals, Subtotals and Grand Total 6. How to Work with NULL valves Back to Tutorial Index Page Copyright © 2020 Geekstngine.com. All Rights Reserved. ‘This website is hosted by HostGator. Home | Feedback | Terms of Use | Privacy Policy hitps:iwwn.geeksengine.comidatabase!problem:-solving/nartuind-querias-par-3 php ais

You might also like