SQL Compaction Basics (i): differences between inner join, outer join, and Cross join

Source: Internet
Author: User
Tags joins

first, data construction

First build the table, then talk.

Create DatabaseTest UseTestCreate TableA (AIDint Identity(1,1)Primary Key, namenvarchar( -), ageint)Create TableB (BIDint Identity(1,1)Primary Key, namenvarchar( -), Genderint)

After creation, insert data

InsertA (Name,age)Values('Zhang San', *)InsertA (Name,age)Values('John Doe', -)InsertA (Name,age)Values('XXX', *)InsertA (Name,age)Values('YYY', *)

InsertB (Name,gender)Values('Zhang San',1)InsertB (Name,gender)Values('John Doe',1)InsertB (Name,gender)Values('AAA',2)InsertB (Name,gender)Values('BBB',2)

second, contrast test

By default it is inner join (that is, you enter the join directly), the left join and right join used in development belong to the outer join, and the outer join also includes the full join. Below I let you know the difference by the icon.

There are two tables, table a,table B each with four records, of which two records name is the same:

The result of the 1.INNER JOIN is the intersection of AB

SELECT * from A INNER JOIN B on a.name = B.name

A 2.LEFT [OUTER] JOIN produces a full set of table A, whereas a match in B table has a value, and no match is substituted with a null value.

SELECT * from A left OUTER JOIN B on a.name = B.name

A 3.RIGHT [OUTER] JOIN produces a full set of Table B, and a match in table A has a value, and no match is substituted with a null value.

SELECT * from A right OUTER JOIN B on a.name = B.name

The 4.FULL [OUTER] JOIN produces a and B's set. For records that do not have a match, NULL is the value.

SELECT * from A full OUTER JOIN B on a.name = B.name

You can find the value without matching by the IS null:

SELECT * from A full OUTER JOIN B on a.name = B.name

WHERE a.id is null OR b.id is null

5. Cross joins a n*m combination of the data from table A and table B, the Cartesian product. If this regular session produces 4*4=16 records, we must filter the data during the development process, so this is seldom used.

SELECT * from A cross JOIN B

We believe that the difference between inner join, outer join and Cross join is at a glance.

Add one point: According to the SQL Standard cross join is a Cartesian product. For MySQL, however, Cross joins are equivalent to INNER joins.

SQL Compaction Basics (i): differences between inner join, outer join, and Cross join

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.