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

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

1006

DGAT

## Diploma in Gaming and Animation

Techniques

1004

DIC

Diploma In Computing

1006

DICT

1002

DIT

1001

DNC

1003

CourseID

CourseDesp

S001

Bartell

DCS

## Diploma In Computer Studies

S002

Kebel

DIC

Diploma In Computing

S003

Lee

DIT

S004

Lewis

DICT

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

1006

S003

Lee

DIT

DGAT

1004

S004

Lewis

DICT

## Diploma in Gaming and Animation

Techniques

DIC

Diploma In Computing

1006

DICT

1002

DIT

1001

DNC

StudID

LastName

CourseDesp

MentorID

1003

CourseDesp

S001

Bartell

## Diploma In Computer Studies

S001

Bartell

Diploma In Computing

S001

Bartell

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

1006

DGAT

## Diploma in Gaming and Animation

Techniques

1004

DIT

DIC

Diploma In Computing

1006

DICT

DICT

1002

DIT

1001

DNC

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

S006

Mikulski

DCS

S007

Tham

DCS

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

S005

Law

DIT

S011

Ng

DIT

S013

Roche

DIT

S004

Lewis

DICT

S009

Nicosia

DICT

S012

Maser

DICT

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

S007

Tham

## Diploma In Computer Studies

S002

Kebel

Diploma In Computing

S015

Chan

Diploma In Computing

S003

Lee

S013

Roche

S012

Maser

S016

Jann

## 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

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

S005

Law

Arthur M.

S004

Lewis

Derrick H.

S009

Nicosia

Anthony L.

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

LastName

GPA

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
table

Retrieving Records
with Nonequijoins
Where S.GPA
OUTPUT:
LastName

GPA

Bartell

3.21 B

Kebel

2.71 C

Lee

3.82 B

Law

3.05 B

Mikulski

1.89 D

STUDENT
StudID

COURSE
LastName

CourseID

CourseID

CourseDesp

MentorID

DCS

1006

DGAT

## Diploma in Gaming and Animation

Techniques

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

## 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

Carroll

S006

Mikulski

Carroll

S007

Tham

Carroll

S014

Williams

## Diploma In Computer Studies

Carroll

S002

Kebel

Diploma In Computing

Carroll

...

Selfjoins
StudID

LastName

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

## Joining a Table to Itself

SELECT M.LastName & ' belongs to ' & G.LastName & '''s group' AS
[GROUP]
FROM student AS M, student AS G
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