Inner join outer join cross join Cartesian product

First, divide it into three types: inner join, outer join, and cross join 
 
. Inner join (INNER JOIN): 
    divided into three types: equal join, natural join, and unequal join 
     
. OUTER JOIN: 
    divided into three Types: 
    LEFT OUTER JOIN or LEFT JOIN, 
    right outer join (RIGHT OUTER JOIN or RIGHT JOIN), 
    full outer join (FULL OUTER JOIN or FULL JOIN), 
 
cross join (CROSS JOIN): 
    no WHERE clause, it returns the join Cartesian product of all data rows in the table

=========================================== ===================================================== =======

============================================= ===================================================== =====

1.
a. Union UNION: SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2
b. Intersection JOIN: SELECT * FROM table1 AS a JOIN table2 b ON a.name=b.name
c. Difference NOT IN: SELECT * FROM table1 WHERE name NOT IN(SELECT name FROM table2)
d. Cartesian product CROSS JOIN: SELECT * FROM table1 CROSS JOIN table2 (same as SELECT * FROM table1, table2)
2.
In SQL The difference between UNION and UNION ALL is that the former will remove duplicate entries, while the latter will remain.
a. UNION: SQL Statement1 UNION SQL Statement2
b. UNION ALL: SQL Statement1 UNION ALL SQL Statement2
3.
Various JOINs in SQL, connections in SQL can be divided into inner joins, outer joins, and cross joins (that is, Descartes Product)
a. Cross join CROSS JOIN:
If there is no WHERE conditional clause, it will return the Cartesian product of the two tables to be joined, and the number of rows returned is equal to the product of the number of rows in the two tables; for example
SELECT * FROM table1 CROSS JOIN table2 is equivalent to
SELECT * FROM table1, table2
is generally not recommended to use this method, because if there is a WHERE clause, the data table of the rows of the product of the two table rows is often generated first and then selected according to the WHERE condition. Therefore, if the two tables that need to communicate are too large, it will be very, very slow, and it is not recommended to use it.
b. Inner join INNER JOIN :
If you just use SELECT * FROM table1 INNER JOIN table2 without specifying the join condition, the result is the same as the cross join. But normally, using INNER JOIN requires specifying join conditions.
-- Equijoin (= sign is applied to join conditions, will not remove duplicate columns) SELECT * FROM table1 AS a INNER JOIN table2 AS b on a.column=b.column
-- unequal join (>,>=, <,<=,!>,!<,<>) For example SELECT * FROM table1 AS a INNER JOIN table2 AS b on a.column<>b.column
-- natural join (removes duplicate columns)
1), natural Join (Naturaljoin) is a special kind of equijoin, which requires that the components to be compared in the two relations must be the same attribute group, and the duplicate attribute columns are removed from the result. The equijoin does not remove duplicate attribute columns.
2), natural join: use the equal (=) operator in the join condition to compare the column values ​​of the joined columns, but it uses the select list to indicate the columns included in the query result set, and delete the duplicate columns in the join table.
3), natural join and equi-join
In the join operation, one of the most commonly used join is natural join.
The so-called natural join is that in the case of equi-join, when the join attributes X and Y have the same attribute group, the repeated attribute columns in the join result are removed.
Natural connection is to select tuples with the same name attribute in the generalized Cartesian product R×S that meet the equality conditions, and then perform projection to remove the repeated attributes of the same name to form a new relationship.
4) The difference between equi-join and natural join:
4.1) Equijoin does not require the attribute names of equal attribute values ​​to be the same, while the natural join requires that the attribute names of equal attribute values ​​must be the same, that is, the two relations can be naturally joined only if the attributes of the same name are used.
4.2) Equi join does not remove duplicate attributes, but natural join removes duplicate attributes. It can also be said that natural join is an equi join that removes duplicate columns.
c. Outer join OUTER JOIN:
First of all, the difference between inner join and outer join: If the inner join does not specify the join condition, the result of the cross join is the same as the Cartesian product, but different from the Cartesian product, there is no Cartesian product. If the product is so complicated, the data table of the product of the number of rows must be generated first, and the efficiency of the inner join is higher than that of the cross join of the Cartesian product. An inner join with a specified condition returns only entries that match the join condition. The outer join is different. The returned result not only contains the rows that meet the join conditions, but also includes all the data rows of the left table (when left outer join), the right table (when right join) or the connection on both sides (when full outer join). 
1) Left Outer join LEFT [OUTER] JOIN :
Display the data rows that meet the conditions, and display the data rows that do not meet the conditions in the left data table, and display NULL without corresponding entries on the right, such as SELECT * FROM table1 AS a LEFT [OUTER] JOIN ON a.column =b.column                                                                                                                                    
2) Right outer join RIGHT [OUTER] JOIN:
Display the data rows that meet the conditions, and display the data rows that do not meet the conditions in the data table on the right, and display NULL when there is no corresponding entry on the left. For example, SELECT * FROM table1 AS a RIGHT [OUTER] JOIN ON a.column=b.column                                                                                                                                              
3) All outside Connection:
Display the data rows that meet the conditions, and display the left and right data rows that do not meet the conditions at the same time, and display NULL on the corresponding left and right sides

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326775635&siteId=291194637