Detailed analysis of left join, right join and inner join in Oracle

Source: Internet
Author: User
Tags null null

[/Color] [color = Green] Table A records as follows:
Aid anum
1 A11
2 A22
3 A33
4 A44
5 A55

Table B records the following:
Bid bname
1 B11
2 B22
3 B33
4 b44
8 b88

The SQL statements for creating these two tables are as follows:
Create Table
Aid int (1) auto_increment primary key,
Anum char (20)
)
Create Table B (
Bid int (1) not null auto_increment primary key,
Bname char (20)
)

INSERT INTO
Values (1, 'a11 ');
Commit;
.....

Insert into B
Values (1, 'b11 ');
Commit;
.....

1. Left join (left join)

The SQL statement is as follows:
Select * from
Left join B
On a. Aid = B. Bid

The result is as follows:
Aid anum bid bname
1 A11 1 B11
2 A22 2 B22
3 A33 3 B33
4 A44 4 b44
5 A55 null
(The number of affected rows is 5)

Result description:
Left join is based on the records of table A. A can be seen as the left table, B can be seen as the right table, and left join is based on the left table.
In other words, the records in the left table (a) are all expressed, while the right table (B) only displays records that meet the search criteria (in this example:. aid = B. bid ).
All records in Table B are null.

2. Right join (right join)

The SQL statement is as follows:
Select * from
Right joing B
On a. Aid = B. Bid

The result is as follows:
Aid anum bid bname
1 A11 1 B11
2 A22 2 B22
3 A33 3 B33
4 A44 4 b44
Null null 8 b88
(The number of affected rows is 5)

Result description:
After careful observation, we will find that the result of left join is exactly the opposite. This time, it is based on the right table (B) and is filled with null when table A is insufficient.

3. Inner join (equal join or inner join)

The SQL statement is as follows:
Select * from
Inner join B
On a. Aid = B. Bid

Equivalent to the following SQL statement:
Select *
From a, B
Where a. Aid = B. Bid

The result is as follows:
Aid anum bid bname
1 A11 1 B11
2 A22 2 B22
3 A33 3 B33
4 A44 4 b44

Result description:
Obviously, only a. Aid = B. Bid record is displayed here. This indicates that inner join only displays the matching record.
Left join is used in any from clause,

Records of the combined source table. Use the left join operation to create a left Outer Join. The outer join on the left contains all records in the two tables starting from the first (left), that is
So that there is no record of consistent values in the second (right) table.

Syntax: From Table1 left join Table2 on table1.field1 compopr table2.field2
Description: The Table1 and Table2 parameters are used to specify the names of the tables to be combined.
The field1 and field2 Parameters specify the names of joined fields. These fields must have the same data type and contain the same data type, but they do not need to have the same
Name.
The compopr parameter specifies the relational comparison operator: "=", "<", ">", "<=", ">=" or" <> ".
If you want to join a field that contains the memo data type or OLE object data type in the inner join operation, an error will occur.

Related Article

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.