工程与学习方法 / 24

SQL练习:电影评分与社交关系

工具、语法与实践中的边界。 SQL练习:电影评分与社交关系。

阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。

1. Enviroment Setup

1.1 SQL Setup

PYTHON · 仅供阅读
# After Restart runtime, run this cell
import sqlite3 as lite
import pandas as pd

1.2 Create SQL 2 and 3 tables and import data

PYTHON · 仅供阅读
#@title Run this cell to create tables and import data
conn = lite.connect('test.db',timeout=10)
cur= conn.cursor()

# create SQL 2 tables and data
cur.execute("Drop TABLE IF EXISTS customers")
cur.execute("CREATE TABLE IF NOT EXISTS customers(customerid INTEGER, country TEXT)")

cur.execute("Drop TABLE IF EXISTS categories")
cur.execute("CREATE TABLE IF NOT EXISTS categories(categoryid INTEGER, categoryname TEXT)")

cur.execute("Drop TABLE IF EXISTS employees")
cur.execute("CREATE TABLE IF NOT EXISTS employees(employeeid INTEGER, birthdate BLOB)")

cur.execute("Drop TABLE IF EXISTS orderdetails")
cur.execute("CREATE TABLE IF NOT EXISTS orderdetails(orderid INTEGER, productid INTEGER, quantity INTEGER)")

cur.execute("Drop TABLE IF EXISTS orders")
cur.execute("CREATE TABLE IF NOT EXISTS orders(orderid INTEGER, customerid INTEGER)")

cur.execute("Drop TABLE IF EXISTS products")
cur.execute("CREATE TABLE IF NOT EXISTS products(productid INTEGER, categoryid INTEGER, price INTEGER)")


insert_query_customers='''
INSERT INTO customers
    ("customerid", "country")
VALUES
    (1, 'Germany'),
    (2, 'Mexico'),
    (3, 'Mexico'),
    (4, 'UK'),
    (5, 'Sweden'),
    (6, 'Germany'),
    (7, 'France'),
    (8, 'Spain'),
    (9, 'France'),
    (10, 'Canada'),
    (11, 'UK'),
    (12, 'Argentina'),
    (13, 'Mexico'),
    (14, 'Switzerland'),
    (15, 'Brazil'),
    (16, 'UK'),
    (17, 'Germany'),
    (18, 'France'),
    (19, 'UK'),
    (20, 'Austria')
;
'''

insert_query_categories='''
INSERT INTO categories
    ("categoryid", "categoryname")
VALUES
    (1, 'Beverages'),
    (2, 'Condiments'),
    (3, 'Confections'),
    (4, 'Dairy Products'),
    (5, 'Grains/Cereals'),
    (6, 'Meat/Poultry'),
    (7, 'Produce'),
    (8, 'Seafood')
;
'''

insert_query_employees='''
INSERT INTO employees
    ("employeeid", "birthdate")
VALUES
    (1, '1968-12-08'),
    (2, '1952-02-19'),
    (3, '1963-08-30'),
    (4, '1958-09-19'),
    (5, '1955-03-04')
;
'''
insert_query_orderdetails='''
INSERT INTO orderdetails
    ("orderid", "productid", "quantity")
VALUES
    (10248, 11, 12),
    (10248, 42, 10),
    (10248, 72, 5),
    (10249, 14, 9),
    (10249, 51, 40),
    (10250, 41, 10),
    (10250, 51, 35),
    (10250, 65, 15),
    (10251, 22, 6),
    (10251, 57, 15),
    (10251, 65, 20),
    (10252, 20, 40),
    (10252, 33, 25),
    (10252, 60, 40),
    (10253, 31, 20),
    (10253, 39, 42),
    (10253, 49, 40),
    (10254, 24, 15),
    (10254, 55, 21),
    (10254, 74, 21),
    (10255, 2, 20),
    (10255, 16, 35),
    (10255, 36, 25),
    (10255, 59, 30),
    (10256, 53, 15),
    (10256, 77, 12),
    (10257, 27, 25),
    (10257, 39, 6),
    (10257, 77, 15),
    (10258, 2, 50),
    (10258, 5, 65),
    (10258, 32, 6),
    (10259, 21, 10),
    (10259, 37, 1),
    (10260, 41, 16),
    (10260, 57, 50),
    (10260, 62, 15),
    (10260, 70, 21),
    (10261, 21, 20),
    (10261, 35, 20),
    (10262, 5, 12),
    (10262, 7, 15),
    (10262, 56, 2),
    (10263, 16, 60),
    (10263, 24, 28),
    (10263, 30, 60),
    (10263, 74, 36),
    (10264, 2, 35),
    (10264, 41, 25),
    (10265, 17, 30),
    (10265, 70, 20),
    (10266, 12, 12),
    (10267, 40, 50),
    (10267, 59, 70),
    (10267, 76, 15),
    (10268, 29, 10),
    (10268, 72, 4),
    (10269, 33, 60),
    (10269, 72, 20),
    (10270, 36, 30),
    (10270, 43, 25),
    (10271, 33, 24),
    (10272, 20, 6),
    (10272, 31, 40),
    (10272, 72, 24),
    (10273, 10, 24),
    (10273, 31, 15),
    (10273, 33, 20),
    (10273, 40, 60),
    (10273, 76, 33),
    (10274, 71, 20),
    (10274, 72, 7),
    (10275, 24, 12),
    (10275, 59, 6),
    (10276, 10, 15),
    (10276, 13, 10),
    (10277, 28, 20),
    (10277, 62, 12),
    (10278, 44, 16),
    (10278, 59, 15),
    (10278, 63, 8),
    (10278, 73, 25),
    (10279, 17, 15),
    (10280, 24, 12),
    (10280, 55, 20),
    (10280, 75, 30),
    (10281, 19, 1),
    (10281, 24, 6),
    (10281, 35, 4),
    (10282, 30, 6),
    (10282, 57, 2),
    (10283, 15, 20),
    (10283, 19, 18),
    (10283, 60, 35),
    (10283, 72, 3),
    (10284, 27, 15),
    (10284, 44, 21),
    (10284, 60, 20),
    (10284, 67, 5)
;
'''

