Skip to content

Commit 2848aa6

Browse files
author
William Yang
committed
feat: add songs SQL stage
- Add songs stage (Spotify SQL queries) - 8 SQL queries + answers.txt reflection - Update course.yml stage_order
1 parent d412cc7 commit 2848aa6

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

course.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ stage_order:
4848
- credit
4949
- scrabble
5050
- readability
51+
- songs

stages/songs.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Songs
2+
3+
## 背景
4+
5+
假设你在 Spotify 工作,需要分析 2018 年最热门的 100 首歌曲数据。你有一个 SQLite 数据库 `songs.db`,其中包含这些歌曲的信息。
6+
7+
## 数据库结构
8+
9+
数据库包含两个表:
10+
11+
### songs 表
12+
13+
| 列名 | 类型 | 描述 |
14+
| ------------ | ------- | -------------------- |
15+
| id | INTEGER | 歌曲唯一标识 |
16+
| name | TEXT | 歌曲名称 |
17+
| artist_id | INTEGER | 艺术家 ID (外键) |
18+
| danceability | REAL | 可舞性 (0.0-1.0) |
19+
| energy | REAL | 能量值 (0.0-1.0) |
20+
| key | INTEGER | 调性 (0-11) |
21+
| loudness | REAL | 响度 (dB) |
22+
| speechiness | REAL | 语言性 (0.0-1.0) |
23+
| valence | REAL | 情绪积极度 (0.0-1.0) |
24+
| tempo | REAL | 节奏 (BPM) |
25+
| duration_ms | INTEGER | 时长 (毫秒) |
26+
27+
### artists 表
28+
29+
| 列名 | 类型 | 描述 |
30+
| ---- | ------- | -------------- |
31+
| id | INTEGER | 艺术家唯一标识 |
32+
| name | TEXT | 艺术家名称 |
33+
34+
## 任务
35+
36+
编写 SQL 查询来回答以下问题。每个查询保存在对应编号的 `.sql` 文件中。
37+
38+
### 1.sql
39+
40+
列出所有歌曲的名称。
41+
42+
### 2.sql
43+
44+
按节奏(tempo)升序列出所有歌曲的名称。
45+
46+
### 3.sql
47+
48+
列出时长最长的 5 首歌曲名称(按 duration_ms 降序)。
49+
50+
### 4.sql
51+
52+
列出可舞性(danceability)、能量值(energy)和情绪积极度(valence)都大于 0.75 的歌曲名称。
53+
54+
### 5.sql
55+
56+
计算所有歌曲的平均能量值。
57+
58+
### 6.sql
59+
60+
列出 Post Malone 的所有歌曲名称。
61+
62+
### 7.sql
63+
64+
计算 Drake 歌曲的平均能量值。
65+
66+
### 8.sql
67+
68+
列出名称中包含 "feat." 的歌曲名称。
69+
70+
## answers.txt
71+
72+
`answers.txt` 中回答以下问题(用几句话):
73+
74+
- 你认为 Spotify 的这些音乐特征(如 danceability、energy)是如何计算的?
75+
- 通过这些数据,你发现了 2018 年流行音乐的什么趋势?
76+
77+
## 示例
78+
79+
查询 1.sql 的输出示例:
80+
81+
```
82+
God's Plan
83+
SAD!
84+
rockstar (feat. 21 Savage)
85+
...
86+
```
87+
88+
## 提示
89+
90+
<details>
91+
<summary>点击查看提示</summary>
92+
93+
### 常用 SQL 语法
94+
95+
```sql
96+
-- 选择所有列
97+
SELECT * FROM songs;
98+
99+
-- 选择特定列
100+
SELECT name FROM songs;
101+
102+
-- 条件过滤
103+
SELECT name FROM songs WHERE energy > 0.8;
104+
105+
-- 排序
106+
SELECT name FROM songs ORDER BY energy ASC;
107+
108+
-- 限制结果数量
109+
SELECT name FROM songs ORDER BY energy DESC LIMIT 5;
110+
111+
-- 聚合函数
112+
SELECT AVG(energy) FROM songs;
113+
114+
-- 多表连接
115+
SELECT songs.name
116+
FROM songs
117+
JOIN artists ON songs.artist_id = artists.id
118+
WHERE artists.name = 'Drake';
119+
```
120+
121+
</details>
122+
123+
## 参考资料
124+
125+
- [SQL 教程](https://www.w3schools.com/sql/)
126+
- [SQLite 文档](https://www.sqlite.org/docs.html)

stages/songs.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# stages/songs.yml
2+
version: "1.0"
3+
4+
slug: songs
5+
name: "Songs"
6+
description: songs.md
7+
summary: |
8+
编写 SQL 查询来分析 Spotify 歌曲数据库。
9+
10+
category: Week 7
11+
12+
# SQL 只支持单语言
13+
languages:
14+
sql:
15+
# SQL 题目没有 starter 代码,学生从零开始写
16+
files:
17+
required:
18+
- 1.sql
19+
- 2.sql
20+
- 3.sql
21+
- 4.sql
22+
- 5.sql
23+
- 6.sql
24+
- 7.sql
25+
- 8.sql
26+
- answers.txt
27+
allowed:
28+
- "*.sql"
29+
- "*.txt"
30+
blocked: []
31+
32+
# 评测配置
33+
evaluation:
34+
timeout: 60
35+
36+
hints:
37+
- "使用 SELECT 语句从表中检索数据"
38+
- "使用 WHERE 子句过滤结果"
39+
- "使用 ORDER BY 对结果排序"
40+
- "使用 AVG、COUNT 等聚合函数"
41+
42+
resources:
43+
- title: "Lecture 7: SQL"
44+
url: https://cs50.harvard.edu/x/2024/weeks/7/
45+
- title: "SQL Keywords"
46+
url: https://www.w3schools.com/sql/sql_ref_keywords.asp

0 commit comments

Comments
 (0)