SQL Tutorial - Joins


'Joins' are used to get data from two or more tables which are joined on a certain relationship between specified columns present in the tables being queried.
  • Cross Join
  • Inner Join
  • Outer Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join

SQL Cross Join


The 'SQL Joins ' are cartesian products that join everything to everything ie. each row of table A is joined to each row of table B. So if table A has 5 rows of data and table B has 6 rows of data, then you get a cartesian product of 5x6= 30 rows of data. So you can see why this is a very dangerous join to run against large tables.

SQL 'Cross Join' Syntax


Select column-1,column-2,column-3,... column-n
from tableA
CROSS JOIN tableB

The Cross Join Example


The 'Employees' table contains their respective data as shown below.

EmployeeID EmployeeName DateOfBirth DesignationID DeptID City Salary
1 Richard Hughes 4/23/1945 12:00:00 AM 1 1 New Orleans 950.00
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 2 San Francisco 800.00
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 4 New York 700.00
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 4 Salt Lake City 600.00
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 3 New York 500.00
6 Jana Rae 9/23/1976 12:00:00 AM 8 2 Houston 400.00
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 6 Long Island 650.00
8 Colin Flooks 12/29/1988 12:00:00 AM 4 2 Salt Lake City 600.00
9 Anthony Frank 2/22/1988 12:00:00 AM 9 3 San Francisco 750.00
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 2 Salt Lake City 450.00
11 Jack Martin 8/25/1985 12:00:00 AM NULL NULL Austin 500.00


The 'Department' table contains the following columns and their respective data type as shown above.

DeptID DeptName
1 Finance
2 Development
3 Testing
4 Delivery
5 Human Resources
6 Administration

Note : In a Cross Join, the where clause is absent. If present, it then functions like a Inner Join which we will discuss in the following chapters. We will be using an alias name as there are two tables being used here and they both have a common column.

Select A.EmployeeID,A.EmployeeName,A.DateOfBirth,A.DesignationID,A.City,
A.Salary ,B.DeptID,B.DeptName
from Employees A
Cross join Department B

EmployeeID EmployeeName DateOfBirth DesignationID City Salary DeptID DeptName
1 Richard Hughes 4/23/1945 12:00:00 AM 1 New Orleans 950.00 1 Finance
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 San Francisco 800.00 1 Finance
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 New York 700.00 1 Finance
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 Salt Lake City 600.00 1 Finance
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 New York 500.00 1 Finance
6 Jana Rae 9/23/1976 12:00:00 AM 8 Houston 400.00 1 Finance
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 Long Island 650.00 1 Finance
8 Colin Flooks 12/29/1988 12:00:00 AM 4 Salt Lake City 600.00 1 Finance
9 Anthony Frank 2/22/1988 12:00:00 AM 9 San Francisco 750.00 1 Finance
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 Salt Lake City 450.00 1 Finance
11 Jack Martin 8/25/1985 12:00:00 AM NULL Austin 500.00 1 Finance
1 Richard Hughes 4/23/1945 12:00:00 AM 1 New Orleans 950.00 2 Development
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 San Francisco 800.00 2 Development
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 New York 700.00 2 Development
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 Salt Lake City 600.00 2 Development
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 New York 500.00 2 Development
6 Jana Rae 9/23/1976 12:00:00 AM 8 Houston 400.00 2 Development
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 Long Island 650.00 2 Development
8 Colin Flooks 12/29/1988 12:00:00 AM 4 Salt Lake City 600.00 2 Development
9 Anthony Frank 2/22/1988 12:00:00 AM 9 San Francisco 750.00 2 Development
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 Salt Lake City 450.00 2 Development
11 Jack Martin 8/25/1985 12:00:00 AM NULL Austin 500.00 2 Development
1 Richard Hughes 4/23/1945 12:00:00 AM 1 New Orleans 950.00 3 Testing
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 San Francisco 800.00 3 Testing
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 New York 700.00 3 Testing
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 Salt Lake City 600.00 3 Testing
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 New York 500.00 3 Testing
6 Jana Rae 9/23/1976 12:00:00 AM 8 Houston 400.00 3 Testing
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 Long Island 650.00 3 Testing
8 Colin Flooks 12/29/1988 12:00:00 AM 4 Salt Lake City 600.00 3 Testing
9 Anthony Frank 2/22/1988 12:00:00 AM 9 San Francisco 750.00 3 Testing
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 Salt Lake City 450.00 3 Testing
11 Jack Martin 8/25/1985 12:00:00 AM NULL Austin 500.00 3 Testing
1 Richard Hughes 4/23/1945 12:00:00 AM 1 New Orleans 950.00 4 Delivery
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 San Francisco 800.00 4 Delivery
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 New York 700.00 4 Delivery
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 Salt Lake City 600.00 4 Delivery
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 New York 500.00 4 Delivery
6 Jana Rae 9/23/1976 12:00:00 AM 8 Houston 400.00 4 Delivery
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 Long Island 650.00 4 Delivery
8 Colin Flooks 12/29/1988 12:00:00 AM 4 Salt Lake City 600.00 4 Delivery
9 Anthony Frank 2/22/1988 12:00:00 AM 9 San Francisco 750.00 4 Delivery
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 Salt Lake City 450.00 4 Delivery
11 Jack Martin 8/25/1985 12:00:00 AM NULL Austin 500.00 4 Delivery
1 Richard Hughes 4/23/1945 12:00:00 AM 1 New Orleans 950.00 5 Human Resources
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 San Francisco 800.00 5 Human Resources
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 New York 700.00 5 Human Resources
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 Salt Lake City 600.00 5 Human Resources
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 New York 500.00 5 Human Resources
6 Jana Rae 9/23/1976 12:00:00 AM 8 Houston 400.00 5 Human Resources
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 Long Island 650.00 5 Human Resources
8 Colin Flooks 12/29/1988 12:00:00 AM 4 Salt Lake City 600.00 5 Human Resources
9 Anthony Frank 2/22/1988 12:00:00 AM 9 San Francisco 750.00 5 Human Resources
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 Salt Lake City 450.00 5 Human Resources
11 Jack Martin 8/25/1985 12:00:00 AM NULL Austin 500.00 5 Human Resources
1 Richard Hughes 4/23/1945 12:00:00 AM 1 New Orleans 950.00 6 Administration
2 Taryn Sinclair 3/22/1980 12:00:00 AM 2 San Francisco 800.00 6 Administration
3 Ted Horowitz 3/31/1960 12:00:00 AM 3 New York 700.00 6 Administration
4 Jonathan Douglas 7/7/1971 12:00:00 AM 3 Salt Lake City 600.00 6 Administration
5 Miranda Leigh 7/19/1983 12:00:00 AM 7 New York 500.00 6 Administration
6 Jana Rae 9/23/1976 12:00:00 AM 8 Houston 400.00 6 Administration
7 Lita Rosanna 9/14/1982 12:00:00 AM 4 Long Island 650.00 6 Administration
8 Colin Flooks 12/29/1988 12:00:00 AM 4 Salt Lake City 600.00 6 Administration
9 Anthony Frank 2/22/1988 12:00:00 AM 9 San Francisco 750.00 6 Administration
10 Stephanie Lynn 7/30/1979 12:00:00 AM 8 Salt Lake City 450.00 6 Administration
11 Jack Martin 8/25/1985 12:00:00 AM NULL Austin 500.00 6 Administration


Note: Since there are 10 records in the Employees table and 6 records in the Department table, it combines to form 60 records in this case.