├── README.md ├── data.sql ├── schema.sql └── solutions ├── 1.sql ├── 10.sql ├── 11.sql ├── 12.sql ├── 2.sql ├── 3.sql ├── 4.sql ├── 5.sql ├── 6.sql ├── 7.sql ├── 8.sql └── 9.sql /README.md: -------------------------------------------------------------------------------- 1 | # Learn SQL 2 | If you haven't already make sure you watch [this video](https://youtu.be/p3qvj9hO_Bo) which will teach you all the basics of SQL in 60 minutes. 3 | 4 | After watching the video try to complete the exercises listed below using the data provided in this repository. 5 | 6 | All of the solutions are available in the repository, and [this video](https://youtu.be/30W5wjgJR08) goes over all of the solutions. 7 | 8 | ## Setup 9 | First drop your existing database that was created in the tutorial. `DROP DATABASE record_company;` 10 | 11 | Copy the code inside the [schema.sql](schema.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) 12 | 13 | ## Exercises 14 | ### 1. Create a Songs Table 15 | [Solution](solutions/1.sql) 16 | 17 | This table should be called `songs` and have four properties with these exact names. 18 | 1. `id`: An integer that is the primary key, and auto increments. 19 | 2. `name`: A string that cannot be null. 20 | 3. `length`: A float that represents the length of the song in minutes that cannot be null. 21 | 4. `album_id`: An integer that is a foreign key referencing the albums table that cannot be null. 22 | 23 | After successfully creating the table copy the code from [data.sql](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. 24 | 25 | ### 2. Select only the Names of all the Bands 26 | [Solution](solutions/2.sql) 27 | 28 | Change the name of the column the data returns to `Band Name` 29 | 30 | | Band Name | 31 | |-------------------| 32 | | Seventh Wonder | 33 | | Metallica | 34 | | The Ocean | 35 | | Within Temptation | 36 | | Death | 37 | | Van Canto | 38 | | Dream Theater | 39 | 40 | ### 3. Select the Oldest Album 41 | [Solution](solutions/3.sql) 42 | 43 | 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. 44 | 45 | | id | name | release_year | band_id | 46 | |----|------------------------|--------------|---------| 47 | | 5 | ...And Justice for All | 1988 | 2 | 48 | 49 | ### 4. Get all Bands that have Albums 50 | [Solution](solutions/4.sql) 51 | 52 | There are multiple different ways to solve this problem, but they will all involve a join. 53 | 54 | Return the band name as `Band Name`. 55 | 56 | | Band Name | 57 | |-------------------| 58 | | Seventh Wonder | 59 | | Metallica | 60 | | The Ocean | 61 | | Within Temptation | 62 | | Death | 63 | | Van Canto | 64 | 65 | ### 5. Get all Bands that have No Albums 66 | [Solution](solutions/5.sql) 67 | 68 | This is very similar to #4 but will require more than just a join. 69 | 70 | Return the band name as `Band Name`. 71 | 72 | | Band Name | 73 | |---------------| 74 | | Dream Theater | 75 | 76 | ### 6. Get the Longest Album 77 | [Solution](solutions/6.sql) 78 | 79 | This problem sounds a lot like #3 but the solution is quite a bit different. I would recommend looking up the SUM aggregate function. 80 | 81 | Return the album name as `Name`, the album release year as `Release Year`, and the album length as `Duration`. 82 | 83 | | Name | Release Year | Duration | 84 | |----------------|--------------|-------------------| 85 | | Death Magnetic | 2008 | 74.76666593551636 | 86 | 87 | ### 7. Update the Release Year of the Album with no Release Year 88 | [Solution](solutions/7.sql) 89 | 90 | Set the release year to 1986. 91 | 92 | 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. 93 | 94 | ### 8. Insert a record for your favorite Band and one of their Albums 95 | [Solution](solutions/8.sql) 96 | 97 | If you performed this correctly you should be able to now see that band and album in your tables. 98 | 99 | ### 9. Delete the Band and Album you added in #8 100 | [Solution](solutions/9.sql) 101 | 102 | The order of how you delete the records is important since album has a foreign key to band. 103 | 104 | ### 10. Get the Average Length of all Songs 105 | [Solution](solutions/10.sql) 106 | 107 | Return the average length as `Average Song Duration`. 108 | 109 | | Average Song Duration | 110 | |-----------------------| 111 | | 5.352472513259112 | 112 | 113 | 114 | ### 11. Select the longest Song off each Album 115 | [Solution](solutions/11.sql) 116 | 117 | Return the album name as `Album`, the album release year as `Release Year`, and the longest song length as `Duration`. 118 | 119 | | Album | Release Year | Duration | 120 | |-----------------------------|--------------|----------| 121 | | Tiara | 2018 | 9.5 | 122 | | The Great Escape | 2010 | 30.2333 | 123 | | Mercy Falls | 2008 | 9.48333 | 124 | | Master of Puppets | 1986 | 8.58333 | 125 | | ...And Justice for All | 1988 | 9.81667 | 126 | | Death Magnetic | 2008 | 9.96667 | 127 | | Heliocentric | 2010 | 7.48333 | 128 | | Pelagial | 2013 | 9.28333 | 129 | | Anthropocentric | 2010 | 9.4 | 130 | | Resist | 2018 | 5.85 | 131 | | The Unforgiving | 2011 | 5.66667 | 132 | | Enter | 1997 | 7.25 | 133 | | The Sound of Perseverance | 1998 | 8.43333 | 134 | | Individual Thought Patterns | 1993 | 4.81667 | 135 | | Human | 1991 | 4.65 | 136 | | A Storm to Come | 2006 | 5.21667 | 137 | | Break the Silence | 2011 | 6.15 | 138 | | Tribe of Force | 2010 | 8.38333 | 139 | 140 | ### 12. Get the number of Songs for each Band 141 | [Solution](solutions/12.sql) 142 | 143 | This is one of the toughest question on the list. It will require you to chain together two joins instead of just one. 144 | 145 | Return the band name as `Band`, the number of songs as `Number of Songs`. 146 | 147 | | Band | Number of Songs | 148 | |-------------------|-----------------| 149 | | Seventh Wonder | 35 | 150 | | Metallica | 27 | 151 | | The Ocean | 31 | 152 | | Within Temptation | 30 | 153 | | Death | 27 | 154 | | Van Canto | 32 | 155 | -------------------------------------------------------------------------------- /data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO bands(id,name) VALUES (1,'Seventh Wonder'); 2 | INSERT INTO bands(id,name) VALUES (2,'Metallica'); 3 | INSERT INTO bands(id,name) VALUES (3,'The Ocean'); 4 | INSERT INTO bands(id,name) VALUES (4,'Within Temptation'); 5 | INSERT INTO bands(id,name) VALUES (5,'Death'); 6 | INSERT INTO bands(id,name) VALUES (6,'Van Canto'); 7 | INSERT INTO bands(id,name) VALUES (7,'Dream Theater'); 8 | 9 | INSERT INTO albums(id,name,release_year,band_id) VALUES (1,'Tiara',2018,1); 10 | INSERT INTO albums(id,name,release_year,band_id) VALUES (2,'The Great Escape',2010,1); 11 | INSERT INTO albums(id,name,release_year,band_id) VALUES (3,'Mercy Falls',2008,1); 12 | INSERT INTO albums(id,name,release_year,band_id) VALUES (4,'Master of Puppets',NULL,2); 13 | INSERT INTO albums(id,name,release_year,band_id) VALUES (5,'...And Justice for All',1988,2); 14 | INSERT INTO albums(id,name,release_year,band_id) VALUES (6,'Death Magnetic',2008,2); 15 | INSERT INTO albums(id,name,release_year,band_id) VALUES (7,'Heliocentric',2010,3); 16 | INSERT INTO albums(id,name,release_year,band_id) VALUES (8,'Pelagial',2013,3); 17 | INSERT INTO albums(id,name,release_year,band_id) VALUES (9,'Anthropocentric',2010,3); 18 | INSERT INTO albums(id,name,release_year,band_id) VALUES (10,'Resist',2018,4); 19 | INSERT INTO albums(id,name,release_year,band_id) VALUES (11,'The Unforgiving',2011,4); 20 | INSERT INTO albums(id,name,release_year,band_id) VALUES (12,'Enter',1997,4); 21 | INSERT INTO albums(id,name,release_year,band_id) VALUES (13,'The Sound of Perseverance',1998,5); 22 | INSERT INTO albums(id,name,release_year,band_id) VALUES (14,'Individual Thought Patterns',1993,5); 23 | INSERT INTO albums(id,name,release_year,band_id) VALUES (15,'Human',1991,5); 24 | INSERT INTO albums(id,name,release_year,band_id) VALUES (16,'A Storm to Come',2006,6); 25 | INSERT INTO albums(id,name,release_year,band_id) VALUES (17,'Break the Silence',2011,6); 26 | INSERT INTO albums(id,name,release_year,band_id) VALUES (18,'Tribe of Force',2010,6); 27 | 28 | INSERT INTO songs(id,name,length,album_id) VALUES (1,'Arrival',1+(30/60),1); 29 | INSERT INTO songs(id,name,length,album_id) VALUES (2,'The Everones',6+(13/60),1); 30 | INSERT INTO songs(id,name,length,album_id) VALUES (3,'Dream Machines',5+(38/60),1); 31 | INSERT INTO songs(id,name,length,album_id) VALUES (4,'Against the Grain',6+(58/60),1); 32 | INSERT INTO songs(id,name,length,album_id) VALUES (5,'Victorious',4+(55/60),1); 33 | INSERT INTO songs(id,name,length,album_id) VALUES (6,'Tiara''s Song (Farewell Pt. 1)',7+(16/60),1); 34 | INSERT INTO songs(id,name,length,album_id) VALUES (7,'Goodnight (Farewell Pt. 2)',7+(10/60),1); 35 | INSERT INTO songs(id,name,length,album_id) VALUES (8,'Beyond Today (Farewell Pt. 3)',5+(06/60),1); 36 | INSERT INTO songs(id,name,length,album_id) VALUES (9,'The Truth',4+(17/60),1); 37 | INSERT INTO songs(id,name,length,album_id) VALUES (10,'By the Light of the Funeral Pyres',3+(54/60),1); 38 | INSERT INTO songs(id,name,length,album_id) VALUES (11,'Damnation Below',6+(44/60),1); 39 | INSERT INTO songs(id,name,length,album_id) VALUES (12,'Procession',0+(45/60),1); 40 | INSERT INTO songs(id,name,length,album_id) VALUES (13,'Exhale',9+(30/60),1); 41 | INSERT INTO songs(id,name,length,album_id) VALUES (14,'Wiseman',5+(42/60),2); 42 | INSERT INTO songs(id,name,length,album_id) VALUES (15,'Alley Cat',6+(06/60),2); 43 | INSERT INTO songs(id,name,length,album_id) VALUES (16,'The Angelmaker',8+(29/60),2); 44 | INSERT INTO songs(id,name,length,album_id) VALUES (17,'King of Whitewater',7+(20/60),2); 45 | INSERT INTO songs(id,name,length,album_id) VALUES (18,'Long Way Home',4+(26/60),2); 46 | INSERT INTO songs(id,name,length,album_id) VALUES (19,'Move on Through',5+(04/60),2); 47 | INSERT INTO songs(id,name,length,album_id) VALUES (20,'The Great Escape',30+(14/60),2); 48 | INSERT INTO songs(id,name,length,album_id) VALUES (21,'A New Beginning',3+(05/60),3); 49 | INSERT INTO songs(id,name,length,album_id) VALUES (22,'There and Back',3+(02/60),3); 50 | INSERT INTO songs(id,name,length,album_id) VALUES (23,'Welcome to Mercy Falls',5+(11/60),3); 51 | INSERT INTO songs(id,name,length,album_id) VALUES (24,'Unbreakable',7+(19/60),3); 52 | INSERT INTO songs(id,name,length,album_id) VALUES (25,'Tears for a Father',1+(58/60),3); 53 | INSERT INTO songs(id,name,length,album_id) VALUES (26,'A Day Away',3+(43/60),3); 54 | INSERT INTO songs(id,name,length,album_id) VALUES (27,'Tears for a Son',1+(42/60),3); 55 | INSERT INTO songs(id,name,length,album_id) VALUES (28,'Paradise',5+(46/60),3); 56 | INSERT INTO songs(id,name,length,album_id) VALUES (29,'Fall in Line',6+(09/60),3); 57 | INSERT INTO songs(id,name,length,album_id) VALUES (30,'Break the Silence',9+(29/60),3); 58 | INSERT INTO songs(id,name,length,album_id) VALUES (31,'Hide and Seek',7+(46/60),3); 59 | INSERT INTO songs(id,name,length,album_id) VALUES (32,'Destiny Calls',6+(18/60),3); 60 | INSERT INTO songs(id,name,length,album_id) VALUES (33,'One Last Goodbye',4+(21/60),3); 61 | INSERT INTO songs(id,name,length,album_id) VALUES (34,'Back in Time',1+(14/60),3); 62 | INSERT INTO songs(id,name,length,album_id) VALUES (35,'The Black Parade',6+(57/60),3); 63 | INSERT INTO songs(id,name,length,album_id) VALUES (36,'Battery',5+(13/60),4); 64 | INSERT INTO songs(id,name,length,album_id) VALUES (37,'Master of Puppets',8+(35/60),4); 65 | INSERT INTO songs(id,name,length,album_id) VALUES (38,'The Thing That Should Not Be',6+(36/60),4); 66 | INSERT INTO songs(id,name,length,album_id) VALUES (39,'Welcome Home (Sanitarium)',6+(27/60),4); 67 | INSERT INTO songs(id,name,length,album_id) VALUES (40,'Disposable Heroes',8+(17/60),4); 68 | INSERT INTO songs(id,name,length,album_id) VALUES (41,'Leper Messiah',5+(40/60),4); 69 | INSERT INTO songs(id,name,length,album_id) VALUES (42,'Orion',8+(27/60),4); 70 | INSERT INTO songs(id,name,length,album_id) VALUES (43,'Damage Inc.',5+(32/60),4); 71 | INSERT INTO songs(id,name,length,album_id) VALUES (44,'Blackened',6+(41/60),5); 72 | INSERT INTO songs(id,name,length,album_id) VALUES (45,'...And Justice for All',9+(47/60),5); 73 | INSERT INTO songs(id,name,length,album_id) VALUES (46,'Eye of the Beholder',6+(30/60),5); 74 | INSERT INTO songs(id,name,length,album_id) VALUES (47,'One',7+(27/60),5); 75 | INSERT INTO songs(id,name,length,album_id) VALUES (48,'The Shortest Straw',6+(36/60),5); 76 | INSERT INTO songs(id,name,length,album_id) VALUES (49,'Harvester of Sorrow',5+(46/60),5); 77 | INSERT INTO songs(id,name,length,album_id) VALUES (50,'The Frayed Ends of Sanity',7+(44/60),5); 78 | INSERT INTO songs(id,name,length,album_id) VALUES (51,'To Live Is to Die',9+(49/60),5); 79 | INSERT INTO songs(id,name,length,album_id) VALUES (52,'Dyers Eve',5+(13/60),5); 80 | INSERT INTO songs(id,name,length,album_id) VALUES (53,'That Was Just Your Life',7+(08/60),6); 81 | INSERT INTO songs(id,name,length,album_id) VALUES (54,'The End of the Line',7+(52/60),6); 82 | INSERT INTO songs(id,name,length,album_id) VALUES (55,'Broken Beat & Scarred',6+(25/60),6); 83 | INSERT INTO songs(id,name,length,album_id) VALUES (56,'The Day That Never Comes',7+(56/60),6); 84 | INSERT INTO songs(id,name,length,album_id) VALUES (57,'All Nightmare Long',7+(58/60),6); 85 | INSERT INTO songs(id,name,length,album_id) VALUES (58,'Cyanide',6+(40/60),6); 86 | INSERT INTO songs(id,name,length,album_id) VALUES (59,'The Unforgiven III',7+(47/60),6); 87 | INSERT INTO songs(id,name,length,album_id) VALUES (60,'The Judas Kiss',8+(01/60),6); 88 | INSERT INTO songs(id,name,length,album_id) VALUES (61,'Suicide & Redemption',9+(58/60),6); 89 | INSERT INTO songs(id,name,length,album_id) VALUES (62,'My Apocalypse',5+(01/60),6); 90 | INSERT INTO songs(id,name,length,album_id) VALUES (63,'Shamayim',1+(53/60),7); 91 | INSERT INTO songs(id,name,length,album_id) VALUES (64,'Firmament',7+(29/60),7); 92 | INSERT INTO songs(id,name,length,album_id) VALUES (65,'The First Commandment of the Luminaries',6+(47/60),7); 93 | INSERT INTO songs(id,name,length,album_id) VALUES (66,'Ptolemy Was Wrong',6+(28/60),7); 94 | INSERT INTO songs(id,name,length,album_id) VALUES (67,'Metaphysics of the Hangman',5+(41/60),7); 95 | INSERT INTO songs(id,name,length,album_id) VALUES (68,'Catharsis of a Heretic',2+(08/60),7); 96 | INSERT INTO songs(id,name,length,album_id) VALUES (69,'Swallowed by the Earth',4+(59/60),7); 97 | INSERT INTO songs(id,name,length,album_id) VALUES (70,'Epiphany',3+(37/60),7); 98 | INSERT INTO songs(id,name,length,album_id) VALUES (71,'The Origin of Species',7+(23/60),7); 99 | INSERT INTO songs(id,name,length,album_id) VALUES (72,'The Origin of God',4+(33/60),7); 100 | INSERT INTO songs(id,name,length,album_id) VALUES (73,'Epipelagic',1+(12/60),8); 101 | INSERT INTO songs(id,name,length,album_id) VALUES (74,'Mesopelagic: Into the Uncanny',5+(56/60),8); 102 | INSERT INTO songs(id,name,length,album_id) VALUES (75,'Bathyalpelagic I: Impasses',4+(24/60),8); 103 | INSERT INTO songs(id,name,length,album_id) VALUES (76,'Bathyalpelagic II: The Wish in Dreams',3+(18/60),8); 104 | INSERT INTO songs(id,name,length,album_id) VALUES (77,'Bathyalpelagic III: Disequilibrated',4+(27/60),8); 105 | INSERT INTO songs(id,name,length,album_id) VALUES (78,'Abyssopelagic I: Boundless Vasts',3+(27/60),8); 106 | INSERT INTO songs(id,name,length,album_id) VALUES (79,'Abyssopelagic II: Signals of Anxiety',5+(05/60),8); 107 | INSERT INTO songs(id,name,length,album_id) VALUES (80,'Hadopelagic I: Omen of the Deep',1+(07/60),8); 108 | INSERT INTO songs(id,name,length,album_id) VALUES (81,'Hadopelagic II: Let Them Believe',9+(17/60),8); 109 | INSERT INTO songs(id,name,length,album_id) VALUES (82,'Demersal: Cognitive Dissonance',9+(05/60),8); 110 | INSERT INTO songs(id,name,length,album_id) VALUES (83,'Benthic: The Origin of Our Wishes',5+(55/60),8); 111 | INSERT INTO songs(id,name,length,album_id) VALUES (84,'Anthropocentric',9+(24/60),9); 112 | INSERT INTO songs(id,name,length,album_id) VALUES (85,'The Grand Inquisitor I: Karamazov Baseness',5+(02/60),9); 113 | INSERT INTO songs(id,name,length,album_id) VALUES (86,'She Was the Universe',5+(39/60),9); 114 | INSERT INTO songs(id,name,length,album_id) VALUES (87,'For He That Wavereth...',2+(07/60),9); 115 | INSERT INTO songs(id,name,length,album_id) VALUES (88,'The Grand Inquisitor II: Roots & Locusts',6+(33/60),9); 116 | INSERT INTO songs(id,name,length,album_id) VALUES (89,'The Grand Inquisitor III: A Tiny Grain of Faith',1+(56/60),9); 117 | INSERT INTO songs(id,name,length,album_id) VALUES (90,'Sewers of the Soul',3+(44/60),9); 118 | INSERT INTO songs(id,name,length,album_id) VALUES (91,'Wille zum Untergang',6+(03/60),9); 119 | INSERT INTO songs(id,name,length,album_id) VALUES (92,'Heaven TV',5+(04/60),9); 120 | INSERT INTO songs(id,name,length,album_id) VALUES (93,'The Almightiness Contradiction',4+(34/60),9); 121 | INSERT INTO songs(id,name,length,album_id) VALUES (94,'The Reckoning',4+(11/60),10); 122 | INSERT INTO songs(id,name,length,album_id) VALUES (95,'Endless War',4+(09/60),10); 123 | INSERT INTO songs(id,name,length,album_id) VALUES (96,'Raise Your Banner',5+(34/60),10); 124 | INSERT INTO songs(id,name,length,album_id) VALUES (97,'Supernova',5+(34/60),10); 125 | INSERT INTO songs(id,name,length,album_id) VALUES (98,'Holy Ground',4+(10/60),10); 126 | INSERT INTO songs(id,name,length,album_id) VALUES (99,'In Vain',4+(25/60),10); 127 | INSERT INTO songs(id,name,length,album_id) VALUES (100,'Firelight',4+(46/60),10); 128 | INSERT INTO songs(id,name,length,album_id) VALUES (101,'Mad World',4+(57/60),10); 129 | INSERT INTO songs(id,name,length,album_id) VALUES (102,'Mercy Mirror',3+(49/60),10); 130 | INSERT INTO songs(id,name,length,album_id) VALUES (103,'Trophy Hunter',5+(51/60),10); 131 | INSERT INTO songs(id,name,length,album_id) VALUES (104,'Why Not Me',0+(34/60),11); 132 | INSERT INTO songs(id,name,length,album_id) VALUES (105,'Shot in the Dark',5+(02/60),11); 133 | INSERT INTO songs(id,name,length,album_id) VALUES (106,'In the Middle of the Night',5+(11/60),11); 134 | INSERT INTO songs(id,name,length,album_id) VALUES (107,'Faster',4+(23/60),11); 135 | INSERT INTO songs(id,name,length,album_id) VALUES (108,'Fire and Ice',3+(57/60),11); 136 | INSERT INTO songs(id,name,length,album_id) VALUES (109,'Iron',5+(40/60),11); 137 | INSERT INTO songs(id,name,length,album_id) VALUES (110,'Where Is the Edge',3+(59/60),11); 138 | INSERT INTO songs(id,name,length,album_id) VALUES (111,'Sinéad',4+(23/60),11); 139 | INSERT INTO songs(id,name,length,album_id) VALUES (112,'Lost',5+(14/60),11); 140 | INSERT INTO songs(id,name,length,album_id) VALUES (113,'Murder',4+(16/60),11); 141 | INSERT INTO songs(id,name,length,album_id) VALUES (114,'A Demon''s Fate',5+(30/60),11); 142 | INSERT INTO songs(id,name,length,album_id) VALUES (115,'Stairway to the Skies',5+(32/60),11); 143 | INSERT INTO songs(id,name,length,album_id) VALUES (116,'Restless',6+(08/60),12); 144 | INSERT INTO songs(id,name,length,album_id) VALUES (117,'Enter',7+(15/60),12); 145 | INSERT INTO songs(id,name,length,album_id) VALUES (118,'Pearls of Light',5+(15/60),12); 146 | INSERT INTO songs(id,name,length,album_id) VALUES (119,'Deep Within',4+(30/60),12); 147 | INSERT INTO songs(id,name,length,album_id) VALUES (120,'Gatekeeper',6+(43/60),12); 148 | INSERT INTO songs(id,name,length,album_id) VALUES (121,'Grace',5+(10/60),12); 149 | INSERT INTO songs(id,name,length,album_id) VALUES (122,'Blooded',3+(38/60),12); 150 | INSERT INTO songs(id,name,length,album_id) VALUES (123,'Candles',7+(07/60),12); 151 | INSERT INTO songs(id,name,length,album_id) VALUES (124,'Scavenger of Human Sorrow',6+(56/60),13); 152 | INSERT INTO songs(id,name,length,album_id) VALUES (125,'Bite the Pain',4+(29/60),13); 153 | INSERT INTO songs(id,name,length,album_id) VALUES (126,'Spirit Crusher',6+(47/60),13); 154 | INSERT INTO songs(id,name,length,album_id) VALUES (127,'Story to Tell',6+(34/60),13); 155 | INSERT INTO songs(id,name,length,album_id) VALUES (128,'Flesh and the Power It Holds',8+(26/60),13); 156 | INSERT INTO songs(id,name,length,album_id) VALUES (129,'Voice of the Soul',3+(43/60),13); 157 | INSERT INTO songs(id,name,length,album_id) VALUES (130,'To Forgive Is to Suffer',5+(55/60),13); 158 | INSERT INTO songs(id,name,length,album_id) VALUES (131,'A Moment of Clarity',7+(25/60),13); 159 | INSERT INTO songs(id,name,length,album_id) VALUES (132,'Painkiller',6+(02/60),13); 160 | INSERT INTO songs(id,name,length,album_id) VALUES (133,'Overactive Imagination',3+(30/60),14); 161 | INSERT INTO songs(id,name,length,album_id) VALUES (134,'In Human Form',3+(57/60),14); 162 | INSERT INTO songs(id,name,length,album_id) VALUES (135,'Jealousy',3+(41/60),14); 163 | INSERT INTO songs(id,name,length,album_id) VALUES (136,'Trapped in a Corner',4+(14/60),14); 164 | INSERT INTO songs(id,name,length,album_id) VALUES (137,'Nothing Is Everything',3+(19/60),14); 165 | INSERT INTO songs(id,name,length,album_id) VALUES (138,'Mentally Blind',4+(49/60),14); 166 | INSERT INTO songs(id,name,length,album_id) VALUES (139,'Individual Thought Patterns',4+(01/60),14); 167 | INSERT INTO songs(id,name,length,album_id) VALUES (140,'Destiny',4+(06/60),14); 168 | INSERT INTO songs(id,name,length,album_id) VALUES (141,'Out of Touch',4+(22/60),14); 169 | INSERT INTO songs(id,name,length,album_id) VALUES (142,'The Philosopher',4+(13/60),14); 170 | INSERT INTO songs(id,name,length,album_id) VALUES (143,'Flattening of Emotions',4+(28/60),15); 171 | INSERT INTO songs(id,name,length,album_id) VALUES (144,'Suicide Machine',4+(23/60),15); 172 | INSERT INTO songs(id,name,length,album_id) VALUES (145,'Together as One',4+(10/60),15); 173 | INSERT INTO songs(id,name,length,album_id) VALUES (146,'Secret Face',4+(39/60),15); 174 | INSERT INTO songs(id,name,length,album_id) VALUES (147,'Lack of Comprehension',3+(43/60),15); 175 | INSERT INTO songs(id,name,length,album_id) VALUES (148,'See Through Dreams',4+(39/60),15); 176 | INSERT INTO songs(id,name,length,album_id) VALUES (149,'Cosmic Sea',4+(27/60),15); 177 | INSERT INTO songs(id,name,length,album_id) VALUES (150,'Vacant Planets',3+(52/60),15); 178 | INSERT INTO songs(id,name,length,album_id) VALUES (151,'Stora Rövardansen',1+(33/60),16); 179 | INSERT INTO songs(id,name,length,album_id) VALUES (152,'King',3+(44/60),16); 180 | INSERT INTO songs(id,name,length,album_id) VALUES (153,'The Mission',4+(18/60),16); 181 | INSERT INTO songs(id,name,length,album_id) VALUES (154,'Lifetime',4+(49/60),16); 182 | INSERT INTO songs(id,name,length,album_id) VALUES (155,'Rain',4+(03/60),16); 183 | INSERT INTO songs(id,name,length,album_id) VALUES (156,'She''s Alive',4+(12/60),16); 184 | INSERT INTO songs(id,name,length,album_id) VALUES (157,'I Stand Alone',4+(44/60),16); 185 | INSERT INTO songs(id,name,length,album_id) VALUES (158,'Starlight',4+(40/60),16); 186 | INSERT INTO songs(id,name,length,album_id) VALUES (159,'Battery',5+(13/60),16); 187 | INSERT INTO songs(id,name,length,album_id) VALUES (160,'If I Die in Battle',4+(46/60),17); 188 | INSERT INTO songs(id,name,length,album_id) VALUES (161,'The Seller of Souls',3+(24/60),17); 189 | INSERT INTO songs(id,name,length,album_id) VALUES (162,'Primo Victoria',3+(44/60),17); 190 | INSERT INTO songs(id,name,length,album_id) VALUES (163,'Dangers in My Head',4+(05/60),17); 191 | INSERT INTO songs(id,name,length,album_id) VALUES (164,'Black Wings of Hate',4+(41/60),17); 192 | INSERT INTO songs(id,name,length,album_id) VALUES (165,'Bed of Nails',3+(37/60),17); 193 | INSERT INTO songs(id,name,length,album_id) VALUES (166,'Spelled in Waters',4+(26/60),17); 194 | INSERT INTO songs(id,name,length,album_id) VALUES (167,'Neuer Wind',3+(21/60),17); 195 | INSERT INTO songs(id,name,length,album_id) VALUES (168,'The Higher Flight',5+(00/60),17); 196 | INSERT INTO songs(id,name,length,album_id) VALUES (169,'Master of the Wind',6+(09/60),17); 197 | INSERT INTO songs(id,name,length,album_id) VALUES (170,'Lost Forever',4+(44/60),18); 198 | INSERT INTO songs(id,name,length,album_id) VALUES (171,'To Sing a Metal Song',3+(24/60),18); 199 | INSERT INTO songs(id,name,length,album_id) VALUES (172,'One to Ten',4+(06/60),18); 200 | INSERT INTO songs(id,name,length,album_id) VALUES (173,'I Am Human',3+(56/60),18); 201 | INSERT INTO songs(id,name,length,album_id) VALUES (174,'My Voice',5+(30/60),18); 202 | INSERT INTO songs(id,name,length,album_id) VALUES (175,'Rebellion',4+(05/60),18); 203 | INSERT INTO songs(id,name,length,album_id) VALUES (176,'Last Night of the Kings',3+(52/60),18); 204 | INSERT INTO songs(id,name,length,album_id) VALUES (177,'Tribe of Force',3+(17/60),18); 205 | INSERT INTO songs(id,name,length,album_id) VALUES (178,'Water Fire Heaven Earth',3+(32/60),18); 206 | INSERT INTO songs(id,name,length,album_id) VALUES (179,'Master of Puppets',8+(23/60),18); 207 | INSERT INTO songs(id,name,length,album_id) VALUES (180,'Magic Taborea',3+(22/60),18); 208 | INSERT INTO songs(id,name,length,album_id) VALUES (181,'Hearted',4+(00/60),18); 209 | INSERT INTO songs(id,name,length,album_id) VALUES (182,'Frodo''s Dream',3+(06/60),18); -------------------------------------------------------------------------------- /schema.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE record_company; 2 | USE record_company; 3 | 4 | CREATE TABLE bands ( 5 | id INT NOT NULL AUTO_INCREMENT, 6 | name VARCHAR(255) NOT NULL, 7 | PRIMARY KEY (id) 8 | ); 9 | 10 | CREATE TABLE albums ( 11 | id INT NOT NULL AUTO_INCREMENT, 12 | name VARCHAR(255) NOT NULL, 13 | release_year INT, 14 | band_id INT NOT NULL, 15 | PRIMARY KEY (id), 16 | FOREIGN KEY (band_id) REFERENCES bands(id) 17 | ); -------------------------------------------------------------------------------- /solutions/1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE songs ( 2 | id INT NOT NULL AUTO_INCREMENT, 3 | name VARCHAR(255) NOT NULL, 4 | length FLOAT NOT NULL, 5 | album_id INT NOT NULL, 6 | PRIMARY KEY (id), 7 | FOREIGN KEY (album_id) REFERENCES albums(id) 8 | ); -------------------------------------------------------------------------------- /solutions/10.sql: -------------------------------------------------------------------------------- 1 | SELECT AVG(length) as 'Average Song Duration' 2 | FROM songs; -------------------------------------------------------------------------------- /solutions/11.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | albums.name AS 'Album', 3 | albums.release_year AS 'Release Year', 4 | MAX(songs.length) AS 'Duration' 5 | FROM albums 6 | JOIN songs ON albums.id = songs.album_id 7 | GROUP BY songs.album_id; -------------------------------------------------------------------------------- /solutions/12.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | bands.name AS 'Band', 3 | COUNT(songs.id) AS 'Number of Songs' 4 | FROM bands 5 | JOIN albums ON bands.id = albums.band_id 6 | JOIN songs ON albums.id = songs.album_id 7 | GROUP BY albums.band_id; -------------------------------------------------------------------------------- /solutions/2.sql: -------------------------------------------------------------------------------- 1 | SELECT bands.name AS 'Band Name' 2 | FROM bands; -------------------------------------------------------------------------------- /solutions/3.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM albums 2 | WHERE release_year IS NOT NULL 3 | ORDER BY release_year 4 | LIMIT 1; -------------------------------------------------------------------------------- /solutions/4.sql: -------------------------------------------------------------------------------- 1 | 2 | /* This assummes all bands have a unique name */ 3 | SELECT DISTINCT bands.name AS 'Band Name' 4 | FROM bands 5 | JOIN albums ON bands.id = albums.band_id; 6 | 7 | /* If bands do not have a unique name then use this query */ 8 | /* 9 | SELECT bands.name AS 'Band Name' 10 | FROM bands 11 | JOIN albums ON bands.id = albums.band_id 12 | GROUP BY albums.band_id 13 | HAVING COUNT(albums.id) > 0; 14 | */ -------------------------------------------------------------------------------- /solutions/5.sql: -------------------------------------------------------------------------------- 1 | SELECT bands.name AS 'Band Name' 2 | FROM bands 3 | LEFT JOIN albums ON bands.id = albums.band_id 4 | GROUP BY albums.band_id 5 | HAVING COUNT(albums.id) = 0; -------------------------------------------------------------------------------- /solutions/6.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | albums.name as Name, 3 | albums.release_year as 'Release Year', 4 | SUM(songs.length) as 'Duration' 5 | FROM albums 6 | JOIN songs on albums.id = songs.album_id 7 | GROUP BY songs.album_id 8 | ORDER BY Duration DESC 9 | LIMIT 1; -------------------------------------------------------------------------------- /solutions/7.sql: -------------------------------------------------------------------------------- 1 | /* This is the query used to get the id */ 2 | /* 3 | SELECT * FROM albums where release_year IS NULL; 4 | */ 5 | 6 | UPDATE albums 7 | SET release_year = 1986 8 | WHERE id = 4; -------------------------------------------------------------------------------- /solutions/8.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO bands (name) 2 | VALUES ('Favorite Band Name'); 3 | 4 | /* This is the query used to get the band id of the band just added */ 5 | /* 6 | SELECT id FROM bands 7 | ORDER BY id DESC LIMIT 1; 8 | */ 9 | 10 | INSERT INTO albums (name, release_year, band_id) 11 | VALUES ('Favorite Album Name', 2000, 8); -------------------------------------------------------------------------------- /solutions/9.sql: -------------------------------------------------------------------------------- 1 | /* This is the query used to get the album id of the album added in #8 */ 2 | /* 3 | SELECT id FROM albums 4 | ORDER BY id DESC LIMIT 1; 5 | */ 6 | 7 | DELETE FROM albums 8 | WHERE id = 19; 9 | 10 | /* This is the query used to get the band id of the band added in #8 */ 11 | /* 12 | SELECT id FROM bands 13 | ORDER BY id DESC LIMIT 1; 14 | */ 15 | 16 | DELETE FROM bands 17 | WHERE id = 8; --------------------------------------------------------------------------------