insert_query_orders='''
INSERT INTO orders
    ("orderid", "customerid")
VALUES
    (10248, 90),
    (10249, 81),
    (10250, 34),
    (10251, 84),
    (10252, 76),
    (10253, 34),
    (10254, 14),
    (10255, 68),
    (10256, 88),
    (10257, 35),
    (10258, 20),
    (10259, 13),
    (10260, 55),
    (10261, 61),
    (10262, 65),
    (10263, 20),
    (10264, 24),
    (10265, 7),
    (10266, 87)
;
'''

insert_query_products='''
INSERT INTO products
    ("productid", "categoryid", "price")
VALUES
    (1, 1, 18),
    (2, 1, 19),
    (3, 2, 10),
    (4, 2, 22),
    (5, 2, 21.35),
    (6, 2, 25),
    (7, 7, 30),
    (8, 2, 40),
    (9, 6, 97),
    (10, 8, 31),
    (11, 4, 21),
    (12, 4, 38),
    (13, 8, 6),
    (14, 7, 23.25),
    (15, 2, 15.5),
    (16, 3, 17.45),
    (17, 6, 39),
    (18, 8, 62.5),
    (19, 3, 9.2),
    (20, 3, 81),
    (21, 3, 10),
    (22, 5, 21),
    (23, 5, 9),
    (24, 1, 4.5),
    (25, 3, 14),
    (26, 3, 31.23),
    (27, 3, 43.9),
    (28, 7, 45.6),
    (29, 6, 123.79),
    (30, 8, 25.89),
    (31, 4, 12.5),
    (32, 4, 32),
    (33, 4, 2.5),
    (34, 1, 14),
    (35, 1, 18),
    (36, 8, 19),
    (37, 8, 26),
    (38, 1, 263.5),
    (39, 1, 18),
    (40, 8, 18.4),
    (41, 8, 9.65),
    (42, 5, 14),
    (43, 1, 46),
    (44, 2, 19.45),
    (45, 8, 9.5),
    (46, 8, 12),
    (47, 3, 9.5),
    (48, 3, 12.75),
    (49, 3, 20)
;
'''


cur.execute(insert_query_customers)
cur.execute(insert_query_categories)
cur.execute(insert_query_employees)
cur.execute(insert_query_orderdetails)
cur.execute(insert_query_orders)
cur.execute(insert_query_products)

# create SQL 3 tables and data
cur.execute("Drop TABLE IF EXISTS highschooler")
cur.execute("CREATE TABLE IF NOT EXISTS highschooler(id INTEGER, name TEXT, grade INTEGER)")

cur.execute("Drop TABLE IF EXISTS friend")
cur.execute("CREATE TABLE IF NOT EXISTS friend(id1 INTEGER, id2 INTEGER)")

cur.execute("Drop TABLE IF EXISTS likes")
cur.execute("CREATE TABLE IF NOT EXISTS likes(id1 INTEGER, id2 INTEGER)")

cur.execute("Drop TABLE IF EXISTS movie")
cur.execute("CREATE TABLE IF NOT EXISTS movie(mid INTEGER, title TEXT, year INTEGER, director TEXT)")

cur.execute("Drop TABLE IF EXISTS reviewer")
cur.execute("CREATE TABLE IF NOT EXISTS reviewer(rid INTERGER, name TEXT)")

cur.execute("Drop TABLE IF EXISTS rating")
cur.execute("CREATE TABLE IF NOT EXISTS rating(rid INTERGER, mid INTERGER, stars INTERGER, ratingdate BLOB)")

