We can join three or more tables in SQL using the JOIN
keyword. Check out our detailed Answer on How to join 3 or more tables in SQL.
Key takeaways:
LEFT JOIN
, andLEFT OUTER JOIN
gives the same results, returning all records from the left table and matched records from the right table.The only difference is the optional OUTER keyword; both use similar syntax.
Both joins have identical performance characteristics; no difference in execution time.
Before writing an SQL query, it becomes important for a developer or student to understand the differences between different types of joins. The use of LEFT JOIN
and LEFT OUTER JOIN
can be confusing, especially when comparing inner vs. outer joins. For example, this confusion can keep valuable data out of reach and make it difficult to perform tasks as effectively as you would like. In this Answer, we will understand the difference between LEFT JOIN
and LEFT OUTER JOIN
.
LEFT JOIN
vs. LEFT OUTER JOIN
In SQL, the LEFT JOIN
and LEFT OUTER JOIN
return all the records from the left table (Table 1) and those matched from the right table (Table 2). If none of the records are matched from the right table, only the records from the left table are returned and NULL
values for the right table. The term “OUTE’’ is optional; it specifies that you are performing a type of outer join. Thus, LEFT JOIN
is a shorthand for LEFT OUTER JOIN
; it specifies that you perform a type of outer join.
LEFT JOIN
SQLSELECT column_nameFROM table1LEFT JOIN table2 ON table1.column_name=table2.column_name;
Let’s see an example of LEFT JOIN
in the code snippet below:
/* Create table Student */CREATE TABLE Student(ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,AGE INT NOT NULL,PRIMARY KEY (ID));/* Insert values in table Student */INSERT INTO Student (ID, NAME, AGE)VALUES (1, 'Shubham', 22);INSERT INTO Student (ID, NAME, AGE)VALUES (2, 'Parth', 21);INSERT INTO Student (ID, NAME, AGE)VALUES (3, 'Pratik', 23);/* Create table Score */CREATE TABLE Score(ID INT NOT NULL,SCORE INT NOT NULL,StudentID INT NOT NULL,PRIMARY KEY (ID));/* Insert values in table Score */INSERT INTO Score (ID, SCORE, StudentID)VALUES (1, 90, 1);INSERT INTO Score (ID, SCORE, StudentID)VALUES (2, 96, 2);INSERT INTO Score (ID, SCORE, StudentID)VALUES (3, 98, 3);/* Execute LEFT JOIN */SELECT * FROM StudentLEFT JOIN Score ONStudent.ID = Score.StudentID;
Lines 2–7: We create a table, Student
.
Lines 10–17: We insert a few values in the Student
table.
Lines 20–25: We create a table, Score
.
Lines 28–35: We insert a few values in the table, Score
.
Lines 38–40: We execute the LEFT JOIN
.
LEFT OUTER JOIN
SQLSELECT column_nameFROM table1LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name;
LEFT
JOIN
and LEFT
OUTER
JOIN
are equivalent and have no difference—both return the same result. It’s just a difference of terminologies. For example, MySQL uses LEFT
JOIN
, whereas the SQL server uses LEFT OUTER JOIN
.
Let’s see an example of LEFT OUTER JOIN
in the code snippet below:
/* Create table Student */CREATE TABLE Student(ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,AGE INT NOT NULL,PRIMARY KEY (ID));/* Insert values in table Student */INSERT INTO Student (ID, NAME, AGE)VALUES (1, 'Shubham', 22);INSERT INTO Student (ID, NAME, AGE)VALUES (2, 'Parth', 21);INSERT INTO Student (ID, NAME, AGE)VALUES (3, 'Pratik', 23);/* Create table Score */CREATE TABLE Score(ID INT NOT NULL,SCORE INT NOT NULL,StudentID INT NOT NULL,PRIMARY KEY (ID));/* Insert values in table Score */INSERT INTO Score (ID, SCORE, StudentID)VALUES (1, 90, 1);INSERT INTO Score (ID, SCORE, StudentID)VALUES (2, 96, 2);INSERT INTO Score (ID, SCORE, StudentID)VALUES (3, 98, 3);/* Execute LEFT OUTER JOIN */SELECT * FROM StudentLEFT OUTER JOIN Score ONStudent.ID = Score.StudentID;
Lines 2–7: We create a table, Student
.
Lines 10–17: We insert a few values in the Student
table.
Lines 20–25: We create a table, Score
.
Lines 28–35: We insert a few values in the Score
table.
Lines 38–40: We execute the LEFT OUTER JOIN
.
Key Differences | LEFT JOIN | LEFT OUTER JOIN |
Matching records | Includes matching records from the right table. | Same as LEFT JOIN. |
Syntax |
|
|
Good to use | Ideal when clarity in the type of join is not needed. | Ideal when clarity in the type of join is needed. |
Performance | No difference in execution time. | No difference in execution time. |
When we compare the output of LEFT JOIN
and LEFT OUTER JOIN
, we can see that both return the same result and have no difference, except the terminology.
Haven’t found what you were looking for? Contact Us