SQL Server > Joins

Left join / Left outer join in SQL Server

How to perform left join (or left outer join) in SQL Server?


Left outer join (or left join – both are same) is used to join more than one tables where we want all records from the left table (the table name that appears first in the query) even if their matching records are not present in right table (table name that appears next). If match exists in the right table, those records are presented I the result otherwise NULL values are displayed against record of left table.

To demonstrate this example, we have two tables as shown below

PersonalDetails

Accounts

Now run following SQL Statement in the query window

SELECT pd.PersonalDetailsId, pd.FirstName, pd.LastName, pd.Age,
ac.NetSalary
FROM PersonalDetails pd LEFT JOIN Accounts ac ON pd.PersonalDetailsId = ac.PersonalDetailsId

Here, instead of JOIN, we have used LEFT JOIN keyword and the results clearly shows the difference. PersonalDetailsId value that doesn’t exists in the Accounts table, shows NULL value for the NetSalary but for other records, it shows the value from the Accounts table as their PersonalDetailsId exists in the PersonalDetials table.

 Views: 10564 | Post Order: 36



Write for us






Hosting Recommendations