SELECTDISTINCT(language) FROM akas ORDERBYlanguage LIMIT 10;
Q2
1 2 3 4 5
SELECT primary_title, premiered, runtime_minutes ||' (mins)' FROM titles WHERE genres LIKE'%Sci-Fi%' ORDERBY runtime_minutes DESC LIMIT 10;
话说既然 Gradescope
评测结果里面已经有答案了能不能直接搬过来
Q3
1 2 3 4 5
SELECT name, 2022- born AS age FROM people WHERE born >=1900AND died ISNULL ORDERBY age DESC, name LIMIT 20;
官方答案似乎连 died 也要算进去(不过不影响)
Q4
开始上强度了?
其实是因为没兴趣没怎么学 SQL 遭报应了
我更感兴趣的还是 DBMS 内部实现(
最后只能凭借着之前上的蜜汁数据库课(不如叫 Excel
课)以及搜索引擎硬写
1 2 3 4 5
SELECT name, COUNT(*) as num_appearances FROM crew JOIN people ON crew.person_id = people.person_id GROUPBY crew.person_id ORDERBY num_appearances DESC LIMIT 20;
Q5
1 2 3 4 5 6 7 8 9 10
SELECT CAST(titles.premiered /10*10ASCHAR) ||'s'AS decade, ROUND(AVG(ratings.rating), 2) AS avg_rating, ROUND(MAX(ratings.rating), 2), ROUND(MIN(ratings.rating), 2), COUNT(*) FROM ratings INNERJOIN titles USING(title_id) WHERE titles.premiered ISNOTNULL GROUPBY decade ORDERBY avg_rating DESC, decade;
Q6
嵌套!都可以嵌!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SELECT titles.primary_title, ratings.votes FROM titles INNERJOIN ratings USING(title_id) WHERE title_id IN ( SELECTDISTINCT(title_id) FROM crew WHERE person_id IN ( SELECT person_id FROM people WHERE name LIKE'%Cruise%' and born =1962 ) ) ORDERBY ratings.votes DESC LIMIT 10;
Q7
如果先查出来是 2021 年可以简化代码吗,或者直接交答案
1 2 3 4 5 6 7
SELECTCOUNT(DISTINCT title_id) FROM titles WHERE premiered IN ( SELECT premiered FROM titles WHERE primary_title ='Army of Thieves' );
Q8
感觉到后面都是一个套路了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
SELECTDISTINCT(people.name) FROM crew INNERJOIN people USING(person_id) WHERE crew.title_id IN ( SELECT title_id FROM crew WHERE person_id IN ( SELECT person_id FROM people WHERE name ='Nicole Kidman' and born =1967 ) ) AND ( crew.category ='actor' OR crew.category ='actress' ) ORDERBY people.name;
WITH actor_avg_rating AS ( SELECT name, person_id, ROUND(AVG(rating), 2) as avg_rating FROM people INNERJOIN crew USING(person_id) INNERJOIN titles USING(title_id) INNERJOIN ratings USING(title_id) WHERE born =1955 AND titles.type ='movie' GROUPBY person_id ) SELECT name, avg_rating FROM ( SELECT*, NTILE(10) OVER( ORDERBY avg_rating ) AS quartile FROM actor_avg_rating ) WHERE quartile =9 ORDERBY avg_rating DESC, name LIMIT 10;
with p as ( select titles.primary_title as name, akas.title as dubbed from titles innerjoin akas on titles.title_id = akas.title_id where titles.primary_title = "House of the Dragon" AND titles.type ='tvSeries' groupby titles.primary_title, akas.title orderby akas.title ), c as ( selectrow_number() over ( orderby p.name asc ) as seqnum, p.dubbed as dubbed from p ), flattened as ( select seqnum, dubbed from c where seqnum =1 unionall select c.seqnum, f.dubbed ||', '|| c.dubbed from c join flattened f on c.seqnum = f.seqnum +1 ) select dubbed from flattened orderby seqnum desc limit 1;