Tutorials

SQL JOIN


SQL LEFT JOIN


In Sql, LEFT JOIN selects all the records from the left column (table 1) and all the records from the right table that satifies the given condition. 

If there is no match in the right table for the given condition then result will be NULL for those columns.

 

Diagrammatic Explanation

left join by tutorialsinhand

 

Syntax of left join


SELECT column1, column2,...,columnN 
FROM table1 
LEFT JOIN table2 
ON table1.column = table2.column;

We can select column from both the tables by specifying it as table1.column1, table1.column2,....., table2.column1, table2.column2 and so on in above query. See the example section for more clarity.

 

Example on left join

We will take two tables tih_Courses and Registration as shown below:

tih_Courses
Course_ID Course_Name Professor Mode_of_Training
001 .Net Mukesh Tiwari Online and Offline
002 Java Rajneesh Shukla Online and Offline
003 ANSI SQL Rajneesh Shukla Online
004 Basics of Computer Shweta  Online


Registration
Course_ID Course_Name Registered_Students Total_Strength
001 .Net 107 120
002 Java 118 130
009 Oracle 100 110
010 Ruby 80 90
Query-1:Select all the course name and registered student to the respective courses.
SELECT tih_Courses.Course_Name, Registration.Registered_Students 
FROM tih_Courses 
LEFT JOIN Registration 
ON tih_Courses.Course_ID = Registration.Course_ID;

On executing above query successfully, we get:

Result
tih_Courses.Course_ID tih_Courses.Course_Name Registration.Registered_Students
001 .Net 107
002 Java 118
003 ANSI SQL NULL
004 Basics of Computer NULL

All the rows from the table 1 (tih_Courses) is returned and from the table 2 (Registration) those matching condition is returned. 

For course id 003 and 004 we donot have any matching course id in table Registration so they will have NULL value.

 

 

 


Please Share this page
Views : 89
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!