SQL_Joins

Visual_SQL_Joins/INNER_JOIN.png

Inner Join

This query will return all of the records in the left table (table A) that have a matching record in the right table (table B). This Join is written as follows:

1
2
3
4
SELECT <select_list> 
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key

Left Join

This query will return all of the records in the left table (table A) regardless if any of those records have a match in the right table (table B). It will also return any matching records from the right table. This Join is written as follows:

1
2
3
4
SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key

Right Join

This query will return all of the records in the right table (table B) regardless if any of those records have a match in the left table (table A). It will also return any matching records from the left table. This Join is written as follows:

1
2
3
4
SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

Outer Join

This Join can also be referred to as a FULL OUTER JOIN or a FULL JOIN. This query will return all of the records from both tables, joining records from the left table (table A) that match records from the right table (table B). This Join is written as follows:

1
2
3
4
SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key

Left Join Excluding Inner Join

This query will return all of the records in the left table (table A) that do not match any records in the right table (table B). This Join is written as follows:

1
2
3
4
5
SELECT <select_list> 
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

Right Join Excluding Inner Join

This query will return all of the records in the right table (table B) that do not match any records in the left table (table A). This Join is written as follows:

1
2
3
4
5
SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL

Outer Join Excluding Inner Join

This query will return all of the records in the left table (table A) and all of the records in the right table (table B) that do not match. This Join is written as follows:

1
2
3
4
5
SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL OR B.Key IS NULL

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2020-2022 Henry
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信