Uploaded by Nicolas Nicolas

tut04

advertisement
CSCB20 - Tutorial 4
More SQL Fun
Learning Objectives
This lab focuses on getting you to play more with the advanced features of SQL in order to gain
experience with what it can do and what you can do with it.
Getting Started
We will be using examples from the following relations. You should start by creating these tables,
defining appropriate integrity constraints, and filling them with some sample data.
Artist(artist_id, name, nationality)
Group(group_id, group_name)
Album(album_id, album_name, num_tracks, explicit, year)
Track(track_id, album_id, track_name, length, explicit)
Member(artist_id, group_id)
Recorded(track_id, artist_id)
Review:
Basic Querying: We can get a lot of material with just the simple join/select/project model of
SELECT A FROM B JOIN C WHERE D queries.
Practice:
Try to complete the following queries (feel free to substitute the names of artists/bands/groups
to suit your musical tastes/databases):
• Find the names of all artists in the group ‘Cream’
• Find the names of all groups in which ‘Eric Clapton’ was a member
• Find the names of all artists who were in both ‘The Beatles’ and ‘The Travelling Wilburys’
• Find the titles of all tracks that ‘Pink Floyd’ recorded which were over 5min in length.
• Find the nationality of all artists who recorded a song
• Find the titles of all non-explicit songs that were on albums labeled as explicit
1
Advanced features
Review:
• Remember that we can use things like wildcards, grouping, and aggregation to perform more
complex queries
Practice:
Try to implement the following queries
• Find the names of all artists who were in the band ‘The Birds’, ‘The Byrds’, ‘The Birdz’, or
‘The Byrdz’
• Find the names of all albums with ‘best of’ in the title
• Find the names of top 10 songs by length
• Find the titles of the top 10 albums by length (sum of length of all songs on the album)
• Find the names of the top 10 artists by number of songs
• Find the names of the top 10 artists by total length of all songs
• Repeat all the above queries, but only for albums that came out in the 2000s for Canadian
artists (hint: using views makes this a lot simpler)
2
Download