Consulting

Results 1 to 2 of 2

Thread: Need helps with LEFT OUTER JOIN

  1. #1

    Question Need helps with LEFT OUTER JOIN

    I am trying to JOIN data from the same table but somehow the results is NOT correct. can you please take a look at my work and let me know what did I do wrong? Technically I just want to issue a SELECT that will show the title of each movie next to its sequel's title (or NULL if it doesn't have a sequel). But the result is NOT as what I wanted. Thanks so much:

    CREATE TABLE movies (id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT,
    released INTEGER,
    sequel_id INTEGER);


    INSERT INTO movies
    VALUES (1, "Harry Potter and the Philosopher's Stone", 2001, 2);
    INSERT INTO movies
    VALUES (2, "Harry Potter and the Chamber of Secrets", 2002, 3);
    INSERT INTO movies
    VALUES (3, "Harry Potter and the Prisoner of Azkaban", 2004, 4);
    INSERT INTO movies
    VALUES (4, "Harry Potter and the Goblet of Fire", 2005, 5);
    INSERT INTO movies
    VALUES (5, "Harry Potter and the Order of the Phoenix ", 2007, 6);
    INSERT INTO movies
    VALUES (6, "Harry Potter and the Half-Blood Prince", 2009, 7);
    INSERT INTO movies
    VALUES (7, "Harry Potter and the Deathly Hallows – Part 1", 2010, 8);
    INSERT INTO movies
    VALUES (8, "Harry Potter and the Deathly Hallows – Part 2", 2011, NULL);


    SELECT movies.title, numbers.title as sequel_title FROM movies
    LEFT OUTER JOIN movies numbers
    ON movies.id=numbers.sequel_id;

  2. #2
    try this...
    SELECT movies.title, numbers.title as sequel_title
    FROM movies

    LEFT OUTER JOIN numbers
    ON movies.sequel_id = numbers.sequel_id;

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •