"""Offline SQLite lab for SQL Chapter 19: INNER JOIN."""
import json
import sqlite3
import sys
def make_case(name: str, summary: str, expected, actual) -> dict:
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main() -> None:
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.executescript(
"""
CREATE TABLE Students (
StudentID INT,
Name TEXT,
Age INT
);
INSERT INTO Students VALUES
(1, 'Alice', 22),
(2, 'Bob', 23),
(3, 'Charlie', 24);
CREATE TABLE Enrollments (
EnrollmentID INT,
StudentID INT,
Course TEXT
);
INSERT INTO Enrollments VALUES
(101, 1, 'Math'),
(102, 2, 'Science'),
(103, 4, 'History');
"""
)
inner_res = [
list(r)
for r in cur.execute(
"""
SELECT Students.Name, Enrollments.Course
FROM Students
INNER JOIN Enrollments
ON Students.StudentID = Enrollments.StudentID
ORDER BY Students.StudentID
"""
).fetchall()
]
short_join_res = [
list(r)
for r in cur.execute(
"""
SELECT Students.Name, Enrollments.Course
FROM Students
JOIN Enrollments
ON Students.StudentID = Enrollments.StudentID
ORDER BY Students.StudentID
"""
).fetchall()
]
case1 = make_case(
"inner_join_vs_join_syntax",
"Verify INNER JOIN and shorthand JOIN yield identical matched rows on Students and Enrollments",
{
"inner_rows": [["Alice", "Math"], ["Bob", "Science"]],
"same_as_join": True,
},
{
"inner_rows": inner_res,
"same_as_join": inner_res == short_join_res,
},
)
matched_names = [r[0] for r in inner_res]
matched_courses = [r[1] for r in inner_res]
case2 = make_case(
"students_enrollments_exclusion",
"Verify unmatched rows (Charlie with StudentID=3 and History with StudentID=4) are excluded",
{
"row_count": 2,
"charlie_included": False,
"history_included": False,
},
{
"row_count": len(inner_res),
"charlie_included": "Charlie" in matched_names,
"history_included": "History" in matched_courses,
},
)
cur.executescript(
"""
CREATE TABLE Websites (
id INT,
name TEXT,
url TEXT,
alexa INT,
country TEXT
);
INSERT INTO Websites VALUES
(1, 'Google', 'https://www.google.com/', 1, 'USA'),
(2, '淘宝', 'https://www.taobao.com/', 13, 'CN'),
(3, '蓝色天空', 'https://www.mbsky.com/', 4689, 'CN'),
(4, '微博', 'https://weibo.com/', 20, 'CN'),
(5, 'Facebook', 'https://www.facebook.com/', 3, 'USA'),
(7, 'stackoverflow', 'https://stackoverflow.com/', 0, 'IND');
CREATE TABLE access_log (
aid INT,
site_id INT,
count INT,
date TEXT
);
INSERT INTO access_log VALUES
(1, 1, 45, '2016-05-10'),
(2, 3, 100, '2016-05-13'),
(3, 1, 230, '2016-05-14'),
(4, 2, 10, '2016-05-14'),
(5, 5, 205, '2016-05-14'),
(6, 4, 13, '2016-05-15'),
(7, 3, 220, '2016-05-15'),
(8, 5, 545, '2016-05-16'),
(9, 3, 201, '2016-05-17');
"""
)
ordered_rows = [
list(r)
for r in cur.execute(
"""
SELECT Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id = access_log.site_id
ORDER BY access_log.count
"""
).fetchall()
]
case3 = make_case(
"websites_access_log_ordered",
"Verify Websites INNER JOIN access_log ordered ascending by count excludes stackoverflow",
{
"row_count": 9,
"min_row": ["淘宝", 10, "2016-05-14"],
"max_row": ["Facebook", 545, "2016-05-16"],
"counts": [10, 13, 45, 100, 201, 205, 220, 230, 545],
"has_stackoverflow": False,
},
{
"row_count": len(ordered_rows),
"min_row": ordered_rows[0],
"max_row": ordered_rows[-1],
"counts": [r[1] for r in ordered_rows],
"has_stackoverflow": any(r[0] == "stackoverflow" for r in ordered_rows),
},
)
conn.close()
payload = {
"version": f"CPython {sys.version.split()[0]} / SQLite {sqlite3.sqlite_version}",
"cases": [case1, case2, case3],
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、内置 sqlite3(SQLite 3.53.1)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、内置 sqlite3(SQLite 3.53.1)本地离线环境。
入口文件:projects/sql-join-inner-lab/explore-join-inner.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-join-inner.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、内置 sqlite3(SQLite 3.53.1)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联67章) · 含当前章节或实例 · mbskycom202612a1523.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1566.zip