Last active
September 21, 2020 06:27
-
-
Save janreggie/2db31d45b0e100acd3bb539950e3c14a to your computer and use it in GitHub Desktop.
My solution for the SQL Murder Mystery. Solve it for yourself at https://mystery.knightlab.com/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| WITH murder AS ( | |
| -- determine details of the murder | |
| SELECT * | |
| FROM crime_scene_report | |
| WHERE date = 20180115 | |
| AND TYPE = 'murder' | |
| AND city = 'SQL City' | |
| ), | |
| first_witness AS ( | |
| -- last house on Northwestern Dr belongs to Morty Schapiro | |
| SELECT * | |
| FROM person | |
| WHERE address_street_name = 'Northwestern Dr' | |
| AND address_number = ( | |
| SELECT | |
| MAX(address_number) | |
| FROM | |
| person) | |
| ), | |
| second_witness AS ( | |
| -- name similar to "Annabel" and on Franklin Ave is Annabel Miller | |
| SELECT * | |
| FROM person | |
| WHERE name LIKE "%Annabel%" | |
| AND address_street_name = 'Franklin Ave' | |
| ), | |
| witnesses AS ( | |
| -- combine first_witness and second_witness | |
| SELECT person.* | |
| FROM person, first_witness, second_witness | |
| WHERE person.id = first_witness.id | |
| OR person.id = second_witness.id | |
| ), | |
| witnesses_interview AS ( | |
| -- what did they say? details: | |
| -- from witness 1 | |
| -- - member of Get Fit Now | |
| -- - membership number of bag starts with 48Z and gold member | |
| -- - plate number like %H42W% | |
| -- from witness 2 | |
| -- - worked out on 9 Jan | |
| SELECT * | |
| FROM interview | |
| JOIN witnesses ON interview.person_id = witnesses.id | |
| ), | |
| with_48z AS ( | |
| -- from witness 1's report | |
| SELECT * | |
| FROM get_fit_now_member | |
| WHERE id LIKE '48Z%' | |
| AND membership_status = 'gold' | |
| ), | |
| checked_in_at_20180109 AS ( | |
| -- from witness 2's report | |
| SELECT * | |
| FROM get_fit_now_check_in | |
| WHERE check_in_date = 20180109 | |
| ), | |
| suspect_bataks AS ( | |
| -- from witness 1 and 2's reports | |
| SELECT * | |
| FROM person | |
| JOIN with_48z ON person.id = with_48z.person_id | |
| JOIN checked_in_at_20180109 ON with_48z.id = checked_in_at_20180109.membership_id | |
| ), | |
| suspect_drivers AS ( | |
| -- from witness 2's report | |
| SELECT * | |
| FROM person | |
| JOIN drivers_license ON person.license_id = drivers_license.id | |
| AND drivers_license.plate_number LIKE '%H42W%' | |
| ), | |
| suspect AS ( | |
| -- combining suspect_bataks and suspect_drivers leads us to one dude | |
| SELECT * | |
| FROM person | |
| JOIN suspect_bataks ON person.id = suspect_bataks.id | |
| JOIN suspect_drivers ON person.id = suspect_drivers.id | |
| ), | |
| suspect_interview AS ( | |
| -- now the plot thickens... | |
| -- I was hired by a woman with a lot of money. | |
| -- ... she's around 5'5" (65") or 5'7" (67"). | |
| -- She has red hair and she drives a Tesla Model S. | |
| -- I know that she attended the SQL Symphony Concert 3 times in December 2017. | |
| SELECT interview.*, person.* | |
| FROM interview | |
| JOIN suspect ON interview.person_id = suspect.id | |
| JOIN person ON interview.person_id = person.id | |
| ), | |
| employer_driver AS ( | |
| -- from suspect's description: red hair, Tesla Model S, bet. 65 and 67 | |
| SELECT * | |
| FROM drivers_license | |
| WHERE hair_color = 'red' | |
| AND car_make = 'Tesla' | |
| AND car_model = 'Model S' | |
| AND height >= 65 | |
| AND height <= 67 | |
| ), | |
| employer_symphony AS ( | |
| -- what are these people thinking using facebook_event_checkin?! | |
| -- anyhow, at 2017 december, of event name SQL Symphony Concert, | |
| -- and thrice (GROUP BY person_id HAVING count = 3) | |
| SELECT *, COUNT(*) AS count | |
| FROM facebook_event_checkin | |
| WHERE event_name = 'SQL Symphony Concert' | |
| AND date >= 20171201 | |
| AND date <= 20171231 | |
| GROUP BY person_id | |
| HAVING count = 3 | |
| ), | |
| employer AS ( | |
| SELECT person.* | |
| FROM person | |
| JOIN employer_driver ON person.license_id = employer_driver.id | |
| JOIN employer_symphony ON person.id = employer_symphony.person_id | |
| ) | |
| SELECT * | |
| FROM employer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment