Union of SQL statements, intersection join (internal join, external join), and cross

Source: Internet
Author: User

Union of SQL statements, join of intersection (inner join, Outer Join), cross join Cartesian Product, difference set (not in)

1.
A. 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 set not in
Select * From Table1 where name not in (Select name from table2)

D. Cartesian Product
Select * From Table1 cross join Table2
And
Select * From Table1, Table2 is the same

2. Union in SQL
The difference between Union and Union all is that the former removes duplicate entries and the latter retains them.

A. Union
SQL statement1
Union
SQL statement2

B. Union all
SQL statement1
Union all
SQL statement2

3. Various joins in SQL
SQL connections can be divided into internal connections, external connections, and cross connections.

(That is, Cartesian Product)

A. Cross join
Without the WHERE clause, it returns the Cartesian product of the two joined tables, and the number of rows returned is equal to the product of the number of rows of the two tables;

Example
Select * From Table1 cross join Table2
Equivalent
Select * From Table1, Table2

It is generally not recommended to use this method, because if there is a where clause, the data table will always be the data table of the rows of the two tables multiplied by the number of rows before selection based on the where condition.
Therefore, if two tables that require communication are too large, they will be very slow and are not recommended.

B. Inner join
If you only use
Select * From Table1 inner join Table2
If no connection condition is specified, the result is the same as that of the cross join operation.

However, when using inner join, you must specify the connection conditions.
-- Equijoin (= applies to connection conditions, and duplicate columns are not removed)
Select * From Table1 as a inner join Table2 as B on A. Column = B. Column
-- Unequal connection (>,>=,<,<=,!> ,! <, <>)
For example
Select * From Table1 as a inner join Table2 as B on A. Column <> B. Column
-- Natural join (repeated columns are removed)

C. Outer Join
First, the difference between the internal connection and the external connection is as follows:
If no join condition is specified for an inner join, the result of the cross join is the same as that of the Cartesian product. However, unlike the Cartesian product, the data table is not as complex as the Cartesian product, the internal join efficiency is higher than that of cartesian products.

Only entries that meet the connection conditions are returned.
The outer join is different. The returned results include not only the rows that meet the connection conditions, but also the left table (when the left Outer Join is performed) and the right table (when the right join is performed) or all data rows connected by both sides (when all external connections.

1) left Outer Join left [outer] Join
the data rows that meet the conditions are displayed, and the data rows that do not meet the conditions in the left data table are displayed, no corresponding entries on the right show null
for example
select * From Table1 as a left [outer] join on. column = B. column
2) Right outer join right [outer] Join
displays data rows that meet the conditions and data rows that do not meet the conditions in the right data table, no corresponding entries on the left show null
for example
select * From Table1 as a right [outer] join on. column = B. column
3) All external connections
display the qualified data rows, and the left and right data rows that do not meet the conditions. The corresponding left and right data rows are displayed as null: http://blog.china.alibaba.com/blog/congmindu815/article/b0-i3570696.htmlwww.buyansco.comwww.paipai1688.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.