目录

Harvard CS50 学习笔记(七)

摘要
Harvard CS50 学习笔记(七)。

7 SQL

7.1 Lecture

7.1.1 Introduction

7.1.2 Data Collection

7.1.3 Flat-File Databases

7.1.4 Reading from a CSV

7.1.5 Filtering Duplicates

7.1.6 Counting Titles

7.1.7 Sorting Data

7.1.8 Lambda Functions

/images/Harvard_CS50/Lecture_7/lambda.png
lambda

7.1.9 Cleaning Data

7.1.10 Regular Expressions

7.1.11 Searching for a Title

7.1.12 Relational Databases and SQL

7.1.13 Create, Read, Update, Delete

7.1.14 Importing CSVs

7.1.15 SELECT

7.1.16 WHERE

7.1.17 DELETE

7.1.18 UPDATE

7.1.19 Redundancies

7.1.20 Linking Tables

7.1.21 INSERT

7.1.22 Python with SQL

7.1.23 shows.db

7.1.24 SQL Data Types

7.1.25 Mapping a Database (Many-to-Many Relationships)

7.1.26 Indexes

7.1.27 JOIN

7.1.28 SQL Injection Attacks

7.1.29 Race Conditions

7.2 Shorts

/images/Harvard_CS50/Lecture_7/SQL/sql_1.png
sql_1
/images/Harvard_CS50/Lecture_7/SQL/sql_2.png
sql_2
/images/Harvard_CS50/Lecture_7/SQL/sql_3.png
sql_3
/images/Harvard_CS50/Lecture_7/SQL/sql_4.png
sql_4
/images/Harvard_CS50/Lecture_7/SQL/sql_5.png
sql_5
/images/Harvard_CS50/Lecture_7/SQL/sql_6.png
sql_6
/images/Harvard_CS50/Lecture_7/SQL/sql_7.png
sql_7
/images/Harvard_CS50/Lecture_7/SQL/sql_8.png
sql_8
/images/Harvard_CS50/Lecture_7/SQL/sql_9.png
sql_9
/images/Harvard_CS50/Lecture_7/SQL/sql_10.png
sql_10
/images/Harvard_CS50/Lecture_7/SQL/sql_11.png
sql_11
/images/Harvard_CS50/Lecture_7/SQL/sql_12.png
sql_12
/images/Harvard_CS50/Lecture_7/SQL/sql_13.png
sql_13
/images/Harvard_CS50/Lecture_7/SQL/sql_14.png
sql_14
/images/Harvard_CS50/Lecture_7/SQL/sql_15.png
sql_15
/images/Harvard_CS50/Lecture_7/SQL/sql_16.png
sql_16
/images/Harvard_CS50/Lecture_7/SQL/sql_17.png
sql_17
/images/Harvard_CS50/Lecture_7/SQL/sql_18.png
sql_18
/images/Harvard_CS50/Lecture_7/SQL/sql_19.png
sql_19
/images/Harvard_CS50/Lecture_7/SQL/sql_20.png
sql_20
/images/Harvard_CS50/Lecture_7/SQL/sql_21.png
sql_21
/images/Harvard_CS50/Lecture_7/SQL/sql_22.png
sql_22
/images/Harvard_CS50/Lecture_7/SQL/sql_23.png
sql_23
/images/Harvard_CS50/Lecture_7/SQL/sql_24.png
sql_24
/images/Harvard_CS50/Lecture_7/SQL/sql_25.png
sql_25
/images/Harvard_CS50/Lecture_7/SQL/sql_26.png
sql_26
/images/Harvard_CS50/Lecture_7/SQL/sql_27.png
sql_27
/images/Harvard_CS50/Lecture_7/SQL/sql_28.png
sql_28
/images/Harvard_CS50/Lecture_7/SQL/sql_29.png
sql_29
/images/Harvard_CS50/Lecture_7/SQL/sql_30.png
sql_30
/images/Harvard_CS50/Lecture_7/SQL/sql_31.png
sql_31
/images/Harvard_CS50/Lecture_7/SQL/sql_32.png
sql_32

7.3 Lab 7

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-- 1
select name from songs;
-- 2
select name from songs order by tempo;
-- 3
select name from songs order by duration_ms desc limit 5;
-- 4
select name from songs where danceability > 0.75 and energy > 0.75 and valence > 0.75;
-- 5
select sum(energy) / count(*) from songs;
-- 6
select songs.name from songs join artists on songs.artist_id = artists.id where artists.name = 'Post Malone';
-- 7
select SUM(songs.energy) / count(songs.id) from songs join artists on songs.artist_id = artists.id where artists.name = 'Drake';
-- 8
select name from songs where name like '%feat%';

