MySQL ex exercises ercises Dr. Paolo Coletti – Free University of Bolzano Bozen 04 April 2016
Use database northwind2003
Build a query and, if possible, save it as a view, which 1. writes writes the custom customers ers list list !only !only customer customers s compan company y name name is display displayed" ed" with the number of orders handled by that employee with last name #$avolio% in &pril of all the years' result has 1 ro!s (. dis displa plays ys the list list of produ products cts whose whose )nits* )nits*nSt nStoc+ oc+ is smaller smaller than eord eorderL erLeve evel, l, in cate-ory alphabetical order and, within the same cate-ory, by product alphabetical order. order. nly cate-ory and products name are displayed. "esult has 1# ro!s /. writes the the list of orders with with total value value lar-er lar-er than 0. 2otal value value is the sum of )nit3r )nit3rice ice by Quanti Quantity ty by !14$is !14$iscou count" nt"'' result has $%4 ro!s &or 614 if you use UnitPri'e in ta(le Pro)u'ts* 5. dis displa plays ys the list of orders orders with the number of wee+s passed passed between between rder$a rder$ate te and Shipped$ate' result has #0 ro!s 0. displays displays the list list of Suppliers Suppliers and their their orders *$ which which involve involve a Seafood Seafood products products and are handled by #$avolio% !6elds displayed7 8ompany9ame and rder*$". $o not display the same row twice' result has 4+ ro!s &an) not $%* :. calculates calculates for each supplier supplier the the total money -enerate -enerated d by its products products !i-nor !i-nore e the discount" in year 1;;0 !6elds displayed7 8ompany9ame, total paid amount"' result has 2% ro!s <. dis displa plays ys the list of custom customers ers to-ethe to-etherr with with all their their Seafood Seafood produc products ts ordered ordered durin- year 1;;0, sorted by companys name !6elds displayed7 8ompany9ame, 3roduct". =ach couple company9ame43roduct9ame should appear only once' result has 12 ro!s ¬ 66, nor 161*
1
>. calculates for each customer the total money paid !i-nore the discount" for each cate-ory of products ordered in year 1;;0 !6elds displayed7 8ompany9ame, 8ate-ory9ame, total paid amount". "esult has 444 ro!s ¬ #*
Use database northwind2003_free 11.&dd a chec+ constraint which chec+s that Birth$ate be at least 1> years before today and before ?ire$ate. 1(.&dd a chec+ constraint which chec+s that )nit3rice be null or not ne-ative. 1/.Build, throu-h an appropriate table and relation, a mandatory 6eld 2itlef8ourtesy with values #Mr%, #Miss%, #Mrs%.
Use database studentsandexams
Build a query and, if possible, save it as a view, which (1.shows in the 6rst column the year and in the second column how many exams with at least (/ have been passed in that year by all the students in the database' result has 11 ro!s ((.calculates for every student !students lastname and number" the avera-e of his@her -rades received after 1@1@(;' result has #6 ro!s, Alfarei has avera-e -ra)e 26.$ (/.calculates for each student !students lastname and wei-hted avera-e" his wei-hted avera-e -rade usin- as wei-hts the exams credits' this uery is 'hallen-in-, if you !ant you 'an 'reate a vie! an) use it !ith a se'on) uery, even thou-h it 'an (e solve) !ith a sin-le uery/ result has %+ ro!s, stu)ent #0 has avera-e 2,%+06 (5.displays the courses held in 6rst semester with their professor !course name and professors lastname". "esult has 1 ro!s
Use database studentsandexams_free /1.Build, throu-h an appropriate table and relation, a mandatory 6eld Lan-ua-e with values #=n-lish%, #Aerman%, #*talian%.
(
Use database sakila
Build a query, and if possible save it as a view, which 51.displays all documentary 6lms, with their lan-ua-e' result has 6# ro!s 5(.displays, for all documentary 6lms that we have in inventory, the rental dates and the customer who rented it' result has 10$0 ro!s 5/.displays the 6lm cate-ories names with how many 6lm copies per cate-ory do we have in our inventory' result has 16 ro!s, a'tion ovies are 12 55.displays for each customer the sum of money that he has paid' result has $%% ro!s 50.displays the list of stores with the name and surname of the responsible person' result has 2 ro!s 5:.displays the list of not correct customers email addresses !not in the characterscharacters.extension format"' result has 2 ro!s 5<.counts how many movies have been rented per employee' result has $ ro!s, eployees have less than 4 ovies rente) 5>.calculates the avera-e payment per customer' result has $%% ro!s 5;.calculates the avera-e payment and the sum of payments per employee' result has $ ro!s, eployees have very lo! sus 0.displays the movie titles in order of number of rentals, from the top rented to the less rented !use the records in 6lms and not in inventory"' result has %$# ro!s an) starts !ith Bu'et Brotherhoo) 01.counts the number of 6lm copies that we have per actor, ordered in actors surname alphabetical order' result has 200 ro!s an) starts !ith Aroy) Christian !ith 1$$ 'opies/ 0(.calculates the avera-e 6lm len-th per cate-ory' result has 16 ro!s
/
Use database sakila_free :1.Build a query and save it as a view which displays all rentals, with surname of the customer and of the employee who -ave the 6lm and the store to which employee belon-s, assi-nin- to each 6eld a nic+name. 2hen build another query which ta+es data from this view !so not from the ori-inal tables, thus use the nic+names" and, Coinin- it with table employees a-ain, display also the surname of the responsible of the store, everythin- sorted by employees surname !not responsible". "esult has 16044 ro!s an) starts !ith rentals fro Ba'h eployee.
5