Use left join and inner join in SQL.

Source: Internet
Author: User

Use left join and inner join in SQL.

Left join on is a left Outer join. The data is based on the table on the left. The table data on the right is queried even if it is null,

Inner join on means that the data in the tables on both sides of the left and right must correspond to each other. If there is null, the data will be removed. If

If the data in the right or left table is null, no result set is found. The two connections work together to achieve excellent results.

For some requirements, we need to work with these two join queries.


Differences between inner join, left join, right join, and outer join in SQL

For example, you will know!
Table A (a1, b1, c1) Table B (a2, b2)
A1 b1 c1 a2 b2
01 mathematics 95 01 Zhang San
02 language 90 02 Li Si
03 English 80 04 Wang Wu
Select A. *, B. * from
Inner join B on (A. a1 = B. a2)
The result is:
A1 b1 c1 a2 b2
01 mathematics 95 01 Zhang San
02 language 90 02 Li Si

Select A. *, B. * from
Left outer join B on (A. a1 = B. a2)
The result is:
A1 b1 c1 a2 b2
01 mathematics 95 01 Zhang San
02 language 90 02 Li Si
03 English 80 NULL

Select A. *, B. * from
Right outer join B on (A. a1 = B. a2)
The result is:
A1 b1 c1 a2 b2
01 mathematics 95 01 Zhang San
02 language 90 02 Li Si
NULL 04 Wang Wu

Select A. *, B. * from
Full outer join B on (A. a1 = B. a2)
The result is:
A1 b1 c1 a2 b2
01 mathematics 95 01 Zhang San
02 language 90 02 Li Si
03 English 80 NULL
NULL 04 Wang Wu

SQL server 2005 connection query (inner join, left join, right join)

Inner join: inner join. The result only contains columns that meet the conditions.
Left join: left Outer join. The result contains the rows that meet the conditions and all rows in the left table.
Right join: right outer join. The result contains the rows that meet the conditions and all rows in the right table.

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.