Last active
August 29, 2015 14:14
-
-
Save chinkouu/b5bb9bda2f8424511bb9 to your computer and use it in GitHub Desktop.
斯坦福大学公开讲座 DB5 SQL(2014/6/6)练习题
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
/*这是斯坦福大学网络公开课DB5 SQL的练习题解答*/ | |
/*第一部分:SQL Movie-Rating Query Exercises*/ | |
/*数据环境准备*/ | |
/* Delete the tables if they already exist */ | |
drop table if exists Movie; | |
drop table if exists Reviewer; | |
drop table if exists Rating; | |
/* Create the schema for our tables */ | |
create table Movie(mID int, title text, year int, director text); | |
create table Reviewer(rID int, name text); | |
create table Rating(rID int, mID int, stars int, ratingDate date); | |
/* Populate the tables with our data */ | |
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming'); | |
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas'); | |
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise'); | |
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg'); | |
insert into Movie values(105, 'Titanic', 1997, 'James Cameron'); | |
insert into Movie values(106, 'Snow White', 1937, null); | |
insert into Movie values(107, 'Avatar', 2009, 'James Cameron'); | |
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg'); | |
insert into Reviewer values(201, 'Sarah Martinez'); | |
insert into Reviewer values(202, 'Daniel Lewis'); | |
insert into Reviewer values(203, 'Brittany Harris'); | |
insert into Reviewer values(204, 'Mike Anderson'); | |
insert into Reviewer values(205, 'Chris Jackson'); | |
insert into Reviewer values(206, 'Elizabeth Thomas'); | |
insert into Reviewer values(207, 'James Cameron'); | |
insert into Reviewer values(208, 'Ashley White'); | |
insert into Rating values(201, 101, 2, '2011-01-22'); | |
insert into Rating values(201, 101, 4, '2011-01-27'); | |
insert into Rating values(202, 106, 4, null); | |
insert into Rating values(203, 103, 2, '2011-01-20'); | |
insert into Rating values(203, 108, 4, '2011-01-12'); | |
insert into Rating values(203, 108, 2, '2011-01-30'); | |
insert into Rating values(204, 101, 3, '2011-01-09'); | |
insert into Rating values(205, 103, 3, '2011-01-27'); | |
insert into Rating values(205, 104, 2, '2011-01-22'); | |
insert into Rating values(205, 108, 4, null); | |
insert into Rating values(206, 107, 3, '2011-01-15'); | |
insert into Rating values(206, 106, 5, '2011-01-19'); | |
insert into Rating values(207, 107, 5, '2011-01-20'); | |
insert into Rating values(208, 104, 3, '2011-01-02'); | |
/*练习题及其解答*/ | |
/*---------- 1. SQL Movie-Rating Query Exercises ----------*/ | |
-- Q1 Find the titles of all movies directed by Steven Spielberg. | |
-- Q1 查找导演Steven Spielberg执导的全部影片的片名 | |
select title from Movie where director="Steven Spielberg"; | |
-- Q2 Find all years that have a movie that received a rating of 4 or 5, and sort them in increasing order. | |
-- Q2 查找评价为4和5的电影的发布年,按照发布年顺序排列 | |
select distinct year from Movie | |
where mId in (select mID from rating where stars>3) order by year; | |
-- Q3 Find the titles of all movies that have no ratings. | |
-- Q3 查找所有没有被评价的电影 | |
select distinct title from Movie | |
where mID in (select mID from Movie where mID not in (select mID from Rating)); | |
select title from movie where mID not in (select mID from rating); | |
-- Q4 Some reviewers didn't provide a date with their rating. Find the names of all reviewers who have ratings with a NULL value for the date. | |
-- Q4 有些评论家没有提供评论日期,请找出这些评论家的姓名 | |
select distinct name from Reviewer | |
where rID in (select rID from Rating where ratingDate is null); | |
-- Q5 Write a query to return the ratings data in a more readable format: reviewer name, movie title, stars, and ratingDate. Also, sort the data, first by reviewer name, then by movie title, and lastly by number of stars. | |
-- Q5 请列出详细的影评信息:评论家姓名、电影名、评价、评价日,按照评价者姓名、电影名、评价排序 | |
select name,title,stars,ratingDate | |
from Reviewer,Rating,Movie | |
where Reviewer.rID=Rating.rID and Movie.mID=Rating.mID | |
order by name, title, stars; | |
-- Q6 For all cases where the same reviewer rated the same movie twice and gave it a higher rating the second time, return the reviewer's name and the title of the movie. | |
-- Q6 有的评论家对同一部电影评价了2次,而且第二次的评价高于第一次,请列出这些评论家及电影名 | |
select name, title from Reviewer, Movie, Rating, Rating r2 | |
where Rating.mID=Movie.mID and Reviewer.rID=Rating.rID | |
and Rating.rID = r2.rID and r2.mID = Movie.mID | |
and Rating.stars < r2.stars and Rating.ratingDate < r2.ratingDate; | |
-- Q7 For each movie that has at least one rating, find the highest number of stars that movie received. Return the movie title and number of stars. Sort by movie title. | |
-- Q7 每部电影至少有一次评价,找出所有电影获得的最高评价,显示电影名和评价,按照电影名排序 | |
select title,max(stars) from Movie, Rating | |
where Movie.mID=Rating.mID | |
group by Rating.mID order by title; | |
-- Q8 For each movie, return the title and the 'rating spread', that is, the difference between highest and lowest ratings given to that movie. Sort by rating spread from highest to lowest, then by movie title. | |
-- Q8 查找电影评价的偏差值,偏差值是指某电影获得的较高评价和较低评价的差值。按照偏差值降序、电影名升序排序 | |
select title,max(stars)-min(stars) as spread from Movie, Rating | |
where Movie.mID=Rating.mID group by title order by spread desc; | |
-- Q9 Find the difference between the average rating of movies released before 1980 and the average rating of movies released after 1980. (Make sure to calculate the average rating for each movie, then the average of those averages for movies before 1980 and movies after. Don't just calculate the overall average rating before and after 1980.) | |
-- Q9 查找1980年以前的电影的平均评价与1980年以后的电影的平均评价之差 | |
select down.av1980down - up.av1980up | |
from (select avg(avgst) as av1980up from | |
(select title, avg(stars) avgst | |
from Movie,Rating | |
where year>1980 and Movie.mID=Rating.mID group by title | |
) | |
) as up, | |
(select avg(avgst) as av1980down from | |
(select title, avg(stars) avgst | |
from Movie,Rating | |
where year<1980 and Movie.mID=Rating.mID group by title | |
) | |
) as down; | |
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
/*这是斯坦福大学网络公开课DB5 SQL的练习题解答*/ | |
/*第二部分:SQL Movie-Rating Query Exercises Extras*/ | |
/*数据环境准备*/ | |
/* Delete the tables if they already exist */ | |
drop table if exists Movie; | |
drop table if exists Reviewer; | |
drop table if exists Rating; | |
/* Create the schema for our tables */ | |
create table Movie(mID int, title text, year int, director text); | |
create table Reviewer(rID int, name text); | |
create table Rating(rID int, mID int, stars int, ratingDate date); | |
/* Populate the tables with our data */ | |
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming'); | |
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas'); | |
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise'); | |
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg'); | |
insert into Movie values(105, 'Titanic', 1997, 'James Cameron'); | |
insert into Movie values(106, 'Snow White', 1937, null); | |
insert into Movie values(107, 'Avatar', 2009, 'James Cameron'); | |
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg'); | |
insert into Reviewer values(201, 'Sarah Martinez'); | |
insert into Reviewer values(202, 'Daniel Lewis'); | |
insert into Reviewer values(203, 'Brittany Harris'); | |
insert into Reviewer values(204, 'Mike Anderson'); | |
insert into Reviewer values(205, 'Chris Jackson'); | |
insert into Reviewer values(206, 'Elizabeth Thomas'); | |
insert into Reviewer values(207, 'James Cameron'); | |
insert into Reviewer values(208, 'Ashley White'); | |
insert into Rating values(201, 101, 2, '2011-01-22'); | |
insert into Rating values(201, 101, 4, '2011-01-27'); | |
insert into Rating values(202, 106, 4, null); | |
insert into Rating values(203, 103, 2, '2011-01-20'); | |
insert into Rating values(203, 108, 4, '2011-01-12'); | |
insert into Rating values(203, 108, 2, '2011-01-30'); | |
insert into Rating values(204, 101, 3, '2011-01-09'); | |
insert into Rating values(205, 103, 3, '2011-01-27'); | |
insert into Rating values(205, 104, 2, '2011-01-22'); | |
insert into Rating values(205, 108, 4, null); | |
insert into Rating values(206, 107, 3, '2011-01-15'); | |
insert into Rating values(206, 106, 5, '2011-01-19'); | |
insert into Rating values(207, 107, 5, '2011-01-20'); | |
insert into Rating values(208, 104, 3, '2011-01-02'); | |
/*练习题及解答*/ | |
-- 1. Find the names of all reviewers who rated Gone with the Wind. | |
-- 1. 查找评价了电影Gone with the Wind的评论家姓名 | |
SELECT DISTINCT name | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
INNER 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. | |
-- 2. 某评论家与该电影的导演同名,请列出评论家姓名、电影名、评价 | |
SELECT name, title, stars | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
INNER JOIN Reviewer USING(rId) | |
WHERE director = name; | |
-- 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".) | |
-- 3. 列出所有评论家姓名和电影名,输出到一个列表中,按照字母顺序排序 | |
SELECT title FROM Movie | |
UNION | |
SELECT name FROM Reviewer | |
ORDER BY name, title; | |
-- 4. Find the titles of all movies not reviewed by Chris Jackson. | |
-- 4. 列出没有被Chris Jackson评价的所有电影 | |
SELECT title | |
FROM Movie | |
WHERE mId NOT IN ( | |
SELECT mId | |
FROM Rating | |
INNER 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. | |
-- 5. 查找评审了同一部电影的2名评论家,返回2名评审家的姓名,去除重复的,不显示评论家自己的2次评价,一对评论家只显示一次,2名评论家按照字母顺序排序 | |
select distinct name1,name2 | |
from | |
(select r1.rid rid1,r1.name name1,ra.mid m1 from reviewer r1,rating ra where r1.rID=ra.rid), | |
(select r2.rid rid2,r2.name name2,ra.mid m2 from reviewer r2,rating ra where r2.rID=ra.rid) | |
where name1<name2 and m1=m2 | |
order by name1,name2; | |
-- 6. For each rating that is the lowest (fewest stars) currently in the database, return the reviewer name, movie title, and number of stars. | |
-- 6. 查找评价最低的电影,列出评价者、电影名、评价 | |
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. | |
-- 7. 列出电影名及其平均评价,从高评价到低评价,评级相同时按照字母顺序 | |
SELECT title, AVG(stars) AS average | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
GROUP BY mId | |
ORDER BY average DESC, title; | |
-- 8. Find the names of all reviewers who have contributed three or more ratings. | |
-- 8. 列出评价了3次或者更多次数的评论家姓名 | |
SELECT name | |
FROM Reviewer | |
WHERE (SELECT COUNT(*) FROM Rating WHERE Rating.rId = Reviewer.rId) >= 3; | |
SELECT name | |
FROM Reviewer | |
INNER JOIN Rating USING(rId) | |
GROUP BY rId | |
HAVING COUNT(*) >= 3; | |
-- At least 3 ratings to different movies (Remainder to myself) | |
SELECT name | |
FROM Reviewer | |
WHERE (SELECT COUNT(DISTINCT mId) FROM Rating WHERE Rating.rId = Reviewer.rId) >= 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. | |
-- 9. 有些导演编导了2部以上的电影,请找到这些导演,列出他们编导的电影名及导演名,按照导演名、电影名排序 | |
SELECT title, director | |
FROM Movie M1 | |
WHERE (SELECT COUNT(*) FROM Movie M2 WHERE M1.director = M2.director) > 1 | |
ORDER BY director, title; | |
SELECT M1.title, director | |
FROM Movie M1 | |
INNER JOIN Movie M2 USING(director) | |
GROUP BY M1.mId | |
HAVING COUNT(*) > 1 | |
ORDER BY director, M1.title; | |
select title,director | |
from movie | |
where director in | |
(select director from movie group by director having count(*) > 1) | |
order by director,title; | |
-- 10. Find the movie(s) with the highest average rating. Return the movie title(s) and average rating. | |
-- 10. 查找平均评价最高的电影,列出电影名及平均评价 | |
SELECT title, AVG(stars) AS average | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
GROUP BY mId | |
HAVING average = ( | |
SELECT MAX(average_stars) | |
FROM ( | |
SELECT title, AVG(stars) AS average_stars | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
GROUP BY mId | |
) | |
); | |
select title,maxavgstars | |
from movie inner join | |
(select mid,max(avgstars) maxavgstars | |
from | |
(select mid,avg(stars) avgstars | |
from rating | |
group by mid)) a | |
on movie.mid=a.mid; | |
-- 11. Find the movie(s) with the lowest average rating. Return the movie title(s) and average rating. | |
-- 11. 查找平均评价最低的电影,列出电影名及平均评价 | |
SELECT title, AVG(stars) AS average | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
GROUP BY mId | |
HAVING average = ( | |
SELECT MIN(average_stars) | |
FROM ( | |
SELECT title, AVG(stars) AS average_stars | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
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. | |
-- 12. 查找各导演在他们执导的电影中获得最高评价的电影,列出导演名、电影名、评价,不考虑导演名为空的电影 | |
SELECT director, title, MAX(stars) | |
FROM Movie | |
INNER JOIN Rating USING(mId) | |
WHERE director IS NOT NULL | |
GROUP BY 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
/*这是斯坦福大学网络公开课DB5 SQL的练习题解答*/ | |
/*第五部分:SQL Movie-Rating Modification Exercises*/ | |
/*数据环境准备*/ | |
/* Delete the tables if they already exist */ | |
drop table if exists Movie; | |
drop table if exists Reviewer; | |
drop table if exists Rating; | |
/* Create the schema for our tables */ | |
create table Movie(mID int, title text, year int, director text); | |
create table Reviewer(rID int, name text); | |
create table Rating(rID int, mID int, stars int, ratingDate date); | |
/* Populate the tables with our data */ | |
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming'); | |
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas'); | |
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise'); | |
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg'); | |
insert into Movie values(105, 'Titanic', 1997, 'James Cameron'); | |
insert into Movie values(106, 'Snow White', 1937, null); | |
insert into Movie values(107, 'Avatar', 2009, 'James Cameron'); | |
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg'); | |
insert into Reviewer values(201, 'Sarah Martinez'); | |
insert into Reviewer values(202, 'Daniel Lewis'); | |
insert into Reviewer values(203, 'Brittany Harris'); | |
insert into Reviewer values(204, 'Mike Anderson'); | |
insert into Reviewer values(205, 'Chris Jackson'); | |
insert into Reviewer values(206, 'Elizabeth Thomas'); | |
insert into Reviewer values(207, 'James Cameron'); | |
insert into Reviewer values(208, 'Ashley White'); | |
insert into Rating values(201, 101, 2, '2011-01-22'); | |
insert into Rating values(201, 101, 4, '2011-01-27'); | |
insert into Rating values(202, 106, 4, null); | |
insert into Rating values(203, 103, 2, '2011-01-20'); | |
insert into Rating values(203, 108, 4, '2011-01-12'); | |
insert into Rating values(203, 108, 2, '2011-01-30'); | |
insert into Rating values(204, 101, 3, '2011-01-09'); | |
insert into Rating values(205, 103, 3, '2011-01-27'); | |
insert into Rating values(205, 104, 2, '2011-01-22'); | |
insert into Rating values(205, 108, 4, null); | |
insert into Rating values(206, 107, 3, '2011-01-15'); | |
insert into Rating values(206, 106, 5, '2011-01-19'); | |
insert into Rating values(207, 107, 5, '2011-01-20'); | |
insert into Rating values(208, 104, 3, '2011-01-02'); | |
/*练习题及解答*/ | |
-- Q1 Add the reviewer Roger Ebert to your database, with an rID of 209. | |
-- Q1 追加一名评论家Roger Ebert,rID为209 | |
insert into reviewer values(209,'Roger Ebert'); | |
-- Q2 Insert 5-star ratings by James Cameron for all movies in the database. Leave the review date as NULL. | |
-- Q2 插入James Cameron对所有电影的5星评价,评价日不设置(NULL) | |
insert into Rating | |
select rID,mID,5,null from Reviewer, Movie | |
where name="James Cameron"; | |
-- Q3 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.) | |
-- Q3 将所有平均评价为4星或更高的电影的发行年增加25(更新已有数据,不要追加数据), | |
update Movie set year=year+25 where | |
mID in (select mID from (select Rating.mID,avg(stars) as average | |
from Movie, Rating where Movie.mID=Rating.mID | |
group by Rating.mID, year) where average>=4); | |
-- Q4 Remove all ratings where the movie's year is before 1970 or after 2000, and the rating is fewer than 4 stars. | |
-- Q4 删除电影发行年在1970年之前或者2000年之后,并且评价小于4星的所有评价 | |
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