Its is great to talk about JOINs, but I'm sure that there are other people who like me don't learn but hearing, they learn by seeing and trying it them self. To do that, we need to create two basic tables and populate it with some data.
First, let us create two tables to store data for our tests. Since the concept of student and teacher is most easily understood, I'm creating Teacher table and Student table with a key to link back to Teacher Table.
CREATE TABLE [dbo].[Student]
(
[ID] [int] NULL
,[FullName] [varchar](50) NULL
,[TeacherID] [int] NULL
)
GO
CREATE TABLE [dbo].[Teacher]
(
[ID] [int] NULL
,[FullName] [varchar](50) NULL
)
INSERT INTO Teacher (ID, FullName)
VALUES (1, 'Roy Chad')
,(3, 'Dudley Goddard')
,(7, 'Raphael Philander')
,(5, 'Sonnie Davin')
INSERT INTO Student (ID, FullName,TeacherID)
VALUES (1,'Jepson Balfour',1)
,(2,'Milburn Brett',1)
,(4,'Clinton Schuyler',3)
,(5,'Norbert Kemp',3)
,(7,'Meriwether Kennedy',7)
,(8,'Braith Cornelius',7)
,(10,'Dion Hayden',9)
Now that we got some data loaded into tables let the JOINing commence. First will look at 'INNER' JOIN
SELECT t.FullName AS TeacherFullName
,s.FullName AS StudentFullName
FROM Teacher t
INNER JOIN Student s
ON s.TeacherID = t.ID
TeacherFullName | StudentFullName |
---|---|
Roy Chad | Jepson Balfour |
Roy Chad | Milburn Brett |
Dudley Goddard | Clinton Schuylerr |
Dudley Goddard | Norbert Kemp |
Raphael Philander | Meriwether Kennedy |
Raphael Philander | Braith Cornelius |
LEFT OUTER JOIN, returns all records from main table and attempts to match records from secondary table.
SELECT t.FullName AS TeacherFullName
,s.FullName AS StudentFullName
FROM Teacher t
LEFT OUTER JOIN Student s
ON s.TeacherID = t.ID
TeacherFullName | StudentFullName |
---|---|
Roy Chad | Jepson Balfour |
Roy Chad | Milburn Brett |
Dudley Goddard | Clinton Schuylerr |
Dudley Goddard | Norbert Kemp |
Raphael Philander | Meriwether Kennedy |
Raphael Philander | Braith Cornelius |
Sonnie Davin | NULL |
RIGHT OUTER JOIN returns all records from RIGHT table and matches records from left table. The following two queries can be written as RIGHT or LEFT join with same results.
SELECT t.FullName AS TeacherFullName
,s.FullName AS StudentFullName
FROM Teacher t
RIGHT OUTER JOIN Student s
ON s.TeacherID = t.ID
SELECT t.FullName AS TeacherFullName
,s.FullName AS StudentFullName
FROM Student s
LEFT OUTER JOIN Teacher t
ON s.TeacherID = t.ID
TeacherFullName | StudentFullName |
---|---|
Roy Chad | Jepson Balfour |
Roy Chad | Milburn Brett |
Dudley Goddard | Clinton Schuylerr |
Dudley Goddard | Norbert Kemp |
Raphael Philander | Meriwether Kennedy |
Raphael Philander | Braith Cornelius |
NULL | Dion Hayden |
FULL OUTER JOIN attempts to match records from both tables just like INNER JOIN and also returns all additional records that do not match from ether table.
SELECT t.FullName AS TeacherFullName
,s.FullName AS StudentFullName
FROM Teacher t
FULL OUTER JOIN Student s
ON s.TeacherID = t.ID
TeacherFullName | StudentFullName |
---|---|
Roy Chad | Jepson Balfour |
Roy Chad | Milburn Brett |
Dudley Goddard | Clinton Schuylerr |
Dudley Goddard | Norbert Kemp |
Raphael Philander | Meriwether Kennedy |
Raphael Philander | Braith Cornelius |
Sonnie Davin | NULL |
NULL | Dion Hayden |
CROSS JOIN, returns all records from main table and matches them to every record from secondary table.
SELECT t.FullName AS TeacherFullName
,s.FullName AS StudentFullName
FROM Teacher t
CROSS JOIN Student s
TeacherFullName | StudentFullName |
---|---|
Roy Chad | Jepson Balfour |
Roy Chad | Milburn Brett |
Roy Chad | Clinton Schuylerr |
Roy Chad | Norbert Kemp |
Roy Chad | Meriwether Kennedy |
Roy Chad | Braith Cornelius |
Roy Chad | Dion Hayden |
Dudley Goddard | Jepson Balfour |
Dudley Goddard | Milburn Brett |
Dudley Goddard | Clinton Schuylerr |
Dudley Goddard | Norbert Kemp |
Dudley Goddard | Meriwether Kennedy |
Dudley Goddard | Braith Cornelius |
Dudley Goddard | Dion Hayden |
Raphael Philander | Jepson Balfour |
Raphael Philander | Milburn Brett |
Raphael Philander | Clinton Schuylerr |
Raphael Philander | Norbert Kemp |
Raphael Philander | Meriwether Kennedy |
Raphael Philander | Braith Cornelius |
Raphael Philander | Dion Hayden |
Sonnie Davin | Jepson Balfour |
Sonnie Davin | Milburn Brett |
Sonnie Davin | Clinton Schuylerr |
Sonnie Davin | Norbert Kemp |
Sonnie Davin | Meriwether Kennedy |
Sonnie Davin | Braith Cornelius |
Sonnie Davin | Dion Hayden |
This concludes the first look at logical JOIN operations. Just like I mentioned in beginning of the post, just about every time I write query it ends up being LEFT or INNER JOIN. Do I ever had to use others? Yes, but with specific business cases.
Please leave a comment with questions and/or feedback on post.
No comments :
Post a Comment