You are on page 1of 4

MySQL exercises

Dr. Paolo Coletti – Free University of Bolzano Bozen

01 July 2018

Use database northwind2003

Build a query and, if possible, save it as a view, which


1. writes the customers’ list (only customer’s company name is displayed) with the number of orders
handled by that employee with last name “Davolio” in April of all the years; result has 13 rows
2. displays the list of products whose UnitsInStock is smaller than ReorderLevel, in category alphabetical
order and, within the same category, by product alphabetical order. Only category and product’s name
are displayed. Result has 18 rows
3. writes the list of orders with total value larger than 500. Total value is the sum of UnitPrice by Quantity
by (1-Discount); result has 594 rows (or 614 if you use UnitPrice in table Products)
4. displays the list of orders with the number of weeks passed between OrderDate and ShippedDate;
result has 830 rows
5. displays the list of Suppliers and their orders ID which involve a Seafood products and are handled by
“Davolio” (fields displayed: CompanyName and OrderID). Do not display the same row twice; result has
47 rows (and not 59)
6. calculates for each supplier the total money generated by its products (ignore the discount) in year
1995 (fields displayed: CompanyName, total paid amount); result has 29 rows
7. displays the list of customers together with all their Seafood products ordered during year 1995, sorted
by company’s name (fields displayed: CompanyName, Product). Each couple companyName-
ProductName should appear only once; result has 12 rows (not 66, nor 161)
8. calculates for each customer the total money paid (ignore the discount) for each category of products
ordered in year 1995 (fields displayed: CompanyName, CategoryName, total paid amount). Result has
444 rows (not 8)

1
Use database northwind2003_free
11. Add a check constraint which checks that BirthDate be at least 18 years before today and before
HireDate.
12. Add a check constraint which checks that UnitPrice be null or not negative.
13. Build, through an appropriate table and relation, a mandatory field TitleOfCourtesy with values “Mr”,
“Miss”, “Mrs”.

Use database studentsandexams

Build a query and, if possible, save it as a view, which


21. shows in the first column the year and in the second column how many exams with at least 23 have
been passed in that year by all the students in the database; result has 11 rows
22. calculates for every student (student’s lastname and number) the average of his/her grades received
after 1/1/2009; result has 86 rows, Alfarei has average grade 26.5
23. calculates for each student (student’s lastname and weighted average) his weighted average grade
using as weights the exam’s credits; this query is challenging, if you want you can create a view and use
it with a second query, even though it can be solved with a single query; result has 97 rows, student 803
has average 23,9706
24. displays the courses held in first semester with their professor (course name and professor’s lastname).
Result has 13 rows

Use database studentsandexams_free


31. Build, through an appropriate table and relation, a mandatory field Language with values “English”,
“German”, “Italian”.

2
Use database sakila

Build a query, and if possible save it as a view, which


41. displays all documentary films, with their language; result has 68 rows
42. displays, for all documentary films that we have in inventory, the rental dates and the customer who
rented it; result has 1050 rows
43. displays the film categories’ names with how many film copies per category do we have in our
inventory; result has 16 rows, action movies are 312
44. displays for each customer the sum of money that he has paid; result has 599 rows
45. displays the list of stores with the name and surname of the responsible person; result has 2 rows
46. displays the list of not correct customers’ email addresses (not in the characters@characters.extension
format); result has 2 rows
47. counts how many movies have been rented per employee; result has 5 rows, 3 employees have less
than 4 movies rented
48. calculates the average payment per customer; result has 599 rows
49. calculates the average payment and the sum of payments per employee; result has 5 rows, 3
employees have very low sums
50. displays the movie titles in order of number of rentals, from the top rented to the less rented (use the
records in films and not in inventory); result has 958 rows and starts with Bucket Brotherhood
51. counts the number of film copies that we have per actor, ordered in actor’s surname alphabetical
order; result has 200 rows and starts with Arkoyd Christian with 155 copies;
52. calculates the average film length per category; result has 16 rows

Use database sakila_free


61. Build a query and save it as a view which displays all rentals, with surname of the customer and of the
employee who gave the film and the store to which employee belongs, assigning to each field a
nickname. Then build another query which takes data from this view (so not from the original tables,

3
thus use the nicknames) and, joining it with table employees again, display also the surname of the
responsible of the store, everything sorted by employee’s surname (not responsible). Result has 16044
rows and starts with rentals from Bach employee.

You might also like