"""SQL SELECT TOP / LIMIT 离线实验:验证 LIMIT 限行、配合 ORDER BY 确定顺序及百分比等效计算。"""
import json
import math
import platform
import sqlite3
def run() -> dict:
conn = sqlite3.connect(":memory:")
conn.executescript(
"""
CREATE TABLE Employees (
EmployeeID INTEGER PRIMARY KEY,
EmployeeName TEXT NOT NULL,
Salary INTEGER NOT NULL
);
INSERT INTO Employees VALUES
(1, 'John Smith', 50000),
(2, 'Maria Garcia', 60000),
(3, 'Liam Johnson', 70000),
(4, 'Emma Wilson', 80000),
(5, 'Oliver Brown', 90000);
CREATE TABLE websites (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
url TEXT NOT NULL,
alexa INTEGER NOT NULL,
country TEXT NOT NULL
);
INSERT INTO websites VALUES
(1, 'Google', 'https://www.google.com/', 1, 'USA'),
(2, '淘宝', 'https://www.taobao.com/', 13, 'CN'),
(3, '蓝色天空', 'http://www.mbsky.com/', 5000, 'USA'),
(4, '微博', 'http://weibo.com/', 20, 'CN'),
(5, 'Facebook', 'https://www.facebook.com/', 3, 'USA'),
(7, 'stackoverflow', 'http://stackoverflow.com/', 0, 'IND');
"""
)
cases = []
# Case 1: Employees LIMIT 3 and ORDER BY Salary DESC LIMIT 3
top3_id = [
[r[0], r[1]]
for r in conn.execute(
"SELECT EmployeeName, Salary FROM Employees ORDER BY EmployeeID ASC LIMIT 3;"
).fetchall()
]
top3_salary = [
[r[0], r[1]]
for r in conn.execute(
"SELECT EmployeeName, Salary FROM Employees ORDER BY Salary DESC LIMIT 3;"
).fetchall()
]
exp1 = {
"top3_by_id": [["John Smith", 50000], ["Maria Garcia", 60000], ["Liam Johnson", 70000]],
"top3_by_salary_desc": [["Oliver Brown", 90000], ["Emma Wilson", 80000], ["Liam Johnson", 70000]],
}
act1 = {"top3_by_id": top3_id, "top3_by_salary_desc": top3_salary}
cases.append(
{
"name": "limit_employees_top3_and_orderby",
"summary": "在 Employees 表上验证 LIMIT 3 及结合 ORDER BY Salary DESC 获取薪资前三行",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: Websites LIMIT 2
top2_sites = [
[r[0], r[1]]
for r in conn.execute("SELECT id, name FROM websites ORDER BY id ASC LIMIT 2;").fetchall()
]
exp2 = [[1, "Google"], [2, "淘宝"]]
act2 = top2_sites
cases.append(
{
"name": "limit_websites_top2",
"summary": "执行 SELECT * FROM websites LIMIT 2 选取前两条站点记录",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: SQLite equivalent of TOP 10 PERCENT / TOP 50 PERCENT & LIMIT OFFSET pagination
emp_total = conn.execute("SELECT COUNT(*) FROM Employees;").fetchone()[0]
emp_10pct_n = math.ceil(emp_total * 0.10)
emp_10pct = [
r[0]
for r in conn.execute(
"SELECT EmployeeName FROM Employees ORDER BY EmployeeID ASC LIMIT ?;",
(emp_10pct_n,),
).fetchall()
]
site_total = conn.execute("SELECT COUNT(*) FROM websites;").fetchone()[0]
site_50pct_n = math.ceil(site_total * 0.50)
site_50pct = [
r[0]
for r in conn.execute(
"SELECT name FROM websites ORDER BY id ASC LIMIT ?;",
(site_50pct_n,),
).fetchall()
]
page2 = [
r[0]
for r in conn.execute(
"SELECT name FROM websites ORDER BY id ASC LIMIT 2 OFFSET 2;"
).fetchall()
]
exp3 = {
"employees_10pct_ceil": ["John Smith"],
"websites_50pct_ceil": ["Google", "淘宝", "蓝色天空"],
"websites_page2_offset2": ["蓝色天空", "微博"],
}
act3 = {
"employees_10pct_ceil": emp_10pct,
"websites_50pct_ceil": site_50pct,
"websites_page2_offset2": page2,
}
cases.append(
{
"name": "percent_equivalent_and_offset",
"summary": "以向上取整计算等效前 10% 与前 50% 行数,并验证 LIMIT 2 OFFSET 2 分页",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
}
)
conn.close()
return {
"version": f"CPython {platform.python_version()} / SQLite {sqlite3.sqlite_version}",
"cases": cases,
}
if __name__ == "__main__":
print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/sql-top-lab/explore-top.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-top.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联67章) · 含当前章节或实例 · mbskycom202612a1523.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1580.zip