11 - Chapter 6 - Queries Involving More Than One Relation
11 - Chapter 6 - Queries Involving More Than One Relation
Session 12
Chapter 6 – Queries involving
more than one relation
Objectives
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
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
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
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
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
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
Sometimes you
need to join a table
to itself.
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:
Sometimes you
need to join a table
to itself.
{A ∩ B} = {a | a is in A and B}
Difference
SQL Server
Oracle