SQL Four connection: internal connection, left outer connection, right outer connection, full connection--reprint

Source: Internet
Author: User
Tags joins null null

Original: http://zwdsmileface.iteye.com/blog/2191730

Personal understanding
INNER JOIN (INNER join) (a typical join operation, using a comparison operator like = or <>). Includes both equal and natural connections. Inner joins use comparison operators to match rows in two tables based on the values of the columns that are common to each table
Left join (left JOIN or left OUTER join) is the data in the table on the right-hand side is the datum, if the left table has data in the right table, otherwise the data in the right table showing the data in the table is empty
The right join is based on the data in the right table, and if the right table has data on the left table without data, the data in the left table of the data in the right table is displayed as empty.
Full joins (full join or fully OUTER join) complete external joins return all rows from the left and right tables. When a row does not have a matching row in another table, the selection list column for the other table contains a null value. If there are matching rows between the tables, the entire result set row contains the data values of the base table.
Here are a few examples to explain in detail
Two tables:
A (Id,name)
Data: (1, Zhang San) (2, John Doe) (3, Harry)
B (Id,name)
Data: (1, student) (2, teacher) (4, principal)

Internal connection results:
Select a.*,b.* from A inner join B on a.id=b.id;
1 31 students
2 Li 42 Teacher

Left JOIN Connection results:
Select a.*,b.* from A left join B on A.id=b.id;
1 31 students
2 Li 42 Teacher
3 Harry NULL NULL

Right Join Result:
Select a.*,b.* from A right join B on a.id=b.id;
1 31 students
2 Li 42 Teacher
Null NULL 4 Principal

Full connectivity Results
Select a.*,b.* from A full join B on a.id=b.id;
1 31 students
2 Li 42 Teacher
3 Harry NULL NULL
Null NULL 4 Principal

****************
Add: The following situation will be used outside the connection
For example, there are two tables, one is the user table, the other is the transaction table, if I want to query each user's transactions will need to use the left outer connection, because not every user has a transaction record.
After you use the left outer connection, the information for the transaction will be displayed, and nothing will show null, as I have shown in the example above.
If there is no external connection, such as "Harry" There is no transaction record, then the user table "Harry" information will not be displayed, lost the query all user transaction significance.
****************

Look at the results to understand the differences between the various connections.

SQL Four connections: internal connection, left outer connection, right outer connection, full connection--reprint

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.