7.4 Problem Set 7

7.4.1 Movies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- 1
select title from movies where year = 2008;
-- 2
select birth from people where name = 'Emma Stone';
-- 3
select title from movies where year >= 2018 order by title;
-- 4
select count(movie_id) from ratings where rating = 10.0;
-- 5
select title, year from movies where title like 'Harry Potter%' order by year;
-- 6
select sum(rating)/count(movie_id) from ratings join movies on ratings.movie_id = movies.id where movies.year = 2012;
-- 7
select movies.title, ratings.rating from ratings join movies on ratings.movie_id = movies.id where movies.year = 2010 order by ratings.rating desc, movies.title;
-- 8
select people.name from stars join movies on stars.movie_id = movies.id join people on stars.person_id = people.id where movies.title = 'Toy Story';
-- 9
select people.name from movies join stars on movies.id = stars.movie_id join people on stars.person_id = people.id where movies.year = 2004 order by people.birth;
-- 10
select distinct(people.name) from movies join ratings on movies.id = ratings.movie_id join directors on movies.id = directors.movie_id join people on directors.person_id = people.id where ratings.rating >= 9.0;
-- 11
select movies.title from movies join ratings on movies.id = ratings.movie_id join stars on movies.id = stars.movie_id join people on stars.person_id = people.id where people.name = 'Chadwick Boseman' order by ratings.rating desc limit 5;
-- 12
select movies.title from movies join stars on movies.id = stars.movie_id join people on stars.person_id = people.id where movies.id in (select stars.movie_id from stars join people on stars.person_id = people.id where people.name = 'Johnny Depp') and people.name = 'Helena Bonham Carter';
-- 13
select people.name from movies join stars on movies.id = stars.movie_id join people on stars.person_id = people.id where movies.title in (select movies.title from movies join stars on movies.id = stars.movie_id join people on stars.person_id = people.id where people.name = 'Kevin Bacon' and people.birth = 1958) and people.name != 'Kevin Bacon';

7.4.2 Fiftyville

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Keep a log of any SQL queries you execute as you solve the mystery.

-- query crime_scene_reports
select * from crime_scene_reports where year = 2020 and month = 7 and day = 28 and street = 'Chamberlin Street';

-- query interviews
select * from interviews where year = 2020 and month = 7 and day = 28;

-- cars that left courthouse within 10 minutes
select * from courthouse_security_logs where year = 2020 and month = 7 and day = 28 and hour = 10 and minute >= 15 and minute <= 25 and activity = 'exit';

-- people whose car left courthouse within 10 minutes
select * from people where license_plate in (select license_plate from courthouse_security_logs where year = 2020 and month = 7 and day = 28 and hour = 10 and minute >= 15 and minute <= 25 and activity = 'exit');

--people who withdrawed money in that dat at Fifer Street
select * from people join bank_accounts on people.id = bank_accounts.person_id where bank_accounts.account_number in (select account_number from atm_transactions where year = 2020 and month = 7 and day = 28 and atm_location = 'Fifer Street' and transaction_type = 'withdraw');

--people who made a phone call at that day and duration is less than a minute
select * from people where phone_number in (select caller from phone_calls where year = 2020 and month = 7 and day = 28 and duration < 60);

-- people who took the earliest flight out of Fiftyville tomorrow
select * from people where passport_number in (select passport_number from passengers where flight_id in (select id from flights where origin_airport_id = (select id from airports where city = 'Fiftyville') and year = 2020 and month = 7 and day = 29 order by hour limit 1));

-- people whose car left courthouse within 10 minutes and withdrawed money in that dat at Fifer Street and made a phone call at that day and duration is less than a minute and took the earliest flight out of Fiftyville tomorrow
select a.name from (select * from people where license_plate in (select license_plate from courthouse_security_logs where year = 2020 and month = 7 and day = 28 and hour = 10 and minute >= 15 and minute <= 25 and activity = 'exit')) as a join (select * from people join bank_accounts on people.id = bank_accounts.person_id where bank_accounts.account_number in (select account_number from atm_transactions where year = 2020 and month = 7 and day = 28 and atm_location = 'Fifer Street' and transaction_type = 'withdraw')) as b on a.id = b.id join (select * from people where phone_number in (select caller from phone_calls where year = 2020 and month = 7 and day = 28 and duration < 60)) as c on b.id = c.id join (select id from people where passport_number in (select passport_number from passengers where flight_id in (select id from flights where origin_airport_id = (select id from airports where city = 'Fiftyville') and year = 2020 and month = 7 and day = 29 order by hour limit 1))) as d on c.id = d.id;