1344 words
7 minutes
CS50 Problem Set 7
CS50 Problem Set 7
Here’s my answer for the CS50 Problem Set 7. Hope that will help you a bit.
Problem 1: Songs
SELECT name FROM songs;SELECT nameFROM songsORDER BY tempo;SELECT nameFROM songsORDER BY duration_ms DESCLIMIT 5;SELECT nameFROM songsWHERE danceability > 0.75 AND energy > 0.75 AND valence > 0.75;SELECT AVG(energy)FROM songs;SELECT nameFROM songsWHERE artist_id = ( SELECT id FROM artists WHERE name = 'Post Malone');SELECT AVG(energy)FROM songsWHERE artist_id = ( SELECT id FROM artists WHERE name = 'Drake');SELECT nameFROM songsWHERE name LIKE '%feat.%';1. I will use the SQL to count the average energy, valence, and danceability of a person’s top 100 songs from the past year.
2. I should consider how many times they listen to each music.Problem 2: Movies
-- 1. Titles of all movies from 2008SELECT titleFROM moviesWHERE year = 2008;-- 2. Birth year of Emma StoneSELECT birthFROM peopleWHERE name = 'Emma Stone';-- 3. Titles of all movies since 2018, in alphabetical orderSELECT titleFROM moviesWHERE year >= 2018ORDER BY title ASC;-- 4. Number of movies with a 10.0 ratingSELECT COUNT(rating)FROM ratingsWHERE rating = 10.0-- 5. Titles and years of all Harry Potter movies, in chronological order (title beginning with "Harry Potter and the ...")SELECT title, yearFROM moviesWHERE title LIKE "Harry Potter and the%"ORDER BY year;-- 6. Average rating of movies in 2012SELECT AVG(rating)FROM ratingsWHERE movie_id IN ( SELECT id FROM movies WHERE year = 2012)-- 7. All movies and ratings from 2010, in decreasing order by rating (alphabetical for those with same rating)SELECT movies.title, ratings.ratingFROM moviesJOIN ratingsON movies.id = ratings.movie_id AND movies.year = 2010 AND ratings.rating > 0ORDER BY ratings.rating DESC, title ASC;-- 8. Names of people who starred in Toy StorySELECT nameFROM peopleWHERE id IN( SELECT person_id FROM stars WHERE movie_id = ( SELECT id FROM movies WHERE title = 'Toy Story' ));-- 9. Names of all people who starred in a movie released in 2004, ordered by birth yearSELECT id, nameFROM peopleWHERE id IN( SELECT person_id FROM stars WHERE movie_id IN( SELECT id FROM movies WHERE year = 2004 ))ORDER BY birth;-- 10. Names of all directors who have directed a movie that got a rating of at least 9.0SELECT nameFROM peopleWHERE id IN ( SELECT person_id FROM directors WHERE movie_id IN ( SELECT movie_id FROM ratings WHERE rating >= 9.0 ));-- 11. Titles of the five highest rated movies (in order) that Chadwick Boseman starred in, starting with the highest ratedSELECT titleFROM moviesJOIN ratings ON ratings.movie_id = movies.idWHERE id IN ( SELECT movie_id FROM stars WHERE person_id = ( SELECT id FROM people WHERE name = 'Chadwick Boseman' ))ORDER BY ratings.rating DESCLIMIT 5;-- 12. Titles of all of movies in which both Jennifer Lawrence and Bradley Cooper starredSELECT titleFROM moviesWHERE ( id IN ( SELECT movie_id FROM stars WHERE person_id IN ( SELECT id FROM people WHERE name = 'Bradley Cooper' ) ))AND ( id IN ( SELECT movie_id FROM stars WHERE person_id IN ( SELECT id FROM people WHERE name = 'Jennifer Lawrence' ) ));-- 13. Names of all people who starred in a movie in which Kevin Bacon also starredSELECT nameFROM peopleJOIN stars ON people.id = stars.person_idJOIN movies ON movies.id = stars.movie_idWHERE movies.id IN ( SELECT movies.id 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';Problem 3: Fiftyville
The THIEF is: BruceThe city the thief ESCAPED TO: New York CityThe ACCOMPLICE is: Robin-- Keep a log of any SQL queries you execute as you solve the mystery.-- Get the description of the crime scene reports on that day and the streetSELECT description FROM crime_scene_reports WHERE year = 2025 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 the transcript from the reportersSELECT transcript FROM interviews WHERE year = 2025 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.
SELECT bakery_security_logs.activity, bakery_security_logs.license_plate, people.name FROM bakery_security_logsJOIN people ON bakery_security_logs.license_plate = people.license_plateWHERE bakery_security_logs.year = 2025AND bakery_security_logs.month = 7AND bakery_security_logs.day = 28AND bakery_security_logs.hour = 10AND bakery_security_logs.minute >= 15AND bakery_security_logs.minute <= 25;/*+----------+---------------+---------+| activity | license_plate | name |+----------+---------------+---------+| exit | 5P2BI95 | Vanessa || exit | 94KL13X | Bruce | *| exit | 6P58WS2 | Barry || exit | 4328GD8 | Luca || exit | G412CB7 | Sofia || exit | L93JTIZ | Iman || exit | 322W7JE | Diana | *| exit | 0NTHK55 | Kelsey |+----------+---------------+---------+*/
SELECT people.name FROM peopleJOIN bank_accounts ON bank_accounts.person_id = people.idJOIN atm_transactions ON atm_transactions.account_number = bank_accounts.account_numberWHERE atm_transactions.year = 2025AND atm_transactions.month = 7AND atm_transactions.day = 28AND atm_transactions.atm_location = 'Leggett Street'AND atm_transactions.transaction_type = 'withdraw';/*+---------+| name |+---------+| Bruce | *| Diana | *| Brooke || Kenny || Iman || Luca || Taylor || Benista |+---------+*/
UPDATE phone_callsSET receiver = people.nameFROM peopleWHERE phone_calls.receiver = people.phone_number;
UPDATE phone_callsSET caller = people.nameFROM peopleWHERE phone_calls.caller = people.phone_number;
SELECT caller, receiver FROM phone_callsWHERE year = 2025AND month = 7AND day = 28AND duration < 60;/*+---------+------------+| caller | receiver |+---------+------------+| Sofia | Jack || Kelsey | Larry || Bruce | Robin | *| Kelsey | Melissa || Taylor | James || Diana | Philip | *| Carina | Jacqueline || Kenny | Doris || Benista | Anna |+---------+------------+*/
SELECT id, origin_airport_id, destination_airport_id FROM flightsWHERE year = 2025AND month = 7AND day = 29AND origin_airport_id = ( SELECT id FROM airports WHERE city = 'Fiftyville')ORDER BY flights.hour ASC;/*+----+-------------------+------------------------+| id | origin_airport_id | destination_airport_id |+----+-------------------+------------------------+| 36 | 8 | 4 | *| 43 | 8 | 1 || 23 | 8 | 11 || 53 | 8 | 9 || 18 | 8 | 6 |+----+-------------------+------------------------+*/
SELECT city FROM airportsWHERE id = 4;/*+---------------+| city |+---------------+| New York City |+---------------+*/
SELECT name FROM peopleWHERE people.passport_number IN ( SELECT passengers.passport_number from passengers WHERE flight_id = 36);/*+--------+| name |+--------+| Kenny || Sofia || Taylor || Luca || Kelsey || Edward || Bruce | *| Doris |+--------+*/ CS50 Problem Set 7
https://tech.kinghua0629.com/posts/cs50-problem-set-7/