SQL-JOINS usage instructions

JOIN is not unfamiliar to people who have been in contact with databases, and many people know all kinds of JOIN, and many people do not have a thorough understanding of this.
Suppose we have two tables, Table_A and Table_B. The data in these two tables are as follows:

TABLE_A
  PK Value
---- ----------
   1 FOX
   2 COP
   3 TAXI
   6 WASHINGTON
   7 DELL
   5 ARIZONA
   4 LINCOLN
  10 LUCENT

TABLE_B
  PK Value
---- ----------
   1 TROT
   2 CAR
   3 CAB
   6 MONUMENT
   7 PC
   8 MICROSOFT
   9 APPLE
  11 SCOTCH

Join syntax:

join_table:
    table_reference JOIN table_factor [join_condition]                                          //内连接
  | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference join_condition               //外连接
  | table_reference LEFT SEMI JOIN table_reference join_condition                               //左半连接
  | table_reference CROSS JOIN table_reference [join_condition] (as of Hive 0.10)

table_reference:
    table_factor                    //表
  | join_table                      //join语句

table_factor:
    tbl_name [alias]                //表名[别名]
  | table_subquery alias            //子查寻[别名]
  | ( table_references )            //带空号的table_reference

join_condition:
    ON expression                   //on开头的条件语句
1. Inner JOIN: (inner connection)

Insert picture description here
This is the simplest, easiest connection to understand, and the most common connection. This query will return all records in the left table (table A) that have matching records in the right table (table B). This connection is written as follows:

SELECT <select_list> 
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key
-- Inner JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
       B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
INNER JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
(5 row(s) affected)
2. Left JOIN: (Left JOIN)

Insert picture description here
This query will return all records in the left table (table A), regardless of whether these records match any records in the right table (table B). It will also return any matching records from the correct table. This connection is written as follows:

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
-- Left JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   4 LINCOLN    NULL       NULL
   5 ARIZONA    NULL       NULL
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
  10 LUCENT     NULL       NULL
(8 row(s) affected)
3. Left Excluding JOIN: (Left Excluding JOIN results)

This query will return all records in the left table (table A) that do not match any records in the right table (table B). This connection is written as follows:
Insert picture description here

SELECT <select_list> 
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL
-- Left Excluding JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK
WHERE B.PK IS NULL

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   4 LINCOLN    NULL       NULL
   5 ARIZONA    NULL       NULL
  10 LUCENT     NULL       NULL
(3 row(s) affected)
4. Right JOIN: (right join)

Insert picture description here
This query will return all the records in the right table (table B), regardless of whether any of these records match the records in the left table (table A). It will also return any matching records in the left table. This connection is written as follows:

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
-- Right JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11
(8 row(s) affected)
5. Right Excluding JOIN: (Right join excludes inner join result)

Insert picture description here
This query will return all records in the right table (table B) that do not match any records in the left table (table A). This connection is written as follows:

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL
-- Right Excluding JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11
(3 row(s) affected)
6, Outer JOIN: (outer join)

Insert picture description here
This connection can also be called a full outer connection or a full connection. This query will return all the records in the two tables, joining the records in the left table (table A) that match the records in the right table (table B). This connection is written as follows:

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
-- Outer JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11
   5 ARIZONA    NULL       NULL
   4 LINCOLN    NULL       NULL
  10 LUCENT     NULL       NULL
(11 row(s) affected)
7, Outer Excluding JOIN: (outer join excludes inner join result)

Insert picture description here
This query will return all records in the left table (table A) and all records that do not match in the right table (table B). I don't need to use this type of connection yet, but I use all other types of connections quite frequently. This connection is written as follows:

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
-- Outer Excluding JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL
OR B.PK IS NULL

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11
   5 ARIZONA    NULL       NULL
   4 LINCOLN    NULL       NULL
  10 LUCENT     NULL       NULL
(6 row(s) affected)

Note that on outer joins, the inner join record is returned first, then the right join record is returned, and the left join record is returned last (at least, my Microsoft SQL Server does this; of course, this does not require any ORDERBY statement). You can visit the Wikipedia article for more information (however, the entry is not graphical). I also created a cheat sheet that you can print out when needed. If you right-click the image below and select "Save Target As.", you will download the full size image.
SQL-JOINS usage instructions

Guess you like

Origin blog.51cto.com/15080921/2590554