SQL Server [Join] Grooming

Source: Internet
Author: User
Tags null null


"Table A"
Aid Aname Acode
1 AA 001
2 BB 002
3 CC 003
4 DD 004
5 EE 005"Table B"
Bid bname Bcode
1 AAA 101
3 BBB 102
5 CCC 103
7 DDD 104
9 eee----------------------------------------------------------------------------
Left join: The LEFT join returns records that include all records in the left table and the equivalent of the junction fields in the right table. Cases:
SELECT *
From A
Left JOIN B
On A.aid=b.bid
Results:
Aid Aname acode Bid bname bcode
1 AA 001 1 AAA 101
2 BB 002 null NULL NULL
3 CC 003 3 BBB 102
4 DD 004 NULL NULL NULL
5 EE 005 5 CCC 103----------------------------------------------------------------------------Right join: Right Join
Returns records that include all the records in the right table and the equivalent of the junction fields in the left table. Cases:
SELECT *
From A
Right JOIN B
On A.aid=b.bid
Results:
Aid Aname acode Bid bname bcode
1 AA 001 1 AAA 101
3 CC 003 3 BBB 102
5 EE 005 5 CCC 103
null NULL NULL 7 DDD 104
null NULL NULL 9 eee----------------------------------------------------------------------------INNER JOIN: INNER JOIN
Only rows that are equal to the junction fields in two tables are returned.
Cases:
SELECT *
From A
INNER JOIN B//inner can not write
On A.aid=b.bid
Results:
Aid Aname acode Bid bname bcode
1 AA 001 1 AAA 101
3 CC 003 3 BBB 102
5 EE 005 5 CCC 103----------------------------------------------------------------------------Full Join: Full Join
Returns all the fields in two tables.
Cases:
SELECT *
From A
Full JOIN B
On A.aid=b.bid
Results:
Aid Aname acode Bid bname bcode
1 AA 001 1 AAA 101
2 BB 002 null NULL NULL
3 CC 003 3 BBB 102
4 DD 004 NULL NULL NULL
5 EE 005 5 CCC 103
null NULL NULL 7 DDD 104
null NULL NULL 9 eee
----------------------------------------------------------------------------Cross Join: Cross Join
Returns a Cartesian product of two tables.
Example 1:
SELECT *
From A
Cross JOIN B
Results:
Aid Aname acode Bid bname bcode
1 1
2 1
3 1
4 (5*5 article) 1
5 1
1 3
2 3
......
......
......
Example 2:
SELECT *
From A
Cross JOIN B
WHERE
A.aid = B.bid
Results:
Aid Aname acode Bid bname bcode
1 AA 001 1 AAA 101
3 CC 003 3 BBB 102
5 EE 005 5 CCC 103

As with the inner join result, you are actually returning all the results (25) and then querying the results that match where.

SQL Server [Join] Grooming

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.