SQL Left Join – The Complete Basics to Get You Started

Filed Under: SQL
SQL LEFT JOIN

Hey, folks! In this article, we will be focusing on SQL Left Join in detail. SQL Joins help us associate columns of different tables altogether in accordance with the predefined conditions.

So, let us begin!


Working of SQL Left Join

SQL Left Join associates and joins all the records of the left table with the particular records of the right table that happens to have a match with the data conditions.

LEFT JOIN
LEFT JOIN

That is, it compares the values of both the tables using certain relational operators in SQL, and then displays all the data values of the Left table and only those records of the right table that meets the condition.

Let us now focus on the structure of Left Join.


Syntax of Left Join

Have a look at the below syntax!

SELECT columns
FROM table_1
LEFT JOIN table_2
ON table_1.column-name = table_2.column-name;

As seen above, upon the execution of the above query, all the data records from the left table(table_1) will be displayed along with the selected records from the right table(table_2) that fulfills the condition mentioned.

SQL Left Join returns the following values upon execution–

  • All the records from Left table
  • Selected records from the right table which matches the left table records with the condition.
  • Returns NULL for the records of the right table which does not match the left table.

Examples of Left Join

In the below example, we have created two tables ‘Stud_Info’ and ‘Stud_score’ using SQL Create and Insert query as follows:

create table Stud_Info(stud_id integer, Name varchar(200), sub varchar(200));
 
insert into Stud_Info(stud_id, Name, sub) values(001, 'John','Maths');
 
insert into Stud_Info(stud_id, Name, sub) values(002, 'Mary','Science');
 
insert into Stud_Info(stud_id, Name, sub) values(003, 'Ray','Social Science');
 
insert into Stud_Info(stud_id, Name, sub) values(004, 'Jim','History');
 
insert into Stud_Info(stud_id, Name, sub) values(005, 'Rick','Geo');
 
create table Stud_score(stud_id integer, Score Integer);
insert into Stud_score(stud_id, score) values(001, 25);
insert into Stud_score(stud_id, score) values(002, 45);
insert into Stud_score(stud_id, score) values(005, 46);
insert into Stud_score(stud_id, score) values(004, 68);
insert into Stud_score(stud_id, score) values(007, 100);
Table 1-Stud_Info
Table 1-Stud_Info
Table 2-Stud_score
Table 2-Stud_score

Further, we have applied left join on both the table values.

SELECT Stud_Info.stud_id, Stud_Info.Name, Stud_score.score
FROM Stud_Info
LEFT JOIN Stud_score ON Stud_Info.stud_id = Stud_score.stud_id
ORDER BY Stud_Info.stud_id;

Output:

SQL LEFT JOIN
SQL LEFT JOIN

As a result, all the values of the left table are displayed and only the records that matches the condition from the right table gets displayed. A NULL value is displayed in place of records that do not match with the condition.


Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. Till then, Happy Learning!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages