数据库sql习题及答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*
Question 1:Find the titles of all movies directed by Steven Spielberg. select title from movie where director='Steven Spielberg'
Question 2:Find all years that have a movie that received a rating
of 4 or 5, and sort them in increasing order.
select distinct year from movie,rating
where =
and stars in (4,5)
order by year
Question 3:Find the titles of all movies that have no ratings
select title from movie
where mID in
(
select mid from Movie
except
select mid from rating
)
select title from movie
except
select title from movie,rating
where =
Question 4: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.
select name from reviewer,rating
where =
and ratingdate is null
Question 5: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.
select name,title,stars,ratingdate
from movie,rating,reviewer
where =
and =
order by name,title,stars
Question 6: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.
select name,title
from rating as F,Rating as S,Movie,Reviewer
where =
and =
and <
and <
and =
and =
select name,title
from movie,reviewer,
( select , from rating as r1,rating as r2
where = and =
and >
and > as r
where = and =
Question 7:For each movie, find the highest number of stars
that movie received as a rating. Return the movie title and number of stars. Sort by movie title.
select title,MAX(stars) as max_star
from rating,Movie
where =
group by title
order by title
select title,maxrating
from movie,
(select mid,max(stars) as maxrating from rating
group by mid) as r
where =
order by title
Question 8: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.
select title,MAX(stars)-MIN(stars) as rating_spread
from rating,Movie
where =
group by title
order by rating_spread desc,title
select title,spread as "rating spread"
from movie, (select mid,max(stars)-min(stars) as
spread from rating group by mid) as r
where =
order by spread desc,title
Question 9: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.)
select -
from
(
select AVG(avg_star) as avg_star from
(
select ,AVG(stars) as avg_star
from Rating left join Movie on =
where year<1980
group by
) as F
) as F
,
(
select AVG(avg_star) as avg_star from
(
select ,AVG(stars) as avg_star
from Rating left join Movie on =
where year>=1980
group by
) as S
) as S