The difference between a left join, right join, INNER join in an SQL statement

Source: Internet
Author: User

The LEFT join returns records that include all records in the left table and the equivalent of the junction fields in the right table;
Right join returns records that include all records in the right table and the junction fields in the left table;
INNER JOIN (equivalent join) returns only rows that have the same join field in two tables.

We build two simple tables A and B,:

A:B:

1) leftjoin

We execute the SQL statement: SELECT * from A left join B on a.id = b.id;

The results are as follows:

Description: The left join is based on the records of Table A, a can be regarded as the right table, and B can be regarded as left table.
In other words, the records of the left table (A) will all be represented, and the right table (B) will only display records that match the search criteria (in the example: a.ID = b.id).
The low-record of table B is null.

1) Rightjoin

We execute the SQL statement: SELECT * from A right join B on a.id = b.id;

The results are as follows:

Note : The rightjoin is based on the records of Table B, a can be regarded as the left table, B can be regarded as the right table, and then join is based on the table.
Based on the right table (B), a table of insufficient places is filled with null.

1)INNER JOIN

We execute the SQL statement: SELECT * from A inner join B on a.id = b.id;

The results are as follows:

Note : only a.id = b.ID records are shown here. This shows that inner join is not based on who, it only shows records that match the criteria.

The difference between a left join, right join, INNER join in an SQL statement

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.