-- Keep a log of any SQL queries you execute as you solve the mystery.

-- Get overview of available data (tables)
.tables

    -- airports              crime_scene_reports   people
    -- atm_transactions      flights               phone_calls
    -- bakery_security_logs  interviews
    -- bank_accounts         passengers


-- Get structure of crime_scene_reports
.schema crime_scene_reports

    -- id INTEGER,
    -- year INTEGER,
    -- month INTEGER,
    -- day INTEGER,
    -- street TEXT,
    -- description TEXT,
    -- PRIMARY KEY(id)

-- All you know is that the theft took place on July 28, 2023 and that it took place on Humphrey Street.
SELECT description FROM crime_scene_reports
WHERE year = 2023 AND month = 7 AND day = 28
AND street = 'Humphrey Street';

    -- Theft of the CS50 duck took place at 10:15am at the Humphrey Street bakery. Interviews were conducted today with three witnesses who were present at the time –
    -- each of their interview transcripts mentions the bakery.

    -- Littering took place at 16:36. No known witnesses.


-- Get structure of interviews
.schema interviews

    -- id INTEGER,
    -- name TEXT,
    -- year INTEGER,
    -- month INTEGER,
    -- day INTEGER,
    -- transcript TEXT,
    -- PRIMARY KEY(id)


-- Get transcripts from the same day with mentions to bakery
SELECT transcript FROM interviews
WHERE year = 2023 AND month = 7 AND day = 28
AND transcript LIKE '%bakery%';

    -- Sometime within ten minutes of the theft, I saw the thief get into a car in the bakery parking lot and drive away. If you have security footage from the bakery parking lot,
    -- you might want to look for cars that left the parking lot in that time frame.

    -- I don't know the thief's name, but it was someone I recognized. Earlier this morning, before I arrived at Emma's bakery, I was walking by the ATM on Leggett Street
    -- and saw the thief there withdrawing some money.

    -- As the thief was leaving the bakery, they called someone who talked to them for less than a minute. In the call, I heard the thief say that they were planning to take
    -- the earliest flight out of Fiftyville tomorrow. The thief then asked the person on the other end of the phone to purchase the flight ticket.


-- Get structure of bakery_security_logs
.schema bakery_security_logs

    -- id INTEGER,
    -- year INTEGER,
    -- month INTEGER,
    -- day INTEGER,
    -- hour INTEGER,
    -- minute INTEGER,
    -- activity TEXT,
    -- license_plate TEXT,
    -- PRIMARY KEY(id)

-- Get licence plates of leaving customers from within 10 minutes of theft (10:15am)
SELECT license_plate FROM bakery_security_logs
WHERE year = 2023 AND month = 7 AND day = 28 AND hour = 10 AND minute BETWEEN 5 AND 25
AND activity = 'exit';

    -- 5P2BI95
    -- 94KL13X
    -- 6P58WS2
    -- 4328GD8
    -- G412CB7
    -- L93JTIZ
    -- 322W7JE
    -- 0NTHK55


-- Get structure of atm_transactions
.schema atm_transactions

    -- id INTEGER,
    -- account_number INTEGER,
    -- year INTEGER,
    -- month INTEGER,
    -- day INTEGER,
    -- atm_location TEXT,
    -- transaction_type TEXT,
    -- amount INTEGER,
    -- PRIMARY KEY(id)

-- Get account numbers of withdrawals from Leggett Street ATM on the day of theft
SELECT account_number FROM atm_transactions
WHERE year = 2023 AND month = 7 AND day = 28
AND atm_location = 'Leggett Street' AND transaction_type = 'withdraw';

    -- 28500762
    -- 28296815
    -- 76054385
    -- 49610011
    -- 16153065
    -- 25506511
    -- 81061156
    -- 26013199


-- Get structure of phone_calls
.schema phone_calls

    -- id INTEGER,
    -- caller TEXT,
    -- receiver TEXT,
    -- year INTEGER,
    -- month INTEGER,
    -- day INTEGER,
    -- duration INTEGER,
    -- PRIMARY KEY(id)

-- Get caller and receiver of calls with duration under a minute
SELECT caller FROM phone_calls
WHERE year = 2023 AND month = 7 AND day = 28
AND duration < 60;

    -- Caller         | Receiver
    -- (130) 555-0289 | (996) 555-8899
    -- (499) 555-9472 | (892) 555-8872
    -- (367) 555-5533 | (375) 555-8161
    -- (499) 555-9472 | (717) 555-1342
    -- (286) 555-6063 | (676) 555-6554
    -- (770) 555-1861 | (725) 555-3243
    -- (031) 555-6622 | (910) 555-3251
    -- (826) 555-1652 | (066) 555-9701
    -- (338) 555-6650 | (704) 555-2131


-- Get structure of flights
.schema flights

    -- id INTEGER,
    -- origin_airport_id INTEGER,
    -- destination_airport_id INTEGER,
    -- year INTEGER,
    -- month INTEGER,
    -- day INTEGER,
    -- hour INTEGER,
    -- minute INTEGER,
    -- PRIMARY KEY(id),
    -- FOREIGN KEY(origin_airport_id) REFERENCES airports(id),
    -- FOREIGN KEY(destination_airport_id) REFERENCES airports(id)

