Create Database Songdb USE Songdb
Create Database Songdb USE Songdb
Link: https://github.com/WebDevSimplified/Learn-SQL
This table should be called songs and have four properties with these exact names.
id: An integer that is the primary key, and auto increments.
name: A string that cannot be null.
length: A float that represents the length of the song in minutes that cannot be null.
album_id: An integer that is a foreign key referencing the albums table that cannot be null.
2. After successfully creating the table copy the code from data.sql into MySQL Workbench, and
run it to populate all of the data for the rest of the exercises. If you do not encounter any errors,
then your answer is most likely correct.
INSERT the Following:
Change the name of the column the data returns to Band Name:
4. Select the Oldest Album: Make sure to only return one result from this query, and that you are
not returning any albums that do not have a release year.
Try this Code:
6. Get all Bands that have No Albums: This is very similar to #4 but will require more than just a
join.
Return the band name as Band Name. (Try this CODE)
Return the album name as Name, the album release year as Release Year, and the album length
as Duration. TRY this CODE:
SELECT
albums.name as Name,
albums.release_year as 'Release Year',
SUM(songs.length) as 'Duration'
FROM albums
JOIN songs on albums.id = songs.album_id
GROUP BY songs.album_id
ORDER BY Duration DESC
LIMIT 1;
8. Update the Release Year of the Album with no Release Year: Set the release year to 1986.
You may run into an error if you try to update the release year by using release_year IS NULL in
the WHERE statement of your UPDATE. This is because MySQL Workbench by default will not let
you update a table that has a primary key without using the primary key in the UPDATE
statement. This is a good thing since you almost never want to update rows without using the
primary key, so to get around this error make sure to use the primary key of the row you want
to update in the WHERE of the UPDATE statement. TRY THIS CODE:
UPDATE albums
SET release_year = 1986
WHERE id = 4;
9. Insert a record for your favorite Band and one of their Albums: If you performed this correctly
you should be able to now see that band and album in your tables. Try this CODE:
11. Get the Average Length of all Songs: Return the average length as Average Song Duration. (Try
this CODE)
12. Select the longest Song off each Album: Return the album name as Album, the album release
year as Release Year, and the longest song length as Duration. Try this CODE:
SELECT
albums.name AS 'Album',
albums.release_year AS 'Release Year',
MAX(songs.length) AS 'Duration'
FROM albums
JOIN songs ON albums.id = songs.album_id
GROUP BY songs.album_id;
13. Get the number of Songs for each Band: This is one of the toughest question on the list. It will
require you to chain together two joins instead of just one.
Return the band name as Band, the number of songs as Number of Songs. Try this CODE:
SELECT
bands.name AS 'Band',
COUNT(songs.id) AS 'Number of Songs'
FROM bands
JOIN albums ON bands.id = albums.band_id
JOIN songs ON albums.id = songs.album_id
GROUP BY albums.band_id;
NOTE: Try this CODE to SEARCH ALL TOGETHER: