This repository has been archived by the owner on Oct 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.sql
99 lines (90 loc) · 3.84 KB
/
log.sql
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- Keep a log of any SQL queries you execute as you solve the mystery.
-- Explore tables in database
.table
-- Explore crime_scene_reports table
.schema crime_scene_reports
-- Find crime description of CS50 duck theft
SELECT * FROM crime_scene_reports
WHERE month = 7 AND day = 28
AND street = 'Humphrey Street';
-- Theft took place at 10:15am at the Humphrey Street bakery
-- Explore interviews
.schema interviews
-- Find transcripts of interviews of witnesses
SELECT * FROM interviews
WHERE month = 7 AND day = 28;
-- Witnesses:
-- Ruth - Thief left in car from Bakery parking lot within 10 minutes of theft. Look for cars in that time-frame
-- Eugene - Earlier that morning before Eugene went to Emma's bakery, thief withdrawed cash from ATM on Leggett Street
-- Raymond - As the thief was leaving the bakery, they spoke to accomplice over phone for less than a minute, asking to buy a ticket of the earliest flight out of Fiftyville the next day
-- Find destination city of the first flight out of Fiftyville on 29th of July(next day of theft)
SELECT city FROM airports
WHERE id = (
SELECT destination_airport_id FROM flights, airports
WHERE flights.origin_airport_id = airports.id
AND flights.month = 7 AND flights.day = 29
AND airports.city = 'Fiftyville'
ORDER BY flights.hour, flights.minute
LIMIT 1
);
-- Thief escaped to New York City
-- Find suspects who qualify for the following:
-- Withdrawed cash from an ATM on Leggett Street on 28th of July.
-- Departed from bakery within 10 minutes of the theft.
-- Called the accomplice after theft and spoke for less than a minute.
-- Boarded the first flight out of Fiftyville the next day.
SELECT * FROM people
-- suspect on the basis of ATM transaction
WHERE id IN (
SELECT person_id FROM bank_accounts, atm_transactions
WHERE bank_accounts.account_number = atm_transactions.account_number
AND atm_transactions.month = 7 AND atm_transactions.day = 28
AND atm_transactions.atm_location = "Leggett Street"
AND atm_transactions.transaction_type = 'withdraw'
)
-- suspect on the basis of boarding the first flight
AND passport_number IN (
SELECT passport_number FROM passengers
WHERE flight_id = (
SELECT flights.id FROM flights, airports
WHERE flights.origin_airport_id = airports.id
AND flights.month = 7 AND flights.day = 29
AND airports.city = 'Fiftyville'
ORDER BY flights.hour, flights.minute
LIMIT 1
)
)
-- suspect for departing from the bakery in a car within 10 minutes of theft
AND license_plate IN (
SELECT license_plate FROM bakery_security_logs
WHERE month = 7 AND day = 28
AND hour = 10
AND minute <= 25
AND minute >= 15
)
-- suspect for calling the accomplice
AND phone_number IN (
SELECT caller FROM phone_calls
WHERE month = 7 AND day = 28
AND duration < 60
);
-- Suspects:
-- +--------+--------+----------------+-----------------+---------------+
-- | id | name | phone_number | passport_number | license_plate |
-- +--------+--------+----------------+-----------------+---------------+
-- | 686048 | Bruce | (367) 555-5533 | 5773159633 | 94KL13X |
-- +--------+--------+----------------+-----------------+---------------+
-- So, the thief is Bruce.
-- Find the accomplice whom Bruce contacted after theft and asked to book the flight
SELECT * FROM people
WHERE phone_number = (
SELECT receiver FROM phone_calls
WHERE month = 7 AND day = 28
AND duration < 60 AND caller = '(367) 555-5533'
);
-- +--------+-------+----------------+-----------------+---------------+
-- | id | name | phone_number | passport_number | license_plate |
-- +--------+-------+----------------+-----------------+---------------+
-- | 864400 | Robin | (375) 555-8161 | NULL | 4V16VO0 |
-- +--------+-------+----------------+-----------------+---------------+
-- The accomplice is Robin.