-- Get structure of airports
.schema airports

    -- id INTEGER,
    -- abbreviation TEXT,
    -- full_name TEXT,
    -- city TEXT,
    -- PRIMARY KEY(id)

-- Get structure of passengers
.schema passengers

    -- flight_id INTEGER,
    -- passport_number INTEGER,
    -- seat TEXT,
    -- FOREIGN KEY(flight_id) REFERENCES flights(id)

-- Get passengers (passport_number) of earliest flight out of Fiftyville the next day
SELECT passport_number FROM passengers
WHERE flight_id IN (
    SELECT flights.id FROM flights
    JOIN airports ON flights.origin_airport_id = airports.id
    WHERE year = 2023 AND month = 7 AND day = 29
    AND city = 'Fiftyville'
    ORDER BY hour LIMIT 1
);

    -- 7214083635
    -- 1695452385
    -- 5773159633
    -- 1540955065
    -- 8294398571
    -- 1988161715
    -- 9878712108
    -- 8496433585

-- Get destination airport for that flight
SELECT city FROM airports
WHERE id IN (
    SELECT destination_airport_id FROM flights
    JOIN airports ON flights.origin_airport_id = airports.id
    WHERE year = 2023 AND month = 7 AND day = 29
    AND city = 'Fiftyville'
    ORDER BY hour LIMIT 1
);

    -- New York City


-- Get structure of people
.schema people

    -- id INTEGER,
    -- name TEXT,
    -- phone_number TEXT,
    -- passport_number INTEGER,
    -- license_plate TEXT,
    -- PRIMARY KEY(id)

-- Get structure of bank_accounts
.schema bank_accounts

    -- account_number INTEGER,
    -- person_id INTEGER,
    -- creation_year INTEGER,
    -- FOREIGN KEY(person_id) REFERENCES people(id)

-- Get the name of the thief and a phone number of accomplice
SELECT name, receiver FROM people
JOIN bank_accounts ON people.id = bank_accounts.person_id
JOIN phone_calls ON people.phone_number = phone_calls.caller
WHERE license_plate IN ( -- Licence plates of leaving customers from within 10 minutes of theft (10:15am)
    SELECT license_plate FROM bakery_security_logs
    WHERE year = 2023 AND month = 7 AND day = 28 AND hour = 10 AND minute BETWEEN 5 AND 25
    AND activity = 'exit'
)
AND passport_number IN ( -- Ppassport numbers of people on earliest flight out of Fiftyville the next day
    SELECT passport_number FROM passengers
    WHERE flight_id IN (
        SELECT flights.id FROM flights
        JOIN airports ON flights.origin_airport_id = airports.id
        WHERE year = 2023 AND month = 7 AND day = 29
        AND city = 'Fiftyville'
        ORDER BY hour LIMIT 1
)
AND phone_number IN ( -- Caller numbers with duration under a minute
    SELECT caller FROM phone_calls
    WHERE year = 2023 AND month = 7 AND day = 28
    AND duration < 60
)
AND account_number IN ( -- Account numbers of withdrawals from Leggett Street ATM on the day of theft
    SELECT account_number FROM atm_transactions
    WHERE year = 2023 AND month = 7 AND day = 28
    AND atm_location = 'Leggett Street' AND transaction_type = 'withdraw'
)
AND day = 28 AND duration < 60
);

    -- Bruce

-- Get the name of the accomplice
SELECT name FROM people
WHERE phone_number IN (
    SELECT receiver FROM people
    JOIN bank_accounts ON people.id = bank_accounts.person_id
    JOIN phone_calls ON people.phone_number = phone_calls.caller
    WHERE license_plate IN ( -- Licence plates of leaving customers from within 10 minutes of theft (10:15am)
        SELECT license_plate FROM bakery_security_logs
        WHERE year = 2023 AND month = 7 AND day = 28 AND hour = 10 AND minute BETWEEN 5 AND 25
        AND activity = 'exit'
    )
    AND passport_number IN ( -- Ppassport numbers of people on earliest flight out of Fiftyville the next day
        SELECT passport_number FROM passengers
        WHERE flight_id IN (
            SELECT flights.id FROM flights
            JOIN airports ON flights.origin_airport_id = airports.id
            WHERE year = 2023 AND month = 7 AND day = 29
            AND city = 'Fiftyville'
            ORDER BY hour LIMIT 1
    )
    AND phone_number IN ( -- Caller numbers with duration under a minute
        SELECT caller FROM phone_calls
        WHERE year = 2023 AND month = 7 AND day = 28
        AND duration < 60
    )
    AND account_number IN ( -- Account numbers of withdrawals from Leggett Street ATM on the day of theft
        SELECT account_number FROM atm_transactions
        WHERE year = 2023 AND month = 7 AND day = 28
        AND atm_location = 'Leggett Street' AND transaction_type = 'withdraw'
    )
    AND day = 28 AND duration < 60
    )
);

    -- Robin
