Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Exercises for beginners to learn SQL

NotificationsYou must be signed in to change notification settings

WebDevSimplified/Learn-SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

If you haven't already make sure you watchthis video which will teach you all the basics of SQL in 60 minutes.

After watching the video try to complete the exercises listed below using the data provided in this repository.

All of the solutions are available in the repository, andthis video goes over all of the solutions.

Setup

First drop your existing database that was created in the tutorial.DROP DATABASE record_company;

Copy the code inside theschema.sql file, paste it into MySQL Workbench, and run it. (This file contains the code necessary to create and add the tables from the tutorial video)

Exercises

1. Create a Songs Table

Solution

This table should be calledsongs and have four properties with these exact names.

  1. id: An integer that is the primary key, and auto increments.
  2. name: A string that cannot be null.
  3. length: A float that represents the length of the song in minutes that cannot be null.
  4. album_id: An integer that is a foreign key referencing the albums table that cannot be null.

After successfully creating the table copy the code fromdata.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.

2. Select only the Names of all the Bands

Solution

Change the name of the column the data returns toBand Name

Band Name
Seventh Wonder
Metallica
The Ocean
Within Temptation
Death
Van Canto
Dream Theater

3. Select the Oldest Album

Solution

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.

idnamerelease_yearband_id
5...And Justice for All19882

4. Get all Bands that have Albums

Solution

There are multiple different ways to solve this problem, but they will all involve a join.

Return the band name asBand Name.

Band Name
Seventh Wonder
Metallica
The Ocean
Within Temptation
Death
Van Canto

5. Get all Bands that have No Albums

Solution

This is very similar to #4 but will require more than just a join.

Return the band name asBand Name.

Band Name
Dream Theater

6. Get the Longest Album

Solution

This problem sounds a lot like #3 but the solution is quite a bit different. I would recommend looking up the SUM aggregate function.

Return the album name asName, the album release year asRelease Year, and the album length asDuration.

NameRelease YearDuration
Death Magnetic200874.76666593551636

7. Update the Release Year of the Album with no Release Year

Solution

Set the release year to 1986.

You may run into an error if you try to update the release year by usingrelease_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.

8. Insert a record for your favorite Band and one of their Albums

Solution

If you performed this correctly you should be able to now see that band and album in your tables.

9. Delete the Band and Album you added in #8

Solution

The order of how you delete the records is important since album has a foreign key to band.

10. Get the Average Length of all Songs

Solution

Return the average length asAverage Song Duration.

Average Song Duration
5.352472513259112

11. Select the longest Song off each Album

Solution

Return the album name asAlbum, the album release year asRelease Year, and the longest song length asDuration.

AlbumRelease YearDuration
Tiara20189.5
The Great Escape201030.2333
Mercy Falls20089.48333
Master of Puppets19868.58333
...And Justice for All19889.81667
Death Magnetic20089.96667
Heliocentric20107.48333
Pelagial20139.28333
Anthropocentric20109.4
Resist20185.85
The Unforgiving20115.66667
Enter19977.25
The Sound of Perseverance19988.43333
Individual Thought Patterns19934.81667
Human19914.65
A Storm to Come20065.21667
Break the Silence20116.15
Tribe of Force20108.38333

12. Get the number of Songs for each Band

Solution

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 asBand, the number of songs asNumber of Songs.

BandNumber of Songs
Seventh Wonder35
Metallica27
The Ocean31
Within Temptation30
Death27
Van Canto32

About

Exercises for beginners to learn SQL

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp