codescracker


sql

SQL Left Join



« Previous Tutorial Next Tutorial »


SQL LEFT JOIN keyword is used to return all the rows from the left table (i.e., table1 here), with matching rows in the right table (i.e., table2 here). The result is NULL in the right side, in case when there is no match found. Here is the general form of the LEFT JOIN keyword in SQL:

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

Or,

SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

Note - In some databases, the LEFT JOIN is also called as LEFT OUTER JOIN.

SQL Demo Database

Let us assume that we have the following two table with named "Admissions" and "Students". The first one is Admissions table and the second one is Students table:

sql inner join table1

sql inner join table2

SQL LEFT JOIN Keyword Example

Here is an example of LEFT JOIN keyword in SQL. The following SQL statement will return all the Students present in both the table:

SELECT Students.Name, Admissions.StudentID
FROM Students
LEFT JOIN Admissions
ON Students.StudentID=Admissions.StudentID
ORDER BY Students.Name;

After querying the above SQL statements, you will get the following output:

sql left join

Note - The SQL LEFT JOIN keyword returns all the rows from the left table (i.e., Students), even if there are no matches found in the right table (i.e., Admissions).

Here, you are watching that the one column of a student named "Aman Singh" is not displaying i.e. the StudentID, since the StudentID of this student is 1, that is not matching with the other table but displayed here. Since the LEFT JOIN keyword displays all the matching rows in the right table (i.e., table2). The result is NULL in the right side when there is no match found.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test