0% found this document useful (0 votes)
42 views

11 - Chapter 6 - Queries Involving More Than One Relation

This document discusses querying data from multiple tables in a database using SQL. It covers joining tables using various join types like inner joins, outer joins, and self joins. It also discusses disambiguating attribute names when tables share column names, and using tuple variables when querying the same table multiple times. Finally, it covers set operators like UNION, INTERSECT, and EXCEPT that allow combining result sets of multiple queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

11 - Chapter 6 - Queries Involving More Than One Relation

This document discusses querying data from multiple tables in a database using SQL. It covers joining tables using various join types like inner joins, outer joins, and self joins. It also discusses disambiguating attribute names when tables share column names, and using tuple variables when querying the same table multiple times. Finally, it covers set operators like UNION, INTERSECT, and EXCEPT that allow combining result sets of multiple queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Database Systems

Session 12
Chapter 6 – Queries involving
more than one relation
Objectives

1 Know how to query data from more than one relation

2 Know how to Dis-ambiguate Attributes

3 Understand the role of column Aliases and table Aliases

4 Know how to use set-operators (union, different, intersect) in SQL


Contents

1 Product and Join in SQL

2 Dis-ambiguating Attributes

3 Tuple Variables

4 Union

5 Intersection

6 Difference
Review: Capabilities of SQL SELECT
Statements

 Projection: You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
 Selection: You can use the selection capability in SQL to choose the rows
in a table that you want returned by a query (with WHERE clause)
 Joining: You can use the join capability in SQL to bring together data that is
stored in different tables by creating a link between them.
Review: A Cartesian Products example

Customers (6 rows) Orders (7 rows)

Customer x Orders (6x7 = 42 rows)


1 – Products & Joins in SQL

 When data from more than one table in the database


is required, a join condition is used. Rows in one
table can be joined to rows in another table according
to common values existing in corresponding columns,
that is, usually primary and foreign key columns.

 To display data from two or more related tables:


 List each relation in the FROM clause
 Write a simple join condition in the WHERE clause.

 To join n tables together, you need a minimum of n-1


join conditions.
Types of JOINs
What is an inner join?

 To determine a student’s class name, you compare the value in the


CLASS_ID column in the CLASS table with the CLASS_ID values in the
STUDENT table.
 The relationship between the STUDENT and CLASS tables is values in
the CLASS_ID column on both tables must be equal.
How to express inner Join with
Relational Algebra?

SELECT *
FROM Student A, Class B
WHERE A.class_id = B.class_id

SELECT *
FROM Student A INNER JOIN Class B
on A.class_id = B.class_id
Inner join with non-equal condition

 A join condition containing something other


than an equality operator = Theta join in RA.
Student Ranking

δA.mark >= B.min AND A.mark < B.max(AxB)

SELECT A.student_id, B.grade


FROM Student A, Ranking B
WHERE A.marks >= B.MIN
AND A.mark < B.MAX
Outer Joins

 If a row does not satisfy a join condition, the row will not appear in the query
result. These rows are called dangling rows (or dangling tuples).
 The missing rows can be returned if an outer join operator is used in the
join condition.

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201
107 3C
202 Lop 202
107 4D
5E
6F
7G
8H
Left Outer Join

 When using left outer join of CLASS (on the left) and
STUDENT (on the right), all the classes will appear
regardless of whether or not they have any student

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 Lop 202 107 4D
SELECT A.class_name 5E

FROM Class A 6F
7G
LEFT OUTER JOIN Student B
8H

ON A.class_id = B.class_id
Output of LEFT OUTER JOIN

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
106 Lop 106 106 2B
107 Lop 107 107 3C
107 Lop 107 107 4D
201 Lop 201
202 Lop 202

The red box is on the Left


Right Outer Join

When using right outer join of CLASS (on the left) and
STUDENT (on the right), all the students will appear,
regardless of whether or not they have any class

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 Lop 202 107 4D
SELECT B.name 5E
FROM Class A 6F
7G
RIGHT OUTER JOIN Student B 8H
ON A.class_id = B.class_id
Output of RIGHT OUTER JOIN

CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H

The red box is on the Right


Full Outer Join

 When using full outer join of CLASS and


STUDENT, all students and all classes will appear

CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 SELECT B.name
Lop 202 107 4D
5E
FROM class A 6F

FULL OUTER JOIN student B 7G


8H
ON A.class_id = B.class_id
Output of FULL OUTER JOIN

CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H
201 Lop 201
202 Lop 202

LEFT OUTER JOIN RIGHT OUTER JOIN


Self Joins

Sometimes you
need to join a table
to itself.

SELECT A.employee_id + ‘ MANAGED BY ‘


+ B.employee_id
FROM employee A, employee B
WHERE A.Manager_id = B.Employee_ID
Cartesian Products

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.
2. Disambiguating attributes

Sometimes we ask a query involving


several relations, and among these
relations are two or more attributes with
the same name. If so, we need a way to
indicate which of these attributes is meant
by a use of their shared name.
SQL solves this problem by allowing us to
place a relation name and a dot in front of
an attribute, thus R.A refers to the attribute
A of relation R
Disambiguating attributes example

 Two relations:
 MovieStar (name, address, gender, birthdate)
 MovieExec (name, address, cert#, netWorth)
 Both relations have attributes “name” and “address”. Look
at the following query to see the way of dis-ambiguating:

SELECT MovieStar.Name, MovieExec.Name


FROM MovieStar, MovieExec
WHERE MovieStar.Address = MovieExec.Address
3. Tuple Variables

 Disambiguating attributes by prefixing the


relation name works as long as the query
involves combining several different relations
 However, sometimes we need to ask a query
that involves two ore more tuples from the same
relation. How to do that?
 Solution: we may list a relation R as many times
as we need to in the FROM clause but we need
a way to refer to each occurrence of R. SQL
allows us to define, for each occurrence of R in
the FROM clause, an “ALIAS” which we shall
refer to as a tuple variables
Tuple variables example

Sometimes you
need to join a table
to itself.

SELECT A.employee_id + ‘ MANAGED BY ‘


+ B.employee_id
FROM employee A, employee B
WHERE A.Manager_id = B.Employee_ID
ET operators (Union, Intersection, Difference)
UNION & UNION ALL

The UNION operator eliminates any


duplicated rows.

But UNION ALL still returns duplicated


rows
Example: UNION
INTERSECT

{A ∩ B} = {a | a is in A and B}
Difference

SELECT employee_id, job_id FROM employees


EXCEPT
SELECT employee_id, job_id FROM job_history

SQL Server

Oracle

You might also like