TYPE OF
JOIN
|
SQL
QUERY
|
RESULT
|
REMARK
|
||||||||||||||||||||||||||||||||
SELECT *
FROM [HKS].[dbo].[MERIT_LIST]
|
|
Note that Roll No - 1002, 1004 don't exist in this table.
|
|||||||||||||||||||||||||||||||||
SELECT *
FROM [HKS].[dbo].[STUDENT_RECORD]
|
|
Note that Roll No 2004 doesn't exist in this table.
|
|||||||||||||||||||||||||||||||||
INNER JOIN
|
SELECT LIST.RANK, LIST.MARKS, RECORD.NAME,
RECORD.GENDER
FROM STUDENT_RECORD AS RECORD
INNER JOIN MERIT_LIST AS LIST ON LIST.RollNo =
RECORD.RollNo
ORDER BY LIST.RANK
|
|
At least one match in both tables
|
||||||||||||||||||||||||||||||||
LEFT JOIN
OR
LEFT OUTER JOIN
|
SELECT LIST.RANK, LIST.MARKS, RECORD.NAME,
RECORD.GENDER
FROM STUDENT_RECORD AS RECORD
LEFT JOIN MERIT_LIST AS LIST ON LIST.RollNo =
RECORD.RollNo
ORDER BY LIST.RANK
|
|
Returns all
rows from the left table,
even if there are no matches in the right
table
Here
LEFT
TABLE - STUDENT_RECORD
RIGHT TABLE -
MERIT_LIST
So Roll No 1002 (Mike) and 1004 (Sue) exist here with null marks. |
||||||||||||||||||||||||||||||||
RIGHT JOIN
OR
RIGHT OUTER JOIN
|
SELECT LIST.RANK, LIST.MARKS, RECORD.NAME,
RECORD.GENDER
FROM STUDENT_RECORD AS RECORD
RIGHT JOIN MERIT_LIST AS LIST ON LIST.RollNo =
RECORD.RollNo
ORDER BY LIST.RANK
|
|
Returns
all rows from the right table,
even
if there are no matches in the left table
Here
LEFT
TABLE - STUDENT_RECORD
RIGHT TABLE -
MERIT_LIST
So Roll No 2004 exists here without name and gender information. |
||||||||||||||||||||||||||||||||
FULL JOIN
|
SELECT LIST.RANK, LIST.MARKS, RECORD.NAME,
RECORD.GENDER
FROM STUDENT_RECORD AS RECORD
FULL JOIN MERIT_LIST AS LIST ON LIST.RollNo =
RECORD.RollNo
ORDER BY LIST.RANK
|
|
If there is a match in one of the tables
|
My sharing and discussion of topics in C#, WCF, WPF, Winforms, SQL, ASP.Net, Windows Service, Java Script .... with you all.
Monday, September 10, 2012
Quick review of SQL Joins
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment