Understanding Left Join in SQL Server with simple examples.

It returns all records from left side table and matching records from right side table.

Left Join Diagram


LEFT JOIN SYNTAX:

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

LEFT JOIN EXAMPLE 1:

Requirement: I need to display all records from Users and matching records from country

SELECT u.UserName,u.CountryName from users u left join country c on u.countryid=c.countryid;

LEFT JOIN EXAMPLE 2:

Requirement: I need to display all records from Customers and matching records from Orders

SELECT c.CustomerName,ord.OrderDate from Customers c LEFT JOIN Orders ord ON c.Customerid=ord.Customerid

LEFT JOIN EXAMPLE 3:

Requirement: I need to display all records from Students and matching records from Course

SELECT s.StudentName,c.CourseName from Students s INNER JOIN Course c ON s.Studentid = c.Studentid