Difference between four types of SQL connections: left Outer Join, right Outer Join, full join, and inner join

Source: Internet
Author: User
? The connection conditions can be specified in the from or where clause. We recommend that you specify the connection conditions in the from clause. The where and having clauses can also contain search conditions to further filter the rows selected by the connection conditions.

Connections can be divided into the following types:

Internal Connection.(Typical join operations use comparison operators such as = or <> ). IncludingEqual connection and natural connection.

The internal join uses the comparison operator to match rows in two tables based on the values of the columns in each table. For example, retrieve all rows with the same student ID in the students and courses tables.

External Connection. The external connection can beLeft outer connection, right outer connection, or complete external connection.
When an external connection is specified in the from clause, it can be specified by one of the following sets of keywords:
Left join or left Outer Join.
The result set of the left Outer Join includes all rows in the left table specified in the left outer clause, not just the rows matched by the join column. If a row in the left table does not match a row in the right table, all selection list columns in the right table in the row of the associated result set are null.

Right join or right outer join.
The right outer connection is the reverse connection of the left outer connection. All rows in the right table are returned. If a row in the right table does not match a row in the left table, a null value is returned for the left table.

Full join or full outer join.
The Complete External Connection returns all rows in the left and right tables. If a row does not match a row in another table, the selection list column of the other table contains a null value. If there are matched rows between tables, the entire result set row contains the data value of the base table.

Cross join.Returns all rows in the left table. Each row in the left table is combined with all rows in the right table. Cross join is also called Cartesian product.

For example, the following inner connection searches the author of a publisher who lives in the same state and city:

Use pubs
Select a. au_fname, A. au_lname, P. pub_name
From authors as a inner join publishers as P
On a. City = P. City
And a. State = P. State
Order by A. au_lname ASC, A. au_fname ASC

Tables or views in the from clause can be specified in any order through internal connections or complete external connections. However, when you connect to a specified table or view from the left or right, the order of tables or views is very important. For more information about using Left or Right outer join to arrange tables, see using outer join.

Example:
Table a id name table B ID job parent_id
1 piece 3 1 23 1
2 Li Si 2 34 2
3 Wang Wu 3 34 4

A. ID is related to parent_id

?? Internal Connection
Select a. *, B. * from a inner join B on A. ID = B. parent_id

The result is
1 piece 3 1 23 1
2 Li Si 2 34 2

? Left join
Select a. *, B. * from a left join B on A. ID = B. parent_id

The result is
1 piece 3 1 23 1
2 Li Si 2 34 2
3 Wang Wu null

? Right join?
Select a. *, B. * from a right join B on A. ID = B. parent_id

The result is
1 piece 3 1 23 1
2 Li Si 2 34 2
Null 3 34 4

? Full connection?
Select a. *, B. * from a full join B on A. ID = B. parent_id

The result is
1 piece 3 1 23 1
2 Li Si 2 34 2
Null 3 34 4
3 Wang Wu null

??
SQL code
DECLARE @TA TABLE (IDA INT,VA VARCHAR(10)) DECLARE @TB TABLE (IDB INT,VB VARCHAR(10)) INSERT INTO @TA 
SELECT 1,'AA' UNION SELECT 2,'BC' UNION SELECT 3,'CCC' INSERT INTO @TB SELECT 1,'2'  UNION SELECT 3,'58' 
UNION SELECT 4,'67' 
-- Simple Writing of inner join
SELECT

From: http://blog.knowsky.com/190093.htm

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.