I am currently working on pset7 in cs50, and I thought I had selected the correct number of values specified, but my script is outputting 349 rows instead of the 176 the answer key has.
"In 13.sql, write a SQL query to list the names of all people who starred in a movie in which Kevin Bacon also starred. Your query should output a table with a single column for the name of each person. There may be multiple people named Kevin Bacon in the database. Be sure to only select the Kevin Bacon born in 1958. Kevin Bacon himself should not be included in the resulting list."
sqlite> .schema
CREATE TABLE movies (
id INTEGER,
title TEXT NOT NULL,
year NUMERIC,
PRIMARY KEY(id)
);
CREATE TABLE stars (
movie_id INTEGER NOT NULL,
person_id INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id),
FOREIGN KEY(person_id) REFERENCES people(id)
);
CREATE TABLE directors (
movie_id INTEGER NOT NULL,
person_id INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id),
FOREIGN KEY(person_id) REFERENCES people(id)
);
CREATE TABLE ratings (
movie_id INTEGER NOT NULL,
rating REAL NOT NULL,
votes INTEGER NOT NULL,
FOREIGN KEY(movie_id) REFERENCES movies(id)
);
CREATE TABLE people (
id INTEGER,
name TEXT NOT NULL,
birth NUMERIC,
PRIMARY KEY(id)
);
MY SCRIPT
SELECT DISTINCT name
FROM
people
INNER JOIN stars ON people.id = stars.person_id
INNER JOIN movies ON movies.id = stars.movie_id
WHERE movies.title IN (
SELECT
title
From
movies
INNER JOIN stars ON movies.id = stars.movie_id
INNER JOIN people ON stars.person_id= people.id
WHERE
people.name = "Kevin Bacon"
AND
people.birth = "1958"
)
EXCEPT SELECT name FROM people WHERE people.name = "Kevin Bacon"
Are there some logical errors in this script? My logic was:
- Select all movies Kevin Bacon stars in (nested SELECT)
- Select names of stars (main SELECT) who appear in any of these Kevin Bacon movies, except Kevin Bacon himself.