PDA

View Full Version : Need helps with LEFT OUTER JOIN



tcambridge
02-29-2016, 06:06 PM
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;

luciano_n
06-08-2016, 07:51 AM
try this...
SELECT movies.title, numbers.title as sequel_title
FROM movies
LEFT OUTER JOIN numbers
ON movies.sequel_id = numbers.sequel_id;