DB Assignment ENONCE
DB Assignment ENONCE
A word on keys : Primary keys are indicated by underlined attributes. Arrows indicate that the attribute is a foreign key
referring to the pointed to attributed.
2 Questions
2.1 Queries
1. Write a query returning the first and last names of the employees whose title is ’Sales Support Agent’.
Test. Hint : There are 3 sales support agents.
2. Write a query returning without duplicates the list of artists who performed at least one ’Jazz’ song.
Test. Hint : 10 lines.
3. Write a query returning for each customer their assigned support rep employee. Rename the columns to clearly indicate
which names are the customers names and which names are the employees names.
Test. Hint : 59 lines, only the 3 names from the first query should appear for the employees.
5. Write a query returning the minimum and maximum price for a transaction among transaction that included at least
one ’Metal’ track.
Test. Hint : Max is 21.86, min is 0.99.
6. Write a query returning for each sales support agent the number of customers assigned to them.
Test. Hint : Jane has 21, Margaret 20, and Steve 18.
7. Write a query returning the names of the artists who performed at least 10 tracks of more than 5 minutes.
Test. Hint : 17 lines.
8. Write a query returning without duplicates the names of the employees who have at least one other employee reporting
to them.
Test. Hint : There are three : Andrew, Nancy, and Michael.
9. Write a query returning the ’Rock’ tracks that appear on more than 3 playlists.
Test. Hint : 17 lines.
10. Write a query returning the names of the genres having strictly more albums than ’Jazz’.
Test. Hint : 5 lines.
11. Write a query returning the names and total amount of money spent by customers who spent more money than the
average customer.
Test. Hint : 22 lines.
12. Write a query returning the names of customers who bought at least one track of every media type.
Test. Hint : There is only one : Bjørn Hansen.
2.2 Transcription
For each of the following SQL query, write a sentence (in english) describing what it returns, in the same manner as the
questions above.
13.
SELECT DISTINCT customers.FirstName, customers.LastName
FROM customers JOIN invoices ON customers.CustomerId = invoices.CustomerId
JOIN invoice_items ON invoices.InvoiceId = invoice_items.InvoiceId
JOIN tracks ON invoice_items.TrackId = tracks.TrackId
JOIN albums ON tracks.AlbumId = albums.AlbumId
JOIN artists ON albums.ArtistId = artists.ArtistId
14.
SELECT artists.Name
FROM artists JOIN albums ON artists.ArtistId = albums.ArtistId
JOIN tracks ON albums.AlbumId = tracks.AlbumId
JOIN invoice_items ON tracks.TrackId = invoice_items.TrackId
GROUP BY artists.ArtistId
HAVING SUM(invoice_items.Quantity) = (SELECT MAX(T)
FROM (SELECT SUM(invoice_items.Quantity) AS T
FROM albums JOIN tracks ON albums.AlbumId = tracks.AlbumId
JOIN invoice_items ON tracks.TrackId = invoice_items.TrackId
GROUP BY albums.ArtistId))
15.
SELECT c.FirstName, c.LastName
FROM customers AS c
WHERE EXISTS (SELECT *
FROM albums AS a
WHERE NOT EXISTS (SELECT *
FROM tracks
WHERE tracks.AlbumId = a.AlbumId
AND tracks.TrackId NOT IN (SELECT ii.TrackId
FROM invoice_items AS ii JOIN invoices AS i
ON ii.InvoiceId = i.InvoiceId
WHERE i.CustomerId = c.CustomerId)))