Thursday, 28 November 2013

SQL Table Joins (Left, Right, Inner, Outer)

A quick explanation of a join
I am always being asked about SQL joins, so I thought I'd write a quick blog.

SQL Joins are used to combine data from two tables, based on a common field. The returned results (called a result-set) can be used to in the same way as the result-set from a single query.

There are four different SQL Joins you can use:
  • Inner: Returns the rows where the fields match in BOTH tables
  • Left:  Returns all the rows from the left table and the matched rows from the right table
  • Right: Returns all the rows from the right table and the matched rows from the left table
  • Outer: Returns all rows from both tables, this is also called a Full Join
Some Examples
I'm going to use the following tables to demonstrate the differences. Note that the last row of each table does not have a matching row in the other table.

EmployeeLocation
EmpIDEmpName
13Jason
8Alex
3Ram
17Babu
25Johnson
EmpIDEmpLoc
13San Jose
8Los Angeles
3Pune, India
17Chennai, India
39Bangalore, India


The following query

SELECT * 
FROM
    Employee Inner Join Location ON Employee.empID = Location.empID

Employee.EmpIDEmployee.EmpNameLocation.EmpIDLocation.EmpLoc
13Jason13San Jose
8Alex8Los Angeles
3Ram3Pune, India
17Babu17Chennai, India

As you can see, the only rows returned are where there is a match in both tables. Changing the query to use the Outer Join returns a different results-set.

SELECT * 
FROM
    Employee Full Join Location ON Employee.empID = Location.empID

Employee.EmpIDEmployee.EmpNameLocation.EmpIDLocation.EmpLoc
13Jason13San Jose
8Alex8Los Angeles
3Ram3Pune, India
17Babu17Chennai, India
25JohnsonNULLNULL
NULLNULL39Bangalore, India

This returns two extra rows in the results-set. These have Null values where no match has been found in the other table.

Left Joins and Right Joins are similar to Outer Joins, except that all rows are returned from only one table (the Left or Right determines which one)

SELECT * 
FROM
    Employee Left Join Location ON Employee.empID = Location.empID
Employee.EmpIDEmployee.EmpNameLocation.EmpIDLocation.EmpLoc
13Jason13San Jose
8Alex8Los Angeles
3Ram3Pune, India
17Babu17Chennai, India
25JohnsonNULLNULL

SELECT * 
FROM
    Employee Right Join Location ON Employee.empID = Location.empID

Employee.EmpIDEmployee.EmpNameLocation.EmpIDLocation.EmpLoc
13Jason13San Jose
8Alex8Los Angeles
3Ram3Pune, India
17Babu17Chennai, India
NULLNULL39Bangalore, India

The two results-sets above show all of the rows from one table and the matching rows from the other table. Null values are used where no match has been found.

So what determines the left and right table? It is simply the position in regards to the Join. This means you could rewrite the Left Join about like so

SELECT * 
FROM
    Location Right Join Employee ON Location.empID = Employee.empID

and you would get the same results-set

Employee.EmpIDEmployee.EmpNameLocation.EmpIDLocation.EmpLoc
13Jason13San Jose
8Alex8Los Angeles
3Ram3Pune, India
17Babu17Chennai, India
25JohnsonNULLNULL

No comments:

Post a Comment