Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 of 6
http://www.codeproject.com/Articles/571933/Joining-Tables-in-SQL
Sign in
home
articles
quick answers
discussions
features
community
help
CPOL
Rate this:
Introduction
This document will try and explain the ways that joining two tables work. To help explain this I created two temp tables with specific rows
in them to proof the point. Find the SQL to create the sample Temp tables in Appendix A. We have table TEMP_A with four rows in it the
ID's of this four rows in unique and numbered 1,2,3,4 respectively. Then we also have table TEMP_B with five rows in it. It has rows 1,
2,3,3,5 in it. Note that row 1 and 2 from Table A have one reference each in table B.
Row 3 have two reverences in table B row 4 have no reverences in table B at all, and there is an Orphan row in table B (row 5) that have no
parent row in table A.
Also note that the reserved words inner and outer is optional. left outer join and left join mean exactly the same thing.
OK now on to the fun stuff.
Using the data sample created in Appendix A we will get the following result:
Hide Copy Code
select *
from TEMP_A
INNER JOIN TEMP_B
ON TEMP_A.Tbl_ID = TEMP_B.Tbl_ID
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 1
Tbl B Row 1
Tbl A Row 2
Tbl B Row 2
Tbl A Row 3
Tbl B Row 3a
6/11/2015 12:56 AM
2 of 6
Tbl A Row 3
http://www.codeproject.com/Articles/571933/Joining-Tables-in-SQL
Tbl B Row 3b
Note that row 3 in table a is duplicated once for each corresponding row in table B.
Using the data sample created in Appendix A we will get the following result.
Hide Copy Code
select *
from TEMP_A
LEFT OUTER JOIN TEMP_B
ON TEMP_A.Tbl_ID = TEMP_B.Tbl_ID
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 1
Tbl B Row 1
Tbl A Row 2
Tbl B Row 2
Tbl A Row 3
Tbl B Row 3a
Tbl A Row 3
Tbl B Row 3b
Tbl A Row 4
NULL
NULL
Note that Row 4 from table A is now included but since there is not corresponding row in table B all the fields from table B contain
NULL's.
Using the data sample created in Appendix A we will get the following result.
Hide Copy Code
select *
from TEMP_A
RIGHT OUTER JOIN TEMP_B
ON TEMP_A.Tbl_ID = TEMP_B.Tbl_ID
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 1
Tbl B Row 1
Tbl A Row 2
Tbl B Row 2
6/11/2015 12:56 AM
3 of 6
http://www.codeproject.com/Articles/571933/Joining-Tables-in-SQL
Tbl A Row 3
Tbl B Row 3a
Tbl A Row 3
Tbl B Row 3b
NULL
NULL
Tbl B Row 5
This set also contains 5 rows. But the last row this time contains data from table B and all data from table A is NULL.
Using the data sample created in Appendix A we will get the following result:
Hide Copy Code
select *
from TEMP_A
FULL OUTER JOIN TEMP_B
ON TEMP_A.Tbl_ID = TEMP_B.Tbl_ID
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 1
Tbl B Row 1
Tbl A Row 2
Tbl B Row 2
Tbl A Row 3
Tbl B Row 3a
Tbl A Row 3
Tbl B Row 3b
Tbl A Row 4
NULL
NULL
NULL
NULL
Tbl B Row 5
Cross Join
Well a Cross join is not really a join you do not specify the fields to join on you just specify the name of the tables. It will return every row
from table A matched up with every row in table B so the end result will have lots of rows.
Using the data sample created in Appendix A we will get the following result.
Hide Copy Code
select *
from TEMP_A, TEMP_B
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 1
Tbl B Row 1
Tbl A Row 1
Tbl B Row 2
Tbl A Row 1
Tbl B Row 3a
Tbl A Row 1
Tbl B Row 3b
Tbl A Row 1
Tbl B Row 5
Tbl A Row 2
Tbl B Row 1
Tbl A Row 2
Tbl B Row 2
Tbl A Row 2
Tbl B Row 3a
Tbl A Row 2
Tbl B Row 3b
Tbl A Row 2
Tbl B Row 5
Tbl A Row 3
Tbl B Row 1
Tbl A Row 3
Tbl B Row 2
Tbl A Row 3
Tbl B Row 3a
Tbl A Row 3
Tbl B Row 3b
Tbl A Row 3
Tbl B Row 5
6/11/2015 12:56 AM
4 of 6
http://www.codeproject.com/Articles/571933/Joining-Tables-in-SQL
Tbl A Row 4
Tbl B Row 1
Tbl A Row 4
Tbl B Row 2
Tbl A Row 4
Tbl B Row 3a
Tbl A Row 4
Tbl B Row 3b
Tbl A Row 4
Tbl B Row 5
select *
from TEMP_A
INNER JOIN TEMP_B
ON TEMP_A.Tbl_ID = TEMP_B.Tbl_ID
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 1
Tbl B Row 1
Tbl A Row 2
Tbl B Row 2
Tbl A Row 3
Tbl B Row 3a
Tbl A Row 3
Tbl B Row 3b
Tbl B Row 3a
Tbl B Row 3b
select *
from TEMP_A
LEFT OUTER JOIN TEMP_B
ON TEMP_A.Tbl_ID = TEMP_B.Tbl_ID
where TEMP_B.Tbl_ID is null
Tbl_ID
Tbl_Data
Tbl_ID
Tbl_Data
Tbl A Row 4
NULL
NULL
6/11/2015 12:56 AM
5 of 6
http://www.codeproject.com/Articles/571933/Joining-Tables-in-SQL
temp table
Row 1')
Row 2')
Row 3a')
Row 3b')
Row 5')
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
EMAIL
6/11/2015 12:56 AM
6 of 6
http://www.codeproject.com/Articles/571933/Joining-Tables-in-SQL
SQL Joins
Search Comments
Profile popups
Spacing Relaxed
Layout Normal
Per page 25
Update
My vote of 5
csharpbd
My vote of 5
Amir Farid
Awesome
blitzkrieged
My vote of 3
prahalad.gaggar
10-Apr-13 3:28
My vote of 5
Ken Kazinski
10-Apr-13 2:18
My vote of 5
SRSHINDE
9-Apr-13 19:01
My vote of 5
fox6367
8-Apr-13 0:23
My vote of 5
Sherdz
7-Apr-13 22:38
My vote of 2
Selvin
5-Apr-13 1:09
Re: My vote of 2
16-Mar-14 21:17
5-Oct-13 21:48
3-Sep-13 3:06
Patrick Harris
9-Apr-13 17:32
Selvin
10-Apr-13 0:11
My vote of 5
cpaganin
4-Apr-13 23:08
My vote of 2
Member 9963480
My vote of 5
bluesathish
3-Apr-13 19:35
My vote of 5
TechnoGeek001
3-Apr-13 19:25
My vote of 5
Nikhil_S
3-Apr-13 19:01
Re: My vote of 2
General
News
4-Apr-13 9:51
Suggestion
Question
Refresh
Bug
Answer
Joke
Praise
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151103.1 | Last Updated 3 Apr 2013
6/11/2015 12:56 AM