斯坦福大学sql练习(基础篇)

 这是斯坦福大学的公开课上关于sql数据库知识的基础测试,看看计算机顶尖学府是怎么出题的?真心有水平。
You've started a new movie-rating website, and you've been collecting data on reviewers' ratings of various movies. There's not much data yet, but you can still try out some interesting queries. Here's the schema:

Movie ( mID, title, year, director )
English: There is a movie with ID number mID, a title, a release year, and a director.

Reviewer ( rID, name )
English: The reviewer with ID number rID has a certain name.

Rating ( rID, mID, stars, ratingDate )
English: The reviewer rID gave the movie mID a number of stars rating (1-5) on a certain ratingDate.
 
Movie
mIDtitleyeardirector
101Gone with the Wind1939Victor Fleming
102Star Wars1977George Lucas
103The Sound of Music1965Robert Wise
104E.T.1982Steven Spielberg
105Titanic1997James Cameron
106Snow White1937<null>
107Avatar2009James Cameron
108Raiders of the Lost Ark1981Steven Spielberg

Reviewer
rIDname
201Sarah Martinez
202Daniel Lewis
203Brittany Harris
204Mike Anderson
205Chris Jackson
206Elizabeth Thomas
207James Cameron
208Ashley White

Rating(这里的stars是几星的意思,就是评价)
rIDmIDstarsratingDate
20110122011-01-22
20110142011-01-27
2021064<null>
20310322011-01-20
20310842011-01-12
20310822011-01-30
20410132011-01-09
20510332011-01-27
20510422011-01-22
2051084<null>
20610732011-01-15
20610652011-01-19
20710752011-01-20
20810432011-01-02
Question 1:Find the titles of all movies directed by Steven Spielberg.
1 select title from Movie
2 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.
1 select distinct Movie.year from Movie
2 join Rating
3 on Movie.mID=Rating.mID and (Rating.stars=4 or Rating.stars=5)
4 order by Movie.year
Question 3:Find the titles of all movies that have no ratings.
1 select distinct Movie.title from Movie
2 left join Rating
3 on Movie.mID=Rating.mID
4 where Rating.mID is null
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.
1 select Reviewer.name from Reviewer
2 join Rating
3 on Reviewer.rID=Rating.rID
4 where Rating.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.
1 select Reviewer.name,Movie.title,Rating.stars,Rating.ratingDate from 
2 (Reviewer join Rating) join Movie
3 on Reviewer.rID=Rating.rID and Rating.mID=Movie.mID
4 order by Reviewer.name,Movie.title,Rating.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.

1 select  Reviewer.name,Movie.title from Movie join
2 (select Rating.mID,Rating.rID,Rating.stars,Rating.ratingDate from Rating,
3 (select Rating.mID,Rating.rID,max(Rating.stars) as maxStars,max(Rating.ratingDate) as maxDate from Rating
4 group by mID,rID
5 having count(rId)=2 and count(mID)=2) as nan
6 where Rating.mID=nan.mID and Rating.rID=nan.rID and Rating.stars=nan.maxStars and Rating.ratingDate=maxDate) as nannan
7 on Movie.mID=nannan.mID join Reviewer on Reviewer.rID=nannan.rID
或者:
select Movie.title,Reviewer.name from Movie join
(select Rating.rID,Rating.mID from Rating,
(select Rating.rID,Rating.mID,max(Rating.stars) as maxStar,max(Rating.ratingDate) as maxDate from Rating
group by Rating.rID,Rating.mID
having count(rID)=2) as nan
where nan.maxStar=Rating.Stars and nan.maxDate=Rating.ratingDate) as nannan
on Movie.mID=nannan.mID
join Reviewer on Reviewer.rID=nannan.rID
Question 9: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.
1 select Movie.title,nan.maxStars from Movie join
2 (select Rating.mID,max(Rating.stars) as maxStars from Rating
3 group by mID) as nan
4 on Movie.mID=nan.mID
5 ORDER BY Movie.title
Question 8: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.
1 select Movie.title,nan.avgStars from Movie join
2 (select Rating.mID,avg(Rating.stars) as avgStars from Rating
3 group by Rating.mID
4 ) as nan
5 on Movie.mID=nan.mID
6 order by nan.avgStars desc,Movie.title

Question 9:Find the names of all reviewers who have contributed three or more ratings. (As an extra challenge, try writing the query without HAVING or without COUNT.)
常规方法:

1 select Reviewer.name from Reviewer join 
2 (select Rating.rID,count(Rating.rID) as countRID from Rating
3 group by rID
4 having countRID>=3) as nan
5 on nan.rID=Reviewer.rID

非常规方法暂时还没有想到。

Question 10: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.

1 select Movie.title,nan.spread from Movie join
2 (select Rating.mID,(max(Rating.stars)-min(Rating.stars)) as spread from Rating
3 group by Rating.mID)as nan
4 on Movie.mID=nan.mID
5 order by nan.spread desc,Movie.title

未完待续。。。

SELECT tbl_info.name, tbl_info.num, tbl_info.phone, tbl_info.faculty, tbl_info.qq, tbl_grade.name, tbl_class.name
FROM tbl_info
JOIN tbl_grade
JOIN tbl_class ON tbl_info.grade_id = tbl_grade.id
AND tbl_info.class_id = tbl_class.id

。。。

转载于:https://www.cnblogs.com/nannanITeye/archive/2013/04/11/3008627.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值