Join two tables in SQL, associate two tables, and query multiple tables

Source: Internet
Author: User

1. What is join
Join: Join indicates the relationship between two tables. we can regard two tables as two sets. Suppose there are two tables, which are represented by a and B. The two tables have one or more identical fields. There are three different sets:
1. Intersection: records with equal fields in two tables
2. A to B supplement: records with the same field content not equal to B in
3. B submits a supplement: records with the same field content not equal to a in B

2. query by join
Three Join Operations correspond to the preceding three sets.
1. Intersection: inner join Internal join
2. A to B complement: left join
3. B cross a complement: Right join right join

Iii. Instances
Now let's look at an instance. There are two tables, table 1: address; table 2: email.
For simplicity, both tables have only one field, and the field name is "name ". Now we can get three sets:
1. There is a name record in both tables.
Select address. Name: As name
From Address inner join email on address. Name = Email. Name;

2. records with a name in the address and no name in the email
Select address. Name
From address left join email on address. Name = Email. Name
Where (email. Name) is null ));

Note: This is a left join for the address table and a right join for the mail table.

3. Records with no name in the address and a name in the email
Select email. Name
From address right join email on address. Name = Email. Name
Where (address. Name) is null ));

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.