SQL left Outer Join, right Outer Join, full join, internal join

Source: Internet
Author: User

SQL left Outer Join, right Outer Join, full join, internal join
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 <> ). Including equal connections and natural connections.
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. External connections can be left external connections, right external connections, or complete external connections.
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, 'A' 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 a. Ida, A. Va, B. IDB, B. VB from @ ta a, @ TB B
Where a. Ida = B. IDB
-- Inner join
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a inner join @ TB B
On a. Ida = B. IDB
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a join @ TB B
On a. Ida = B. IDB
-- Left Outer Join
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a left join @ TB B
On a. Ida = B. IDB
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a left Outer Join @ TB B
On a. Ida = B. IDB
-- Outer right join
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a right join @ TB B
On a. Ida = B. IDB
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a right Outer Join @ TB B
On a. Ida = B. IDB
-- Complete Outer Join
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a full join @ TB B
On a. Ida = B. IDB
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a full outer join @ TB B
On a. Ida = B. IDB
-- Cross join
Select a. Ida, A. Va, B. IDB, B. VB from @ ta a cross join @ TB B
-- Auto join
Select a. Ida, A. Va, B. Ida, B. Va from @ ta a, @ ta B where a. Ida = B. Ida + 1
Query analyzer execution:
-- Create Table Table1, Table2:
Create Table Table1 (ID int, name varchar (10 ))
Create Table Table2 (ID int, score INT)
Insert into Table1 select 1, 'lil'
Insert into Table1 select 2, 'zhang'
Insert into Table1 select 4, 'wang'
Insert into Table2 select 1, 90
Insert into Table2 select 2,100
Insert into Table2 select 3, 70
Such as table
-------------------------------------------------
Table1 | Table2 |
-------------------------------------------------
Idname | idscore |
1lee | 190 |
2zhang | 2100 |
4wang | 370 |
-------------------------------------------------

Run the following commands in the query Analyzer:
1. External Connection
1. Concept: including left Outer Join, right Outer Join or complete external join
2. Left join: left join or left Outer Join
(1) 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 the selection list columns in the right table in the row of the associated result set are null ).
(2) SQL statements
Select * From Table1 left join Table2 on table1.id = table2.id
------------- Result -------------
Idnameidscore
------------------------------
1lee190
2zhang2100
4 wangnullnull
------------------------------
Note: all the clauses containing Table 1 return the corresponding fields of Table 2 based on the specified conditions. The non-conforming fields are displayed as null.
3. Right join: Right join or right Outer Join
(1) The right outer join is the reverse join of the left Outer Join. 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.
(2) SQL statements
Select * From Table1 right join Table2 on table1.id = table2.id
------------- Result -------------
Idnameidscore
------------------------------
1lee190
2zhang2100
Nullnull370
------------------------------
Note: all the clauses containing Table 2 return the corresponding fields of Table 1 Based on the specified conditions. The non-conforming fields are displayed as null.
4. Complete External join: Full join or full outer join
(1) The Complete External join 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.
(2) SQL statements
Select * From Table1 full join Table2 on table1.id = table2.id
------------- Result -------------
Idnameidscore
------------------------------
1lee190
2zhang2100
4 wangnullnull
Nullnull370
------------------------------
Note: returns the sum of left and right connections (see upper left and right connections)
2. Internal Connection
1. Concept: inner join is a join that uses a comparison operator to compare the values of the columns to be joined.
2. Inner join: Join or inner join
3. SQL statements
Select * From Table1 join Table2 on table1.id = table2.id
------------- Result -------------
Idnameidscore
------------------------------
1lee190
2zhang2100
------------------------------
Note: Only the Table1 and Table2 columns that meet the conditions are returned.
4. equivalent (same as the following execution)
A: select a. *, B. * From Table1 A, Table2 B where a. ID = B. ID
B: Select * From Table1 cross join Table2 where table1.id = table2.id (Note: cross join can only use where, not on)
Iii. Cross-join (complete)
1. Concept: A cross join without a where clause will generate the Cartesian product of the table involved in the join. The number of rows in the first table multiplied by the number of rows in the second table is equal to the size of the Cartesian result set. (Table1 and Table2 generate 3*3 = 9 records)
2. Cross join: cross join (without the condition where ...)
3. SQL statements
Select * From Table1 cross join Table2
------------- Result -------------
Idnameidscore
------------------------------
1lee190
2zhang190
4wang190
1lee2100
2zhang2100
4wang2100
1lee370
2zhang370
4wang370
------------------------------
Note: 3*3 = 9 records are returned, that is, Cartesian product.
4. equivalent (same as the following execution)
A: Select * From Table1, Table2

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.