insert_query_highschooler='''
insert into highschooler("id","name","grade") 
values 
(1510, 'Jordan', 9),
(1689, 'Gabriel', 9),
(1381, 'Tiffany', 9),
(1709, 'Cassandra', 9),
(1101, 'Haley', 10),
(1782, 'Andrew', 10),
(1468, 'Kris', 10),
(1641, 'Brittany', 10),
(1247, 'Alexis', 11),
(1316, 'Austin', 11),
(1911, 'Gabriel', 11),
(1501, 'Jessica', 11),
(1304, 'Jordan', 12),
(1025, 'John', 12),
(1934, 'Kyle', 12),
(1661, 'Logan', 12);
'''
insert_query_friend='''
insert into friend("id1","id2") 
values 
(1510, 1381),
(1101, 1501),
(1510, 1689),
(1689, 1709),
(1381, 1247),
(1709, 1247),
(1689, 1782),
(1782, 1468),
(1782, 1316),
(1782, 1304),
(1468, 1101),
(1468, 1641),
(1101, 1641),
(1247, 1911),
(1247, 1501),
(1911, 1501),
(1501, 1934),
(1316, 1934),
(1934, 1304),
(1304, 1661),
(1661, 1025),
(1689, 1709),
(1709, 1689),
(1782, 1709),
(1911, 1247),
(1247, 1468),
(1641, 1468),
(1316, 1304),
(1501, 1934),
(1934, 1501),
(1025, 1101);
'''
insert_query_likes='''
insert into likes("id1","id2")
values 
(1689, 1709),
(1709, 1689),
(1782, 1709),
(1911, 1247),
(1247, 1468),
(1641, 1468),
(1316, 1304),
(1501, 1934),
(1934, 1501),
(1025, 1101),
(1101, 1381),
(1025, 1641);

'''
insert_query_movie='''
insert into movie("mid","title","year","director") 
values
(101, 'Gone with the Wind', 1939, 'Victor Fleming'),
(102, 'Star Wars', 1977, 'George Lucas'),
(103, 'The Sound of Music', 1965, 'Robert Wise'),
(104, 'E.T.', 1982, 'Steven Spielberg'),
(105, 'Titanic', 1997, 'James Cameron'),
(106, 'Snow White', 1937, null),
(107, 'Avatar', 2009, 'James Cameron'),
(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg');

'''
insert_query_reviewer='''
insert into reviewer("rid","name") 
values
(201, 'Sarah Martinez'),
(202, 'Daniel Lewis'),
(203, 'Brittany Harris'),
(204, 'Mike Anderson'),
(205, 'Chris Jackson'),
(206, 'Elizabeth Thomas'),
(207, 'James Cameron'),
(208, 'Ashley White');

'''
insert_query_rating='''
insert into rating("rid", "mid", "stars", "ratingdate")
values
(201, 101, 2, '2011-01-22'),
(201, 101, 4, '2011-01-27'),
(202, 106, 4, null),
(203, 103, 2, '2011-01-20'),
(203, 108, 4, '2011-01-12'),
(203, 108, 2, '2011-01-30'),
(204, 101, 3, '2011-01-09'),
(205, 103, 3, '2011-01-27'),
(205, 104, 2, '2011-01-22'),
(205, 108, 4, null),
(206, 107, 3, '2011-01-15'),
(206, 106, 5, '2011-01-19'),
(207, 107, 5, '2011-01-20'),
(208, 104, 3, '2011-01-02');
'''
cur.execute(insert_query_highschooler)
cur.execute(insert_query_friend)
cur.execute(insert_query_likes)
cur.execute(insert_query_movie)
cur.execute(insert_query_reviewer)
cur.execute(insert_query_rating)


conn.commit()

2. Movie-Rating Query Experience

电影评分关系: movie(mid,title,year,director),reviewer(rid,name),rating(rid,mid,stars,ratingdate)。一位评论者可多次评价同一影片,评分日期也可能缺失。

2.1 Find the titles of all movies directed by Steven spielberg

PYTHON · 仅供阅读
query = '''
SELECT DISTINCT title
FROM movie
WHERE director IN ('Steven Spielberg');
'''

# run script
pd.read_sql_query(query, conn)
原笔记输出 · 本次未执行
                     title
0                     E.T.
1  Raiders of the Lost Ark

2.2 Find all years of ratings for movies which received a rating of 4 or 5, and sort them in increasing order

PYTHON · 仅供阅读
# Sqlite不支持extract
# SELECT DISTINCT EXTRACT(YEAR FROM ratingdate)
query = '''
SELECT DISTINCT strftime('%Y', ratingdate) AS years
FROM rating
WHERE stars in (4,5) 
AND ratingdate IS NOT null
order by 1;
'''

# run script
pd.read_sql_query(query, conn)
原笔记输出 · 本次未执行
  years
0  2011

2.3 Find the titles of all movies that have no ratings

PYTHON · 仅供阅读
query = '''
SELECT DISTINCT a.title
FROM movie a 
LEFT JOIN rating b
ON a.mid=b.mid
WHERE b.mid IS NULL;
'''

# run script
pd.read_sql_query(query, conn)
原笔记输出 · 本次未执行
       title
0  Star Wars
1    Titanic

3. Social-Network Query Experience

社交关系: highschooler(id,name,grade),friend(id1,id2),likes(id1,id2)。喜欢通常有方向;友谊是否双向存储必须按实际数据约定,不能自动假设两条方向都齐全。

1. UNION and UNION ALL (纵向去重合并)

SQL · 仅供阅读
SELECT Country FROM Customers
UNION | UNION ALL
SELECT Country FROM Suppliers
ORDER BY 1;

整理参考资料