SQL作业:编写统计各创作者家庭电影数量并降序排序的查询
SQL多表关联查询实现指导
题目核心需求
- 输出所有电影系列创作者的完整名单,即便创作者从未创作过「Family Film」类电影也必须纳入结果集
- 为每位创作者统计其名下属于「Family Film」类别的电影总数量
- 结果按统计得到的家庭类电影数量降序排列,创作数量最高的创作者排在列表最顶端
- 编写单条可执行查询,输出列名具备实际业务可读性,无需二次解析
给定表结构
Create Table Genre ( Genre_id decimal(12) NOT NULL Primary Key , Genre_name varchar(64) NOT NULL ) Create Table Creator ( Creator_id decimal(12) not null Primary Key , first_name varchar(64) NOT NULL , last_name varchar(64) NOT NULL ) Create Table Movie_series ( movie_series_id decimal(12) not null Primary Key , genre_id decimal(12) NOT NULL Foreign key references Genre(genre_id) , creator_id decimal(12) NOT NULL Foreign key references Creator(Creator_id) , series_name varchar(255) NOT NULL , suggested_price decimal(8,2) NULL ) Create Table Movie ( Movie_id decimal(12) not null Primary Key , movie_series_id decimal(12) NOT NULL Foreign key references Movie_series(movie_series_id) , movie_name varchar(64) NOT NULL , length_in_minutes decimal(4) )
测试数据集
--Inserting data to Genre Table Insert into Genre (Genre_id, Genre_name) values (1, 'Fantasy'), (2, 'Family Film'), (3, 'Action spy') --Inserting data to Creator Table Insert into Creator (Creator_id, first_name, last_name) values (1, 'George', 'Lucas'), (2, 'John', 'Lasseter'), (3, 'John', 'Tolkien'), (4, 'Bruce', 'Gellar') --Inserting data to Movie_series Table Insert into Movie_series (movie_series_id, genre_id, creator_id, series_name, suggested_price) values (1, 1, 1, 'Star Wars', 129.99), (2, 2, 2, 'Toy Story', 22.13), (3, 1, 3, 'Lord of the Rings', NULL), (4, 3, 4, 'Mission Impossible', 50.00) --Inserting data to Movie Table Insert into Movie (Movie_id, movie_series_id, movie_name, length_in_minutes) values (1, 1, 'Episode I: The Phantom Menace', 136), (2, 1, 'Episode II: Attack of the Clones', 142), (3, 1, 'Episode III: Revenge of the Sith', 140), (4, 1, 'Episode IV: A New Hope', 121), (5, 2, 'Toy Story', 121), (6, 2, 'Toy Story 2', 135), (7, 2, 'Toy Story 3', 148), (8, 3, 'The Lord of the Rings: The Fellowship of the Ring', 228), (9, 3, 'The Lord of the Rings: The Two Towers', 235), (10, 3, 'The Lord of the Rings: The Return of the King', 200), (11, 4, 'Mission: Impossible', 110), (12, 4, 'Mission: Impossible 2', 123), (13, 4, 'Mission: Impossible III', 126), (14, 4, 'Mission: Impossible - Ghost Protocol', 133), (15, 4, 'Mission: Impossible - Rogue Nation', 131), (16, 4, 'Mission: Impossible - Fallout', 147)
预期输出
Creator Number of "Family Film" movies John Lasseter 3 George Lucas 0 John Tolkien 0 Bruce Gellar 0
现有查询的问题定位
你当前编写的查询存在三处核心偏差,无法匹配需求:
- 驱动表选择错误:以Movie表为起点做内连接,会直接过滤掉没有匹配电影记录的条目,无法实现“所有创作者都需纳入结果”的要求
- 统计逻辑错误:直接对
ms.genre_id做计数,实际统计的是创作者所有类型的电影数量,并非仅家庭类电影的数量 - 分组维度错误:按genre_id分组,会把同一个创作者的不同类型电影拆成独立行,无法实现“每个创作者仅占一行结果”的统计逻辑
修正后可直接运行的查询
SELECT c.first_name + ' ' + c.last_name AS Creator, COUNT(m.movie_id) AS [Number of "Family Film" movies] FROM Creator c -- 逐层级左连接,保留所有创作者的主记录 LEFT JOIN Movie_series ms ON c.Creator_id = ms.creator_id -- 把"仅匹配家庭类电影类型"的判断放在左连接的ON条件中,避免过滤掉不匹配的创作者 LEFT JOIN Genre g ON ms.genre_id = g.Genre_id AND g.Genre_name = 'Family Film' LEFT JOIN Movie m ON ms.movie_series_id = m.movie_series_id -- 按创作者主键分组,从根源上保证一人一行结果 GROUP BY c.Creator_id, c.first_name, c.last_name -- 按家庭类电影数量降序排列 ORDER BY [Number of "Family Film" movies] DESC;
逻辑说明
- 以Creator表作为驱动表,所有创作者的记录都会保留,不会因为无关联数据被过滤
- 类型匹配条件放在左连接的ON子句中,而非WHERE子句,对于不是「Family Film」类型的系列,关联Genre和Movie的字段会返回NULL,不会触发过滤
- 计数时使用
COUNT(m.movie_id)自动忽略NULL值,对于没有家庭类电影创作记录的创作者,最终计数自动为0 - 分组时使用Creator表的主键作为核心分组维度,避免出现同姓名创作者被合并统计的问题
内容的提问来源于stack exchange,提问作者CrazyCoder
相关产品推荐
相关产品推荐

