Anda di halaman 1dari 22

Chapter 8

Joining Tables

Objectives
Write

SELECT statements using equality


and nonequality joins to access data from
more than one table
Describe the Cartesian product
Join a table to itself

Obtaining Data from Multiple Tables


STUDENT
StudID

COURSE

LastName

...

CourseID

S001

Bartell

DCS
COURSE
STUDENT

S002

Kebel

DIC

S003

Lee

DIT

S004

Lewis

DICT

StudID

LastName

CourseID

CourseDesp

MentorID

DCS

Diploma In Computer Studies

1006

DGAT

Diploma in Gaming and Animation


Techniques

1004

DIC

Diploma In Computing

1006

DICT

Diploma In Info-Comm Technology

1002

DIT

Diploma In Information Technology

1001

DNC

Diploma in Network and CyberSecurity

1003

CourseID

CourseDesp

S001

Bartell

DCS

Diploma In Computer Studies

S002

Kebel

DIC

Diploma In Computing

S003

Lee

DIT

Diploma In Information Technology

S004

Lewis

DICT

Diploma In Info-Comm Technology

Syntax for Joining Tables


Use

a join to query data from more


than one table:
SELECT
FROM
WHERE

Write

table1.column1, table2.column2
table1, table2
table1.column1 = table2.column2;

the join condition in the WHERE


clause.
Prefix the column name with the table
name when the same column name
appears in more than one table.

Cartesian Product
A Cartesian

product is formed when:

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

a Cartesian product, always


include a valid join condition in a WHERE
clause.

Generating a Cartesian Product


STUDENT (16 rows)

COURSE (6 rows)

StudID

LastName

...

CourseID

S001

Bartell

DCS

CourseI
D

S002

Kebel

DIC

DCS

Diploma In Computer Studies

1006

S003

Lee

DIT

DGAT

1004

S004

Lewis

DICT

Diploma in Gaming and Animation


Techniques

DIC

Diploma In Computing

1006

DICT

Diploma In Info-Comm Technology

1002

DIT

Diploma In Information Technology

1001

DNC

Diploma in Network and CyberSecurity

StudID

LastName

CourseDesp

MentorID

1003

CourseDesp

S001

Bartell

Diploma In Computer Studies

S001

Bartell

Diploma In Computing

S001

Bartell

Diploma In Information Technology

S002

Kebel

Diploma In Computer Studies

S002

Kebel

Diploma In Computing

S002

Kebel

Diploma In Information Technology

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

Links rows that satisfy a specified condition


WHERE Student.courseID = Course.CouseID

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

Diploma In Computer Studies

1006

DGAT

Diploma in Gaming and Animation


Techniques

1004

DIT

DIC

Diploma In Computing

1006

DICT

DICT

Diploma In Info-Comm Technology

1002

DIT

Diploma In Information Technology

1001

DNC

Diploma in Network and


CyberSecurity

1003

DCS

COURSE
STUDENT
DIC

Foreign key

Primary key

Retrieving Records with an Equijoin


SELECT StudID, LastName,Course.CourseID, CourseDesp
from Student, Course
Where Student.CourseID = Course.CourseID;

OUTPUT:

StudID

LastName

CourseID

CourseDesp

S001

Bartell

DCS

Diploma In Computer Studies

S006

Mikulski

DCS

Diploma In Computer Studies

S007

Tham

DCS

Diploma In Computer Studies

S014

Williams

DCS

Diploma In Computer Studies

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

Diploma In Information Technology

S005

Law

DIT

Diploma In Information Technology

S011

Ng

DIT

Diploma In Information Technology

S013

Roche

DIT

Diploma In Information Technology

S004

Lewis

DICT

Diploma In Info-Comm Technology

S009

Nicosia

DICT

Diploma In Info-Comm Technology

S012

Maser

DICT

Diploma In Info-Comm Technology

S016

Jann

DNC

Diploma in Network and CyberSecurity

Use

Qualifying Ambiguous
Column Names

table prefixes to qualify column names that


are in multiple tables.
Use table prefixes to improve performance.

Retrieving Data using Equi join


SELECT StudID, LastName, CourseDesp
from Student, Course
Where Student.CourseID = Course.CourseID
and Gender = 'F';
Output:
StudID

LastName

CourseDesp

S006

Mikulski

Diploma In Computer Studies

S007

Tham

Diploma In Computer Studies

S002

Kebel

Diploma In Computing

S015

Chan

Diploma In Computing

S003

Lee

Diploma In Information Technology

S013

Roche

Diploma In Information Technology

S012

Maser

Diploma In Info-Comm Technology

S016

Jann

Diploma in Network and CyberSecurity

Retrieving Data using Equi join


SELECT StudID, LastName, CourseDesp
from Student, Course
Where Student.CourseID = Course.CourseID
and Gender = 'F'
and Student.CourseID in ('DCS','DIC');
Output:
StudID

LastName

CourseDesp

S002

Kebel

Diploma In Computing

S006

Mikulski

Diploma In Computer Studies

S007

Tham

Diploma In Computer Studies

S015

Chan

Diploma In Computing

Retrieving Data using Equi join


SELECT StudID, LastName, Firstname, CourseDesp
from Student, Course
Where Student.CourseID = Course.CourseID
and (LastName Like 'L*'
or FirstName Like 'A*')
Output:
StudID

LastName

Firstname

CourseDesp

S003

Lee

Choy Yan

Diploma In Information Technology

S005

Law

Arthur M.

Diploma In Information Technology

S004

Lewis

Derrick H.

Diploma In Info-Comm Technology

S009

Nicosia

Anthony L.

Diploma In Info-Comm Technology

Table Aliases
Simplify

queries by using table

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

GPA in the STUDENT


table is between
Low_gradepoint and
Hi_gradepoint in the GRADETABLE
table

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

Joining More Than Two Tables


STUDENT
StudID

COURSE
LastName

CourseID

CourseID

CourseDesp

MentorID

DCS

Diploma In Computer Studies

1006

DGAT

Diploma in Gaming and Animation


Techniques

1004

DIT

DIC

Diploma In Computing

1006

Lewis

DICT

DICT

Diploma In Info-Comm Technology

1002

S005

Law

DIT

DIT

Diploma In Information Technology

1001

S006

Mikulski

DCS

DNC

Diploma in Network and CyberSecurity

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

Using Multiple Joins


SELECT S.StudID, S.LastName, C.CourseDesp,M.MentorName
from Student S, Course C, Mentor M
Where S.CourseID = C.CourseID
and C.MentorID = M.MentorID;
OUTPUT:
StudID

LastName

CourseDesp

MentorName

S001

Bartell

Diploma In Computer Studies

Carroll

S006

Mikulski

Diploma In Computer Studies

Carroll

S007

Tham

Diploma In Computer Studies

Carroll

S014

Williams

Diploma In Computer Studies

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

GroupLeader in the member table is equal to the StudID in


the GROUPLEADER table.

Joining a Table to Itself


SELECT M.LastName & ' belongs to ' & G.LastName & '''s group' AS
[GROUP]
FROM student AS M, student AS G
WHERE M.GroupLeader=G.studid;
OUTPUT:

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