Skip to content

Changes to a few solutions: 5, 7 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions solutions/1.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CREATE TABLE songs (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
length FLOAT NOT NULL,
album_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (album_id) REFERENCES albums(id)
CREATE TABLE songs
(
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
length float NOT NULL,
album_id int NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(album_id) REFERENCES albums(id)
);
2 changes: 1 addition & 1 deletion solutions/10.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT AVG(length) as 'Average Song Duration'
SELECT avg(length) AS 'Average Song Duration'
FROM songs;
12 changes: 6 additions & 6 deletions solutions/11.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SELECT
albums.name AS 'Album',
albums.release_year AS 'Release Year',
MAX(songs.length) AS 'Duration'
SELECT albums.name AS 'Name', albums.release_year AS 'Release Year', longest_songs.duration AS 'Duration'
FROM albums
JOIN songs ON albums.id = songs.album_id
GROUP BY songs.album_id;
INNER JOIN (
SELECT album_id, max(length) AS duration FROM songs
GROUP BY album_id
) AS longest_songs
ON albums.id = longest_songs.album_id;
19 changes: 13 additions & 6 deletions solutions/12.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
SELECT
bands.name AS 'Band',
COUNT(songs.id) AS 'Number of Songs'
SELECT bands.name AS 'Band', songs_per_band 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;
INNER JOIN (
SELECT band_id, sum(albums_songs.songs_per_album) AS songs_per_band
FROM albums
INNER JOIN (
SELECT album_id, count(album_id) AS songs_per_album
FROM songs
GROUP BY album_id
) AS albums_songs
ON albums.id = albums_songs.album_id
GROUP BY band_id
) AS bands_songs
ON bands.id = bands_songs.band_id;
2 changes: 1 addition & 1 deletion solutions/2.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT bands.name AS 'Band Name'
SELECT name AS 'Band Name'
FROM bands;
12 changes: 11 additions & 1 deletion solutions/3.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
SELECT * FROM albums
# My solution
SELECT albums.*
FROM albums
INNER JOIN
(SELECT min(release_year) AS release_year FROM albums) AS oldest_album
ON albums.release_year = oldest_album.release_year;


# Given solution
SELECT *
FROM albums
WHERE release_year IS NOT NULL
ORDER BY release_year
LIMIT 1;
31 changes: 19 additions & 12 deletions solutions/4.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# Solution 1
/* Assumes that bands with different ids have different names. */
SELECT DISTINCT(b.name) AS 'Band Name'
FROM albums AS a
INNER JOIN (bands AS b) ON a.band_id = b.id;

/* This assummes all bands have a unique name */
SELECT DISTINCT bands.name AS 'Band Name'

/* The following two solutions don't assume that. */

# Solution 2
SELECT name AS 'Band Name'
FROM bands
INNER JOIN (SELECT DISTINCT(band_id) AS album_band_id FROM albums) AS bands_with_albums
ON bands_with_albums.album_band_id = bands.id;


# Solution 3
SELECT name AS 'Band Name'
FROM bands
JOIN albums ON bands.id = albums.band_id;

/* If bands do not have a unique name then use this query */
/*
SELECT bands.name AS 'Band Name'
FROM bands
JOIN albums ON bands.id = albums.band_id
GROUP BY albums.band_id
HAVING COUNT(albums.id) > 0;
*/
INNER JOIN (SELECT band_id FROM albums GROUP BY band_id) AS bands_with_albums
ON bands_with_albums.band_id = bands.id;
30 changes: 27 additions & 3 deletions solutions/5.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
SELECT bands.name AS 'Band Name'
# Solution 1
/* Assumes band names are unique */
SELECT DISTINCT(bands.name) AS 'Band Name'
FROM bands
LEFT JOIN albums ON bands.id = albums.band_id
GROUP BY albums.band_id
HAVING COUNT(albums.id) = 0;
WHERE albums.band_id IS NULL;


/* No such assumption */
# Solution 2
SELECT bands.name AS 'Band Name'
FROM bands
INNER JOIN (
SELECT bands.id as id
FROM bands
LEFT JOIN albums ON bands.id = albums.band_id
WHERE albums.band_id IS NULL
) AS bands_with_no_albums
ON bands.id = bands_with_no_albums.id;


/*
Note:
There's an issue with the given solution: https://github.com/WebDevSimplified/Learn-SQL/issues/8
The fellow who closed it must have changed the mode of SQL to something else.
But it's been suggested to use full_group_by mode for SQL to avoid "inconsistencies".
(Basically, find a different way to solve your problem
without using columns in SELECT that were not included in your GROUP BY.)
*/
18 changes: 9 additions & 9 deletions solutions/6.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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;
SELECT albums.name AS 'Name', albums.release_year AS 'Release Year', longest_album.Duration
FROM (
SELECT album_id, sum(length) AS 'Duration'
FROM songs
GROUP BY album_id
ORDER BY Duration DESC
LIMIT 1
) AS longest_album
INNER JOIN albums ON longest_album.album_id = albums.id;
12 changes: 4 additions & 8 deletions solutions/7.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/* This is the query used to get the id */
/*
SELECT * FROM albums where release_year IS NULL;
*/

UPDATE albums
SET release_year = 1986
WHERE id = 4;
UPDATE albums,
(SELECT id FROM albums WHERE release_year IS NULL) AS not_yet_released
SET albums.release_year = 1986
WHERE albums.id = not_yet_released.id;
16 changes: 7 additions & 9 deletions solutions/8.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
INSERT INTO bands (name)
VALUES ('Favorite Band Name');
INSERT INTO bands(name)
VALUES ('SSSPSN');

/* This is the query used to get the band id of the band just added */
/*
SELECT id FROM bands
ORDER BY id DESC LIMIT 1;
*/
SELECT * FROM bands;

INSERT INTO albums (name, release_year, band_id)
VALUES ('Favorite Album Name', 2000, 8);
INSERT INTO albums(name, release_year, band_id)
VALUES ('Bhajans', 2009, 8);

SELECT * FROM albums;
20 changes: 7 additions & 13 deletions solutions/9.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/* This is the query used to get the album id of the album added in #8 */
/*
SELECT id FROM albums
ORDER BY id DESC LIMIT 1;
*/

DELETE FROM albums
WHERE id = 19;
WHERE albums.band_id
IN (SELECT id FROM bands WHERE name = 'SSSPSN');

SELECT * FROM albums;

/* This is the query used to get the band id of the band added in #8 */
/*
SELECT id FROM bands
ORDER BY id DESC LIMIT 1;
*/

DELETE FROM bands
WHERE id = 8;
WHERE id IN (SELECT * FROM (SELECT id FROM bands WHERE name = 'SSSPSN') AS to_delete);

SELECT * FROM bands;