Last active
January 23, 2023 01:56
-
-
Save tjpeter/e16a9c8b572a3e7c5371e7ed1ae4a246 to your computer and use it in GitHub Desktop.
StanfordOnline - Databases: Relational Databases and SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 1. Find the names of all reviewers who rated Gone with the Wind. | |
SELECT DISTINCT name | |
FROM Movie | |
JOIN rating USING(mID) | |
JOIN reviewer USING(rID) | |
WHERE title = "Gone with the Wind"; | |
--- 2. For any rating where the reviewer is the same as the director of the movie, return the reviewer name, movie title, and number of stars. | |
SELECT name, title, stars FROM | |
Movie JOIN rating USING(mID) | |
JOIN reviewer using(rID) | |
WHERE name = director; | |
--- 3. Return all reviewer names and movie names together in a single list, alphabetized. (Sorting by the first name of the reviewer and first word in the title is fine; no need for special processing on last names or removing "The".) | |
SELECT DISTINCT name | |
FROM reviewer | |
UNION | |
SELECT distinct title | |
FROM movie | |
ORDER BY 1; | |
--- 4. Find the titles of all movies not reviewed by Chris Jackson. | |
SELECT title FROM movie | |
WHERE mID NOT IN ( | |
SELECT mID FROM | |
rating JOIN reviewer USING(rID) | |
WHERE name = "Chris Jackson" | |
); | |
--- 5. For all pairs of reviewers such that both reviewers gave a rating to the same movie, return the names of both reviewers. Eliminate duplicates, don't pair reviewers with themselves, and include each pair only once. For each pair, return the names in the pair in alphabetical order. | |
SELECT DISTINCT Rw1.name, Rw2.name | |
FROM Movie | |
INNER JOIN Rating R1 USING(mID) | |
INNER JOIN Rating R2 USING(mID) | |
INNER JOIN Reviewer Rw1 ON R1.rID = Rw1.rID | |
INNER JOIN Reviewer Rw2 ON R2.rID = Rw2.rID | |
WHERE R1.mId = R2.mId AND Rw1.name < Rw2.name | |
ORDER by Rw1.name; | |
--- alternative | |
SELECT DISTINCT Rw1.name, Rw2.name | |
FROM Rating R1, Rating R2, Reviewer Rw1, Reviewer Rw2 | |
WHERE R1.mID = R2.mID | |
AND R1.rID = Rw1.rID | |
AND R2.rID = Rw2.rID | |
AND Rw1.name < Rw2.name | |
ORDER BY Rw1.name, Rw2.name; | |
--- 6. For each rating that is the lowest (fewest stars) currently in the database, return the reviewer name, movie title, and number of stars. | |
SELECT name, title, stars | |
FROM movie | |
inner JOIN rating USING(mID) | |
inner JOIN reviewer USING(rID) | |
WHERE stars = (SELECT MIN(stars) from RATING); | |
--- 7. List movie titles and average ratings, from highest-rated to lowest-rated. If two or more movies have the same average rating, list them in alphabetical order. | |
SELECT title, avg(stars) | |
FROM movie INNER JOIN rating USING(mID) | |
GROUP BY title | |
ORDER BY avg(stars) DESC; | |
--- 8. Find the names of all reviewers who have contributed three or more ratings. | |
SELECT name | |
FROM Reviewer | |
INNER JOIN Rating USING(rID) | |
GROUP BY name | |
HAVING COUNT()>=3; | |
--- 9. Some directors directed more than one movie. For all such directors, return the titles of all movies directed by them, along with the director name. Sort by director name, then movie title. | |
SELECT title, director | |
FROM movie WHERE | |
director IN ( | |
SELECT director | |
FROM movie | |
GROUP BY director | |
HAVING count()>1 | |
) | |
ORDER BY director, title; | |
--- alternative | |
SELECT title, director | |
FROM Movie M1 | |
WHERE (SELECT COUNT(*) FROM Movie M2 WHERE M1.director = M2.director) > 1 | |
ORDER BY director, title; | |
--- 10. Find the movie(s) with the highest average rating. Return the movie title(s) and average rating. | |
SELECT title, avg(stars) | |
FROM movie | |
INNER JOIN rating USING(mID) | |
GROUP BY mID | |
HAVING avg(stars) = ( | |
SELECT max(avg_stars) | |
FROM ( | |
SELECT avg(stars) AS avg_stars | |
FROM rating | |
GROUP BY mID | |
) | |
); | |
--- 11. Find the movie(s) with the lowest average rating. Return the movie title(s) and average rating. | |
SELECT title, avg(stars) | |
FROM movie | |
INNER JOIN rating USING(mID) | |
GROUP BY mID | |
HAVING avg(stars) = ( | |
SELECT min(avg_stars) | |
FROM ( | |
SELECT avg(stars) AS avg_stars | |
FROM rating | |
GROUP BY mID | |
) | |
); | |
--- 12. For each director, return the director's name together with the title(s) of the movie(s) they directed that received the highest rating among all of their movies, and the value of that rating. Ignore movies whose director is NULL. | |
SELECT director, title, MAX(stars) | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
WHERE director IS NOT NULL | |
GROUP BY director; | |
--- alternative | |
SELECT DISTINCT director, title, stars | |
FROM movie M1 | |
INNER JOIN rating USING(mID) | |
WHERE stars = ( | |
SELECT MAX(stars) | |
FROM movie M2 | |
INNER JOIN rating USING(mID) | |
GROUP BY director | |
HAVING M2.director = M1.director | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 1. Add the reviewer Roger Ebert to your database, with an rID of 209. | |
INSERT INTO reviewer | |
VALUES(209, "Roger Ebert"); | |
--- 2. For all movies that have an average rating of 4 stars or higher, add 25 to the release year. (Update the existing tuples; don't insert new tuples.) | |
UPDATE movie | |
SET year = year + 25 | |
WHERE mID IN | |
(SELECT mID | |
FROM rating | |
GROUP BY mID | |
HAVING AVG(stars) >= 4 | |
) | |
; | |
--- 3. Remove all ratings where the movie's year is before 1970 or after 2000, and the rating is fewer than 4 stars. | |
DELETE FROM rating | |
WHERE mID IN ( | |
SELECT mID | |
FROM movie | |
WHERE year<1970 OR year>2000) | |
AND stars<4; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment