You are on page 1of 15

Database Systems

CSCS- 213

By:
Shahid Mahmood
Warning!
 Lecture slides are high-level description
of lectures

 Relying only upon the slides do not


guarantee good grades in exams

 For achieving good grades in exams


always attend lectures, Study books,
explore web and other resources, and
be efficient in submitting your allocated
tasks.
Agenda
 SQL Lecture-4

 Accessing Multiple tables

 Nested Query
SQL statements for Accessing
Multiple Tables
o Such statements might require:

- Cartesian Product

- Inner join

- Outer join

- Full outer join

- Semi join

- Natural join
SQL statements for Accessing
Multiple Tables
o Cartesian Product: SQL uses SELECT command for
Cartesian product by using names of the tables
involved.

It Produces m X n rows

E.g., Select * from PROGRAM, COURSE

 Certain columns can be selected. Same column name


needs to be qualified by giving table name appended
SQL statements for Accessing
Multiple Tables
o Inner Join: Only those rows from both tables are
merged that have same value for the common
attribute; equijoin

Common attributes need not to have same name but


must have the same domain

E.g.,

Select * from COURSE innerjoin PROGRAM


ON COURSE. PrName = PROGRAM. PrName

Using Alias:

Select * from COURSE c innerjoin PROGRAM p

ON c. PrName = p. PrName
SQL statements for Accessing
Multiple Tables
o Inner Join with WHERE clause

E.g.,

Select * from COURSE, PROGRAM

WHERE

COURSE. PrName = PROGRAM. PrName


SQL statements for Accessing
Multiple Tables
o Outer Join: An outer join retrieves all rows that
satisfy the join condition, plus unmatched rows in
one or both tables.

non matching rows in the resultant table will have


NULL values against non matched row

It could be in the form of ‘Left outer join’, ‘Right


outer join’, ‘Full outer join’

Left outer join: Inner join plus rows from the non-matching
rows from the left table

A left outer join B = B right outer join A

Right outer join: Inner join plus rows from the non-
matching rows from the right table

Full outer join: Inner join plus the non-matching rows from
both tables
SQL statements for Accessing
Multiple Tables
o Outer Join:

e.g.,

Select *

from

COURSE c

Left outer join

PROGRAM p

ON c. PrName = p. PrName
Outer join (Left-outer Join)
Invoice

INV_NO CUSTNO INV_DATE INV_AMT


10 1 12-02-2010 100
20 1 12-02-2010 200

PAYMENT

INV_NO PMT_NO PMT_DATE INV_AMT


10 1 14-02-2010 50
10 2 16-02-2010 50
Outer join
INVOICE LEFT OUT JOIN PAYMENT
GIVING INV_PMT

INO_NO CUSTNO INV_DATE INV_AMT PMT_NO PMT_DATE PMT_AMT

10 1 1-3-10 100 1 21-3-10 50

10 1 1-3-10 100 2 13-4-10 50

20 1 5-3-10 200 - - -
SQL statements for Accessing
Multiple Tables
o Semi Join: First inner join and then projected on the
attributes of one table.

It tells the rows involve in join

e.g.,

Select distinct p.prName, totsem, prCredits

from

PROGRAM p inner join COURSE c

ON p.PrName = c.PrName
SQL statements for Accessing
Multiple Tables
o Self Join: A table making relationship with itself

e.g.,

Select a.stId, a.stName, b.stId, b.stName

from

Student a, student b
Sub Queries
 It is a query with in a query

 Useful when condition has to be applied against an


unknown value

e.g.,

Get the names of those students who have more


cgpa than that of maximum of BCS students.

SELECT * from student where

cgpa > (SELECT Max(cgpa) from STUDENT where


prName= ‘BCS’)
Questions?

You might also like