Thursday, May 20, 2021

SQL Left Outer Join with Example

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2).

In a SQL Left Outer Join, we get following rows in our output. It gives the output of the matching row between both the tables. If no records match from the left table, it also shows those records with NULL values.

Syntax:

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

Example:

Lets take below two tables

Employee

EmpName

DeptID

Rafferty

31

Jones

33

Heisenberg

33

Robinson

34

Smith

34

Williams

null


Department

DeptID

DepName

31

Sales

33

Engineering

34

Clerical

35

Marketing

SQL Query:

SELECT e.EmpName, d.DepName  
    FROM Employees AS e   
        LEFT OUTER JOIN  Department AS d ON e.DepID == d.DepID; 
Output:

EmpName

DepName

Rafferty

Sales

Jones

Engineering

Heisenberg

Engineering

Robinson

Clerical

Smith

Clerical

Williams

null


Please visit related article below:


No comments:

Post a Comment