SQL Server difference test method for several joins _mssql

Source: Internet
Author: User
Tags joins

This is a major introduction to the difference between the next inner join, the full out join, the Cross join, the left join, and the right join.

Inner Join: Filter records on both sides
Full out Join: Both sides are filtered out, matching can match, cannot match the list with null
Cross Join: Lists all the combinations on both sides, also known as Cartesian sets AXB
Left Join: Lists all records in the main table, matches, and cannot match, with null lists as the table on the left-hand side
Right Join: Lists all records in the primary table, matching matches, mismatched null lists, with the table on the right-hand side as the main table

Here's a look at the code:

To create a test table:

CreateTable Consumers 
(
consumer_id intnotnull, consumer_name VARCHAR
)

CreateTable ORDERS
(
consumer_id intnotnull, 
order_id VARCHAR)

Compiling test data

Insert consumers values (1, ' AA ')
Insert consumers values (2, ' BB ')
Insert consumers values (3, ' CC ')

in SERT Orders VALUES (2, ' O100001 ')
Insert Orders VALUES (3, ' O100002 ')
Insert Orders VALUES (3, ' O100003 ') 
   insert ORDERS VALUES (4, ' O100004 ')

Test

--inner Join
--filter on both sides of the record
SELECT * from
ORDERS o Inner Join consumers c on
o.consumer_id = c.consumer_id
   --full out Join
--both are filtered out, matched to match, and cannot be matched with null list
SELECT * from
ORDERS o full OUTER Join consumers C on
O. consumer_id = c.consumer_id

--cross Join
--Lists all the combinations on both sides, that is, the Cartesian set AXB
SELECT * from
ORDERS o Cross Join Consumers C

--left Join
--Take the table on the left as the main table, list all the records in the primary table, match the matches, and not match the null list
SELECT * from 
consumers Join ORDERS o on
c. consumer_id = O. consumer_id

--right Join
--Lists all records in the primary table as the table on the right, matches the matching, does not match, and lists the
SELECT * from 
consumers C with null Right JOIN ORDERS o on
c. consumer_id = O. consumer_id

OK, the specific people can refer to the cloud Habitat community previously published articles.

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.