The difference between inner join, outer join and cross join in SQL

Table A is the table on the left, Table B is the table on the right

1. The result of INNER JOIN is the intersection of AB
SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.name

2. LEFT [OUTER] JOIN produces the complete set of table A, and there are values ​​in table B that match, and if there is no match, null values ​​are used instead of
SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name

3.RIGHT [OUTER] JOIN produces the complete set of table B, and there is a value in table A if there is a match, and if there is no match, it will be replaced with a null value
SELECT * FROM TableA RIGHT OUTER JOIN TableB ON TableA.name = TableB.name

4.FULL [OUTER] JOIN produces the union of A and B. For no matching records, null will be used as the value
SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name 
You can find no matching values ​​by is NULL:
SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name
WHERE TableA.id IS null OR TableB.id IS null 

5. CROSS JOIN performs an N*M combination of the data in table A and table B, that is, a Cartesian product. For example, 4*4=16 records will be generated in this example. We must filter the data during the development process, so this kind of
SELECT * FROM TableA CROSS JOIN TableB  is rarely used.

Reference address: https://www.cnblogs.com/wq3435/p/6677937.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324978927&siteId=291194637