Summary of how joins are used in SQL Server

Source: Internet
Author: User
Tags joins

JOIN is divided into: Inner Connection ( INNER join), outer join (OUTER join). Among them, the outer joins are: left outer connection (OUTERjoin), right outer join (OUTER join), full outer join ( Full OUTER JOIN ), where the outer connection "OUTER" keywords can be omitted without writing.

1. Internal connection (shows the data that the left and right two tables can match exactly):

Select P.provinceid,p.provincename,c.cityname,c.provinceid,c.cityid
from [dbo]. [City] C inner JOIN [dbo]. [Province] P on p.provinceid = C.cityid where c.provinceid= 6

2. Left outer connection (displays all data on the left table, the right table does not match the display as NULL):

Select P.provinceid,p.provincename,c.cityname,c.provinceid,c.cityid
from [dbo]. [City] C left JOIN [dbo]. [Province] P on p.provinceid = C.cityid where c.provinceid= 6

The result is:

3. Right Outer connection (displays all data in the right table, the left table does not match the display as NULL):

Select P.provinceid,p.provincename,c.cityname,c.provinceid,c.cityid
from [dbo]. [Province] P right JOIN [dbo]. [City] C on p.provinceid = C.cityid where c.provinceid= 16

The result is:

4. Full outer connection (displays all data on both the left and right sides of the table, NULL if the two tables do not match ):

Select P.provinceid,p.provincename,c.cityname,c.provinceid,c.cityid
from [dbo]. [Province] P full OUTER JOIN [dbo]. [City] C on p.provinceid = C.cityid where c.provinceid= 6

The result is:

Summary of how joins are used in SQL Server

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.