Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
Page1of21
PROJECT2
LARGECOMPANYDATABASE
ISE382
TEAM04:ERICLOEB,NATHANCURTIS,DAVIDTANAKA
ADVANCEDSQLQUERIES60points
PROJECTOBJECTIVES
Use JOINs, subqueries, and SQL functions to answer complex questions
TEAMWORK
Each team member must collaborate and contribute to the project. Only one submission of the completed project (per team) is required.
CASESCENARIO(REFERTOPAGE291OFYOURTEXTBOOK)
Large Company database stores data for a company that sells paints. The company tracks the sale of products to customers. The database keeps data on customers [LGCUSTOMER], sales [LGINVOICE], products [LGPRODUCT), which products are on which invoices [LGLINE], employees [LGEMPLOYEE], the salary history of each employee [LGSALARY_HISTORY],
Page2of21
departments [LGDEPARTMENT], product brands [LGBRAND], vendors [LGVENDOR], and which vendors supply each product [LGSUPPLIES] as shown in the ER diagram below.
Historical data for this company in stored in a Microsoft Access database. Some of the tables contain only a few rows of data, while other tables are quite large; e.g. there are only eight departments, but more than 3,300 invoices containing over 11,000 invoice lines.
REVIEWYOURLARGECOMPANYERMODEL
Download the Large Company Data Model file from Blackboard and import it to the Data Modeling panel by choosing Open Existing EER Model. Open the model and review the tables, attributes, keys, relationships and data types.
After reviewing the model, close the model.
INSTRUCTIONS
Page3of21
1. Although you built and populated your own Large Company database in Project 1, you have been given access to a LargeCo database in MYSQL. Use this database to do this project.
2. You may want to save each query definition in a local text file (.sql). Share those with your team members.
3. For turning in your Project, create a Word file.
4. In the Word document, use the following format to answer the queries. Insert the SQL statement and a screen shot of the results.
Query 1:
SELECT *
FROM user01.LGDEPARTMENT;
Result:
QUERIES
1. WriteaquerytodisplaytheeightdepartmentsintheLGDEPARTMENTtable.
uselargeco;
select*
fromlgdepartment;
Page4of21
2. Write a query to display the SKU (stock keeping unit), description, type, base, category, and price for allproductsthathaveaPROD_BASEofwaterandaPROD_CATEGORYofsealer.
uselargeco;selectp.prod_sku,p.prod_description,p.prod_type,p.prod_base,p.prod_category,p.prod_pricefromlgproductpwherep.prod_base="water"andp.prod_category="sealer";
3. Writeaquerytodisplaythefirstname,lastname,ande-mailaddressofemployeeshiredfromJanuary1,
2003,toDecember31,2012.Sorttheoutputbylastnameandthenbyfirstname.uselargeco;selectemp_fname,emp_lname,emp_emailfromlgemployeewhereEmp_Hiredate>'2003-01-1'andEmp_Hiredate<'2012-12-31'orderbyemp_lname,emp_fname;
Page5of21
4. Write a query to display the first name, last name, phone number, title, and department number ofemployeeswhoworkindepartment300orhavethetitle“CLERKI.”Sorttheoutputbylastnameandthenbyfirstname.
uselargeco;Selectemp_fname,emp_lname,emp_phone,emp_title,dept_numfromlgemployeewheredept_num=300oremp_title='CLERKI'orderbyemp_lname,emp_fname;
5. Writeaquerytodisplaytheemployeenumber,lastname,firstname,salary“from”date,salaryenddate,andsalaryamountforemployees83731,83745,and84039.Sorttheoutputbyemployeenumberandsalary“from”date.
uselargeco;selecte.emp_num,e.emp_lname,e.emp_fname,s.sal_from,s.sal_end,s.sal_amountfromlgemployeee,lgsalary_historyswheres.emp_num=e.emp_numand(e.emp_num=83731ore.emp_num=83745ore.emp_num=84039);
Page6of21
6. Write a query to display the first name, last name, street, city, state, and zip codeof any customerwhopurchased a Foresters Best brand top coat between July 15, 2013, and July 31, 2013. If a customerpurchasedmorethanonesuchproduct,displaythecustomer’sinformationonlyonceintheoutput.Sorttheoutputbystate,lastname,andthenfirstname.
uselargeco;selectdistinct(c.cust_fname),c.cust_lname,c.cust_street,c.cust_city,c.cust_state,c.cust_zipfromlgcustomerc,lgbrandb,lgproductp,lglinel,lginvoiceiwhere b.brand_ID = p.brand_ID and p.prod_sku = l.prod_sku and l.inv_num = i.inv_num and i.cust_code =c.cust_codeandb.brand_name= "Foresters best" andp.prod_category = "top coat" and i.inv_datebetween '2013-07-15'and'2013-07-31'orderbycust_state,cust_lname,cust_fname;
Page7of21
7. Writeaquery todisplay theemployeenumber, lastname,e-mailaddress, title,anddepartmentnameofeachemployeewhose job titleends in theword “ASSOCIATE.” Sort theoutputbydepartmentnameandemployeetitle.
Uselargeco;select e.emp_num, e.emp_lname, e.emp_email, e.emp_title, d.dept_name from lgemployee e, lgdepartment d where e.dept_num = d.dept_num and e.emp_title like '%ASSOCIATE' order by d.dept_name, e.emp_title;
Page8of21
8. Writeaquerytodisplayabrandnameandthenumberofproductsofthatbrandthatareinthedatabase.Sorttheoutputbythebrandname.
uselargeco;selectb.brand_name,count(prod_sku)asNumProductsfromlgbrandb,lgproductpwhereb.brand_ID=p.brand_IDgroupbybrand_name;
9. Writeaquerytodisplaythenumberofproductsineachcategorythathaveawaterbase.uselargeco;selectPROD_CATEGORY,count(prod_sku)asNUMPRODUCTSfromlgproductwhereprod_base='Water'groupbyprod_category;
Page9of21
10. Writeaquerytodisplaythenumberofproductswithineachbaseandtypecombination.uselargeco;selectprod_base,prod_type,count(prod_sku)fromlgproductgroupbyprod_base,prod_type;
11. Writeaquerytodisplaythetotalinventory—thatis,thesumofallproductsonhandforeachbrandID.Sort
theoutputbybrandIDindescendingorder.uselargeco;selectb.brand_ID,sum(p.prod_QOH)asTotalInventoryfromlgbrandb,lgproductpwhereb.brand_ID=p.brand_IDgroupbybrand_IDorderbybrand_IDdesc;
12. WriteaquerytodisplaythebrandID,brandname,andaveragepriceofproductsofeachbrand.Sortthe
outputbybrandname.(Resultsareshownwiththeaveragepriceroundedtotwodecimalplaces.)uselargeco;
Page10of21
selectb.brand_idasBRAND_ID,b.brand_nameasBRAND_NAME,round(avg(p.prod_price),2)asAVGPRICEfromlgbrandb,lgproductpwhereb.Brand_ID=p.Brand_IDgroupbyb.Brand_IDorderbyb.Brand_name;
13. Writeaquerytodisplaythedepartmentnumberandmostrecentemployeehiredateforeachdepartment.
Sorttheoutputbydepartmentnumber.uselargeco;selectdept_num,max(emp_hiredate)as'MostRecentHire'fromlgemployeegroupbydept_numorderbydept_num;
14. Writeaquerytodisplaytheemployeenumber, firstname, lastname,and largestsalaryamount foreachemployeeindepartment200.Sorttheoutputbylargestsalaryindescendingorder.
uselargeco;selecte.emp_num,e.emp_fname,e.emp_lname,max(s.sal_amount)asLargestSalaryfromlgemployeee,lgsalary_historys
Page11of21
wheree.emp_num=s.emp_numande.dept_num=200groupbyemp_numorderbyLargestSalarydesc;
15. Write a query to display the customer code, first name, last name, and sum of all invoice totals forcustomerswithcumulativeinvoicetotalsgreaterthan$1,500.Sorttheoutputbythesumofinvoicetotalsindescendingorder.
uselargeco;SelectC.Cust_Code,C.Cust_Fname,C.Cust_Lname,ROUND(SUM(I.Inv_Total),2)ASTotalInvoicesFromLGCUSTOMERC,LGINVOICEIWhereC.Cust_Code=I.Cust_CodeGroupByCust_CodeHavingTotalInvoices>=1500OrderByTotalInvoicesDESC;
Page12of21
16. Writeaquerytodisplaythedepartmentnumber,departmentname,departmentphonenumber,employeenumber,andlastnameofeachdepartmentmanager.Sorttheoutputbydepartmentname.
uselargeco;selectd.dept_num,d.dept_name,d.dept_phone,d.emp_num,e.emp_lnamefromlgdepartmentd,lgemployeeewheree.dept_num=d.dept_numande.emp_num=d.emp_numorderbyd.dept_name;
17. WriteaquerytodisplaythevendorID,vendorname,brandname,andnumberofproductsofeachbrandsuppliedbyeachvendor.Sorttheoutputbyvendornameandthenbybrandname.
uselargeco;selectv.vend_id,v.vend_name,b.brand_name,count(p.prod_sku)asNumProductsfromlgvendorv,lgproductp,lgbrandb,lgsuppliesswherep.brand_ID=b.brand_IDands.prod_sku=p.prod_skuandv.vend_ID=s.vend_ID
Page13of21
groupbybrand_name,vend_nameorderbyvend_name,brand_name;
18. Write a query to display the employee number, last name, first name, and sum of invoice totals for allemployeeswhocompletedaninvoice.Sorttheoutputbyemployeelastnameandthenbyfirstname.
uselargeco;select e.emp_num as EMP_NUM, e.emp_lname as EMP_LNAME, e.emp_fname as EMP_FNAME,round(sum(inv_total),2)asTOTALINVOICESfromlgemployeee,lginvoiceiwheree.Emp_num=i.employee_IDgroupbyEMP_NUMorderbyEMP_LNAME;
Page14of21
19. Writeaquerytodisplaythelargestaverageproductpriceofanybrand.uselargeco;selectmax(avgprice)as'LargestAverage'from(selectbrand_id,round(avg(prod_price),2)asavgpricefromlgproductpgroupbybrand_id)a;
20. WriteaquerytodisplaythebrandID,brandname,brandtype,andaveragepriceofproductsforthebrandthathasthelargestaverageproductprice.
uselargeco;selectp.brand_ID,p.brand_name,p.brand_type,round(max(p.AvgPrice),2)asMaxAvgPricefromlgbrandb, (selectbb.brand_ID,bb.brand_name,bb.brand_type,avg(pp.prod_price)asAvgPricefromlgbrandbb,lgproductppwherepp.brand_ID=bb.brand_IDgroupbybrand_ID)pgroupbybrand_IDorderbyAvgPricedesclimit1;
21. Write a query to display the manager name, department name, department phone number, employee
name,customername, invoicedate,and invoice total for thedepartmentmanagerof theemployeewhomadeasaletoacustomerwhoselastnameisHaganonMay18,2013.Forallpersonnames,concatenatethefirstandlastnamesintoasinglefield.
uselargeco;SelectCONCAT(m.Emp_Fname,"",m.Emp_Lname)AsManagerName,d.Dept_Name,d.Dept_Phone,CONCAT(e.Emp_Fname,"",e.Emp_Lname)AsEmployeeName,CONCAT(c.Cust_Fname,"",c.Cust_Lname)AsCustomerName,i.Inv_Date,i.Inv_TotalFromLGDEPARTMENTd,LGEMPLOYEEm,LGEMPLOYEEe,LGCUSTOMERc,LGINVOICEiWhered.Emp_Num=m.Emp_NumAnde.Emp_Num=i.Employee_IDAndd.Dept_Num=e.Dept_NumAndi.Cust_Code=c.Cust_CodeAndc.Cust_Lname='hagan'
Page15of21
andi.inv_date='2013-05-18'
22. Writeaquerytodisplaytheproductsthathaveapricegreaterthan$50.
uselargeco;
select*
fromlgproductp
havingp.prod_price>50;
23. Writeaquerytodisplaythecurrentsalaryforeachemployeeindepartment300.Assumethatonlycurrentemployeesarekeptinthesystem,andthereforethemostcurrentsalaryforeachemployeeistheentryinthesalaryhistorywithaNULLenddate.Sorttheoutputindescendingorderbysalaryamount.
uselargeco;selecte.emp_num,e.emp_lname,e.emp_fname,s.sal_amountfromlgemployeee,lgsalary_historyswheree.emp_num=s.emp_numandisnull(s.sal_end)ande.dept_num=300orderbys.sal_amountdesc;
Page16of21
24. Writeaquerytodisplaythestartingsalaryforeachemployee.Thestartingsalarywouldbetheentryinthe
salaryhistorywiththeoldestsalarystartdateforeachemployee.Sorttheoutputbyemployeenumber.uselargeco;select e.emp_num as Emp_Num, e.emp_lname as Emp_Lname, e.emp_fname as Emp_Fname,round(s.sal_amount,0)asSal_Amountfromlgemployeee,lgsalary_historyswheree.emp_num=s.emp_numandsal_from= (selectmin(sal_from) fromlgsalary_historys wheree.emp_num=s.emp_num)orderbyEmp_Num;
Page17of21
25. Writeaquerytodisplaytheinvoicenumber,linenumbers,productSKUs,productdescriptions,andbrandID
forsalesofsealerandtopcoatproductsofthesamebrandonthesameinvoice.Uselargeco;select c1.inv_num, c1.line_num, c1.prod_sku, c1.prod_description, c2.line_num, c2.prod_sku, c2.prod_description, c1.brand_id from (select l.inv_num, l.line_num, p.prod_sku, p.prod_description, p.brand_id, p.prod_category from lgline l, lgproduct p where l.prod_sku = p.prod_sku and p.prod_category = 'Sealer') c1, (select l2.line_num, p2.prod_sku, p2.prod_description, p2.brand_id, l2.inv_num, p2.prod_category from lgline l2, lgproduct p2 where l2.prod_sku = p2.prod_sku and p2.prod_category = 'Top Coat') c2 where c1.inv_num = c2.inv_num and c1.brand_id = c2.brand_id order by c1.inv_num, c1.line_num;
26. TheBinderPrimeCompanywantstorecognizetheemployeewhosoldthemostoftheirproductsduringa
specifiedperiod.Writeaquerytodisplaytheemployeenumber,employeefirstname,employeelastname,e-mail address, and total units sold for the employee who sold the most Binder Prime brand productsbetweenNovember1,2013,andDecember5,2013.Ifthereisatieformostunitssold,sorttheoutputbyemployeelastname.
uselargeco;selecte.emp_num,e.emp_fname,e.emp_lname,e.emp_email,sum(l.line_qty)asTotalfromlgemployeee,lginvoicei,lglinel,lgproductp,lgbrandbwhereb.brand_ID=p.brand_IDandp.prod_sku=l.prod_skuandl.inv_num=i.inv_numandi.employee_ID=e.emp_numandb.brand_name="BinderPrime"andinv_datebetween'2013-11-01'and'2013-12-05'groupbyemp_numorderbyTotaldesc,emp_lnamelimit2;
Page18of21
27. Writeaquery todisplay the customer code, firstname, and lastnameof all customerswhohavehadatleastone invoice completedbyemployee83649andat leastone invoice completedbyemployee83677.Sorttheoutputbycustomerlastnameandthenfirstname.
uselargeco;selectdistinctf1.cust_code,f1.cust_fname,f1.cust_lnamefrom ( selectc.cust_code,c.cust_fname,c.cust_lname fromlgcustomerc,lginvoicei wherec.cust_code=i.cust_code andemployee_ID=83649 )f1, ( selectc.cust_code,c.cust_fname,c.cust_lname fromlgcustomerc,lginvoicei wherec.cust_code=i.cust_code andemployee_ID=83677 )f2wheref1.cust_code=f2.cust_codeorderbyf1.cust_lname,f1.cust_fname;
28. LargeCoisplanninganewpromotioninAlabama(AL)andwantstoknowaboutthelargestpurchasesmadebycustomersinthatstate.Writeaquerytodisplaythecustomercode,customerfirstname,lastname,fulladdress, invoice date, and invoice total of the largest purchasemade by each customer in Alabama. Be
Page19of21
certaintoincludeanycustomersinAlabamawhohavenevermadeapurchase(theirinvoicedatesshouldbeNULLandtheinvoicetotalsshoulddisplayas0).
select c.cust_code, c.cust_fname, c.cust_lname, c.cust_street, c.cust_city, c.cust_state, c.cust_zip, i.inv_date, i.inv_total as 'Largest Invoice' from lgcustomer c, lginvoice i where i.cust_code = c.cust_code and c.cust_state = 'AL' and i.inv_total = (select max(i2.inv_total) from lginvoice i2 where i2.cust_code = c.cust_code) union select c2.cust_code, c2.cust_fname, c2.cust_lname, c2.cust_street, c2.cust_city, c2.cust_state, c2.cust_zip, null, 0 from lgcustomer c2 where c2.cust_state = 'AL' and c2.cust_code not in (select i3.cust_code from lginvoice i3) order by cust_lname, cust_fname;
29. Oneofthepurchasingmanagersisinterestedintheimpactofproductpricesonthesaleofproductsofeach
brand.Writeaquerytodisplaythebrandname,brandtype,averagepriceofproductsofeachbrand,andtotalunitssoldofproductsofeachbrand.Evenifaproducthasbeensoldmorethanonce,itspriceshouldonly be included once in the calculation of the average price. However, you must be careful becausemultipleproductsofthesamebrandcanhavethesameprice,andeachofthoseproductsmustbeincludedinthecalculationofthebrand’saverageprice.
uselargeco;selectf1.brand_name,f1.brand_type,round(f1.average_price,2)asAvgPrice,f2.amt_soldasUnitsSoldfrom(
select b.brand_name as brand_name, b.brand_type as brand_type, b.brand_id as brand_id,(sum(p.prod_price)/count(distinctp.prod_sku))asaverage_price
fromlgproductp,lgbrandbwhereb.brand_id=p.brand_id groupbyb.brand_idorderbyb.brand_name )f1,
Page20of21
(selectb.brand_idasbrand_id,sum(l.line_qty)asamt_soldfromlgbrandb,lglinel,lgproductpwhereb.brand_id=p.brand_idandp.prod_sku=l.prod_skugroupbyb.brand_idorderbyb.brand_name)f2wheref1.brand_id=f2.brand_id;
30. Thepurchasingmanagerisstillconcernedabouttheimpactofpriceonsales.Writeaquerytodisplaythebrand name, brand type, product SKU, product description, and price of any products that are not apremiumbrand,butthatcostmorethanthemostexpensivepremiumbrandproducts.
uselargeco;select b.brand_name as Brand_Name, b.brand_type as Brand_Type, p.prod_sku as Prod_Sku,p.prod_descriptionasProd_Descript,p.prod_priceasProd_Pricefromlgbrandb,lgproductpwhereb.brand_id=p.brand_idandb.brand_typenotin('PREMIUM')andp.prod_price> (Selectmax(p.prod_price) fromlgbrandb,lgproductpwhereb.brand_id=p.brand_idandb.brand_type='PREMIUM');
• Name the Word document – Project2 team##.doc.
• Include the names of the team members.
Page21of21
• Finally, submit your Word file on Blackboard. Only one submission is required per team.