// 输出电影里面所有类别(不重复) sqlite> select distinct(type) from titles order by type; movie short tvEpisode tvMiniSeries tvMovie tvSeries tvShort tvSpecial video videoGame
Q2 UNCOMMON_TYPE
List the longest title of each type along with the runtime minutes.
-- select longest runtime by type and its type WITH temp(maxruntime, t) AS ( SELECTMAX(runtime_minutes), titles.type FROM titles GROUPBYtype )
SELECT titles.type, titles.primary_title, titles.runtime_minutes FROM titles, temp WHERE titles.runtime_minutes = temp.maxruntime AND titles.type = temp.t ORDERBYtypeASC, primary_title ASC;
1 2 3 4 5 6 7 8 9 10 11 12 13
-- 结果 sqlite> .read ./placeholder/q2_uncommon_type.sql movie|Logistics|51420 short|Kuriocity|461 tvEpisode|Téléthon 2012|1800 tvMiniSeries|Kôya no yôjinbô|1755 tvMovie|ArtQuench Presents Spirit Art|2112 tvSeries|The Sharing Circle|8400 tvShort|Paul McCartney Backstage at Super Bowl XXXIX|60 tvShort|The People Next Door|60 tvSpecial|Katy Perry Live: Witness World Wide|5760 video|Midnight Movie Madness: 50 Movie Mega Pack|5135 videoGame|Flushy Fish VR: Just Squidding Around|1500
Q3 TV_VS_MOVIE
List all types of titles along with the number of associated titles.
输出每个类别和该类别的电影数量,按数量升序输出。 这道题相对上一道题比较简单,代码如下:
1 2 3 4
WITH type_number(type, number) AS ( SELECTtype, COUNT(*) FROM titles GROUPBYtype ) SELECT * FROM type_number ORDERBYnumberASC;
-- 输出 sqlite> .read ./placeholder/q6_dubbed_smash.sql Mutant Virus: Vol. 1|126 The Good, the Bad and the Ugly|73 Star Wars: Episode V - The Empire Strikes Back|71 Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb|68 Raiders of the Lost Ark|62 Star Wars: Episode VII - The Force Awakens|62 The Shawshank Redemption|61 Once Upon a Timein the West|60 Indiana Jones and the Kingdom of the Crystal Skull|60 Airplane!|59
Q7 IMDB_250
List the IMDB Top 250 movies along with its weighted rating.
按权重分数降序输出IMDB TOP250电影,计算规则如下
1 2 3 4 5
Weighted rating (WR) = (v/(v+m)) * R + (m/(v+m)) * C R = 电影平均得分 i.e. ratings.rating v = 电影投票数, i.e. ratings.votes m = 要进top250的最少投票数 (current 25000) C = 所有投票的平均得分 (计算方式是所有投票乘相应得分再除以总票数)
WITH title_with_hamill(title_id) AS ( SELECT title_id FROM crew, people WHERE crew.person_id = people.person_id AND people.name = "Mark Hamill" AND people.born = 1951 )
SELECTCOUNT(DISTINCT person_id) FROM crew, title_with_hamill WHERE crew.title_id = title_with_hamill.title_id AND (crew.category = "actor"OR crew.category = "actress");
List the movies in alphabetical order which cast both Mark Hamill (born in 1951) and George Lucas (born in 1944).
输出所有Mark Hamill 和 George Lucas 共同参演的电影名,按字母顺序输出,思路跟上一题差不多,找到mark hamill 参演的电影在在里面找 Georage Lucas 参演的过输出名字就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
WITH title_with_hamill(title_id) AS ( SELECT title_id FROM crew, people WHERE crew.person_id = people.person_id AND people.name = "Mark Hamill" AND people.born = 1951 )
SELECTDISTINCT primary_title FROM crew, title_with_hamill, people, titles WHERE crew.title_id = title_with_hamill.title_id AND titles.type = "movie" AND crew.title_id = titles.title_id AND (crew.person_id = people.person_id AND people.name = "George Lucas" AND people.born = 1944) ORDERBY primary_title;
1 2 3
-- 输出 sqlite> .read ./placeholder/q9_movie_names.sql Star Wars: Episode V - The Empire Strikes Back
Q10 GENRE_COUNTS
List all distinct genres and the number of titles associated with them.