Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cs50 – pset7 – movies – 10.sql failed check50 test

I’m currently working on 10.sql which asked me to find all people who directed a movie that received a rating of at least 9.0

Here’s the schema:

CREATE TABLE movies (

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                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)
        );

Here’s the code I wrote:

SELECT DISTINCT name FROM people
JOIN directors ON directors.person_id = people.id
JOIN movies ON movies.id  = directors.person_id
JOIN ratings ON ratings.movie_id = movies.id
WHERE ratings.rating >= 9.0;

When I worked on sqlite3, it returned me a list of names. But it failed the check50 test with error message of ‘Query did not return results’. I couldn’t figure out why. Can anyone tell me what I did wrong? Many thanks

>Solution :

Why do you need to join on movies? Try:

SELECT
    name 
FROM
    people 
    JOIN directors ON people.id = directors.person_id 
    JOIN ratings ON directors.movie_id = ratings.movie_id 
WHERE
    ratings.rating >= 9.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading