Paste) SQL left Outer Join, right Outer Join, full join, inner join

Source: Internet
Author: User

Http://www.blogjava.net/zolly/archive/2007/10/23/SQLJION.html

 

The join condition can be specified in the from or where clause. We recommend that you specify the join condition in the from clause. The where and having clauses can also contain search conditions to further filter the rows selected by the join conditions.

Connections can be divided into the following types:

Inner join (a typical join operation that uses comparison operators such as = or <> ). Including equal join and natural join.
The inner 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.

Outer Join. Outer Join can be left Outer Join, right outer join, or complete external join.
When an external join 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 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.

Full join or full outer join.
The Complete External Join Operation 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 join search is 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 join or complete external join. However, when you use left or right outer join to specify a table or view, 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

 

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.