Joining Tables
Objectives
Write
COURSE
LastName
...
CourseID
S001
Bartell
DCS
COURSE
STUDENT
S002
Kebel
DIC
S003
Lee
DIT
S004
Lewis
DICT
StudID
LastName
CourseID
CourseDesp
MentorID
DCS
1006
DGAT
1004
DIC
Diploma In Computing
1006
DICT
1002
DIT
1001
DNC
1003
CourseID
CourseDesp
S001
Bartell
DCS
S002
Kebel
DIC
Diploma In Computing
S003
Lee
DIT
S004
Lewis
DICT
Write
table1.column1, table2.column2
table1, table2
table1.column1 = table2.column2;
Cartesian Product
A Cartesian
A join
condition is omitted
A join condition is invalid
All rows in the first table are joined to all rows in
the second table
To avoid
COURSE (6 rows)
StudID
LastName
...
CourseID
S001
Bartell
DCS
CourseI
D
S002
Kebel
DIC
DCS
1006
S003
Lee
DIT
DGAT
1004
S004
Lewis
DICT
DIC
Diploma In Computing
1006
DICT
1002
DIT
1001
DNC
StudID
LastName
CourseDesp
MentorID
1003
CourseDesp
S001
Bartell
S001
Bartell
Diploma In Computing
S001
Bartell
S002
Kebel
S002
Kebel
Diploma In Computing
S002
Kebel
Cartesian
Product :
16 * 6 =
96 rows
Types of Joins
Equijoin Nonequijoin
Self join
What Is an Equijoin?
STUDENT
StudID
S001
LastName
Bartell
...
CourseID
DCS
COURSE
CourseID
DCS
CourseDesp
Diploma In Computer Studies
MentorID
1006
Equijoin
STUDENT
COURSE
StudI
D
LastNa
me
...
S001
Bartell
S002
Kebel
S003
Lee
S004
Lewis
Course
ID
CourseI
D
CourseDesp
MentorI
D
DCS
1006
DGAT
1004
DIT
DIC
Diploma In Computing
1006
DICT
DICT
1002
DIT
1001
DNC
1003
DCS
COURSE
STUDENT
DIC
Foreign key
Primary key
OUTPUT:
StudID
LastName
CourseID
CourseDesp
S001
Bartell
DCS
S006
Mikulski
DCS
S007
Tham
DCS
S014
Williams
DCS
S002
Kebel
DIC
Diploma In Computing
S008
Faga
DIC
Diploma In Computing
S010
Owen
DIC
Diploma In Computing
S015
Chan
DIC
Diploma In Computing
S003
Lee
DIT
S005
Law
DIT
S011
Ng
DIT
S013
Roche
DIT
S004
Lewis
DICT
S009
Nicosia
DICT
S012
Maser
DICT
S016
Jann
DNC
Use
Qualifying Ambiguous
Column Names
LastName
CourseDesp
S006
Mikulski
S007
Tham
S002
Kebel
Diploma In Computing
S015
Chan
Diploma In Computing
S003
Lee
S013
Roche
S012
Maser
S016
Jann
LastName
CourseDesp
S002
Kebel
Diploma In Computing
S006
Mikulski
S007
Tham
S015
Chan
Diploma In Computing
LastName
Firstname
CourseDesp
S003
Lee
Choy Yan
S005
Law
Arthur M.
S004
Lewis
Derrick H.
S009
Nicosia
Anthony L.
Table Aliases
Simplify
aliases.
aliases.
SELECT
Student.StudID, Student.LastName,
Student.CourseID, Course.CourseID, Course.CourseDesp
FROM Student, Course
WHERE Student.CourseID=Course.CourseID;
can
be written
as ...
SELECT
S.StudID,
S.LastName,
S.CourseID,C.CourseID,C.CourseDesp
FROM Student S, Course C
WHERE S.CourseID=C.CourseID;
Nonequijoins
STUDENT
StudID
GRADETABLE
LastName
GPA
Grade
Hi_gradepoint Low_gradepoint
S007
Tham
3.89
S003
Lee
3.82
3.9
S010
Owen
3.34
2.9
S001
Bartell
3.21
1.9
S015
Chan
3.12
0.9 0
Retrieving Records
with Nonequijoins
SELECT LastName, GPA, Grade
from Student S, Gradetable G
Where S.GPA
Between G.Low_gradepoint and G.Hi_gradepoint;
OUTPUT:
LastName
GPA
Bartell
3.21 B
Kebel
2.71 C
Lee
3.82 B
Law
3.05 B
Mikulski
1.89 D
Grade
COURSE
LastName
CourseID
CourseID
CourseDesp
MentorID
DCS
1006
DGAT
1004
DIT
DIC
Diploma In Computing
1006
Lewis
DICT
DICT
1002
S005
Law
DIT
DIT
1001
S006
Mikulski
DCS
DNC
1003
S007
Tham
DCS
S008
Faga
DIC
S009
Nicosia
DICT
S010
Owen
DIC
1001
Goile
S011
Ng
DIT
1002
Rimes
S012
Maser
DICT
1003
Christopher
S013
Roche
DIT
1004
Schubert
S014
Williams
DCS
1005
Norman
S015
Chan
DIC
1006
Carroll
S016
Jann
DNC
S001
Bartell
DCS
S002
Kebel
DIC
S003
Lee
S004
MENTOR
MentorID
MentorName
LastName
CourseDesp
MentorName
S001
Bartell
Carroll
S006
Mikulski
Carroll
S007
Tham
Carroll
S014
Williams
Carroll
S002
Kebel
Diploma In Computing
Carroll
...
Selfjoins
STUDENT (GROUPLEADER)
StudID
LastName
GroupLeader
STUDENT (MEMBER)
StudID
LastName
S001
Bartell
S005
S005
Law
S002
Kebel
S007
S007
Tham
S003
Lee
S016
S016
Jann
S004
Lewis
S007
S007
Tham
S006
Mikulski
S010
S010
Owen
S008
Faga
S010
S010
Owen
S009
Nicosia
S005
S005
Law
S011
Ng
S007
S007
Tham
S012
Maser
S016
S016
Jann
S013
Roche
S005
S005
Law
S014
Williams
S010
S010
Owen
S015
Chan
S016
S016
Jann
GROUP
Bartell belongs to Law's group
Kebel belongs to Tham's group
Lee belongs to Jann's group
Lewis belongs to Tham's group
Mikulski belongs to Owen's group
Faga belongs to Owen's group
Nicosia belongs to Law's group
Ng belongs to Tham's group
Maser belongs to Jann's group
Roche belongs to Law's group
Williams belongs to Owen's group
Chan belongs to Jann's group
Summary
Understand the differences between
the types of joins
Avoid Cartesian product in any joins
Specify the join condition in the
WHERE clause