⭐️ Star this repository! It really motivates me to make better explanations and produce more work!! ⭐️
This repository contains various SQL queries used for different data retrieval tasks. The queries are organized by their functionality and purpose, providing insights into how to extract and manipulate data from a database.
SELECT *
FROM crime_scene_reports
WHERE year=2023 AND month=7 AND day=28 AND street='Humphrey Street';
This query fetches all crime scene reports from a specific date and location. It helps in understanding the context and details of the crime scene.
- Filters data based on year, month, day, and street.
- Useful for gathering preliminary information about the crime.
SELECT *
FROM interviews
WHERE year=2023 AND month=7 AND day=28;
This query obtains all interviews conducted on a specific date, which can provide insights into witness statements and other critical information.
- Helps in gathering evidence related to transactions, parking, and calls.
- Useful for understanding witness accounts and potential leads.
SELECT *
FROM bakery_security_logs
WHERE year=2023 AND month=7 AND day=28 AND hour=10 AND minute>=15 AND activity='exit'
ORDER BY minute;
This query retrieves security logs for a specific time window, focusing on exit activities. It helps in analyzing movements and possible suspects.
- Filters logs based on date, time, and activity.
- Orders results by minute to track exact exit times.
SELECT *
FROM people
WHERE license_plate IN (
SELECT license_plate
FROM bakery_security_logs
WHERE year=2023 AND month=7 AND day=28 AND hour=10 AND minute>=15 AND activity='exit');
This query identifies individuals associated with specific license plates found in the security logs.
- Uses subqueries to filter people based on license plates from the logs.
- Helps in identifying potential suspects or witnesses.
SELECT caller
FROM phone_calls
WHERE year = 2023
AND month = 7
AND day = 28
AND duration <= 60
AND (
caller IN (
SELECT phone_number
FROM people
WHERE license_plate IN (
SELECT license_plate
FROM bakery_security_logs
WHERE year = 2023
AND month = 7
AND day = 28
AND hour = 10
AND minute >= 15
AND activity = 'exit'
)
)
);
This query retrieves phone numbers of callers who made brief calls on the day of the crime. It helps in identifying possible suspects.
- Filters calls based on duration and date.
- Uses subqueries to cross-reference phone numbers and license plates.
SELECT *
FROM people
WHERE phone_number IN (
SELECT phone_number
FROM phone_calls
WHERE phone_number IN (
SELECT phone_number
FROM people
WHERE license_plate IN (
SELECT license_plate
FROM bakery_security_logs
WHERE year = 2023
AND month = 7
AND day = 28
AND hour = 10
AND minute >= 15
AND activity = 'exit'
)
)
);
This query helps in identifying individuals who have phone numbers linked to the license plates from the security logs.
- Uses multiple subqueries to filter and cross-reference phone numbers.
- Aids in connecting people based on phone numbers and license plates.
SELECT title
FROM movies
WHERE year=2008;
This query retrieves the titles of movies released in a specific year, in this case, 2008.
- Filters movies based on the year of release.
- Provides a list of movie titles for the specified year.
SELECT DISTINCT people.name
FROM directors
JOIN people ON directors.person_id=people.id
WHERE movie_id IN (
SELECT id
FROM movies
JOIN ratings ON ratings.movie_id=movies.id
WHERE ratings.rating>=9.0
);
This query identifies the names of directors who have directed movies with high ratings.
- Uses DISTINCT to avoid duplicate names.
- Joins multiple tables to filter based on movie ratings.
SELECT movies.title
FROM stars
JOIN movies ON movies.id=stars.movie_id
JOIN people ON people.id=stars.person_id
JOIN ratings ON ratings.movie_id=stars.movie_id
WHERE people.name='Chadwick Boseman'
ORDER BY ratings.rating DESC
LIMIT 5;
This query retrieves the top 5 movies featuring Chadwick Boseman, based on their ratings.
- Joins tables to gather data on movies, stars, and ratings.
- Orders results by rating and limits the output to the top 5 movies.
SELECT movies.title
FROM stars
JOIN movies ON movies.id=stars.movie_id
JOIN people ON people.id=stars.person_id
WHERE stars.movie_id IN (
SELECT movie_id
FROM stars
JOIN people ON people.id=stars.person_id
WHERE people.name='Bradley Cooper'
)
AND stars.movie_id IN (
SELECT movie_id
FROM stars
JOIN people ON people.id=stars.person_id
WHERE people.name='Jennifer Lawrence'
);
This query finds movies that feature both Bradley Cooper and Jennifer Lawrence.
- Uses subqueries to filter movies featuring specific actors.
- Joins multiple tables to gather required information.
SELECT name
FROM people
WHERE id IN (
SELECT person_id
FROM stars
WHERE movie_id IN (
SELECT movie_id
FROM stars
WHERE person_id = (
SELECT id
FROM people
WHERE name = 'Kevin Bacon' AND birth = 1958
)
)
) AND name != 'Kevin Bacon';
This query identifies individuals who have acted in movies with Kevin Bacon, excluding Kevin Bacon himself.
- Uses multiple subqueries to filter and find co-stars.
- Excludes Kevin Bacon from the results.
SELECT birth
FROM people
WHERE name='Emma Stone';
This query retrieves the birth year of Emma Stone from the database.
- Filters the people table based on the name.
- Provides birth information for the specified individual.
SELECT title
FROM movies
WHERE year>=2018
ORDER BY title;
This query retrieves movie titles released in or after 2018 and orders them alphabetically.
- Filters movies based on the release year.
- Orders the results by title for easy browsing.
SELECT COUNT(movie_id)
FROM ratings
WHERE rating=10.0;
This query counts the number of movies that have received a perfect rating of 10.0.
- Uses COUNT to tally the number of qualifying movies.
- Filters ratings to find perfect scores.
SELECT title, year
FROM movies
WHERE title LIKE "%Harry Potter%"
ORDER BY year;
This query retrieves movie titles containing "Harry Potter" and orders them by the release year.
- Uses the LIKE operator to filter titles.
- Orders the results by year for chronological viewing.
SELECT name
FROM people
WHERE license_plate IN (
SELECT license_plate
FROM people
GROUP BY license_plate
HAVING COUNT(*) > 1
);
This query identifies people with license plates that appear in multiple records.
- Uses GROUP BY and HAVING to filter common license plates.
- Helps in finding individuals with multiple records.
SELECT title
FROM movies
JOIN ratings ON movies.id=ratings.movie_id
GROUP BY title
HAVING AVG(rating) > (
SELECT AVG(rating)
FROM ratings
);
This query retrieves movie titles with ratings above the average rating.
- Uses AVG to calculate the average rating and filter movies above this threshold.
- Joins movies and ratings tables for comprehensive data.
SELECT title, year
FROM movies
ORDER BY year DESC
LIMIT 10;
This query retrieves the titles of the most recent movies, up to a specified limit, ordered by release year.
- Orders results by year in descending order.
- Limits the output to the top 10 most recent movies.
This project was inspired by and developed as part of the CS50x course offered by Harvard University. CS50x is Harvard University's introduction to the intellectual enterprises of computer science and the art of programming for majors and non-majors alike, with or without prior programming experience.
Thank you to the CS50x team for providing such a comprehensive and engaging introduction to computer science. |