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

Formal Relational Query Language Part 2

The natural join operation produces the set of all combinations of tuples in relations r and s that are equal on their common attribute names. It is illustrated using an example of joining the Employee and Dept relations on the DeptName attribute.

Uploaded by

Amon Barno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Formal Relational Query Language Part 2

The natural join operation produces the set of all combinations of tuples in relations r and s that are equal on their common attribute names. It is illustrated using an example of joining the Employee and Dept relations on the DeptName attribute.

Uploaded by

Amon Barno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Natural-Join Operation

Natural join (⋈) is a binary operation that is written as (r ⋈ s) where r and s are
relations. The result of the natural join is the set of all combinations of tuples in r
and s that are equal on their common attribute names.
More formally the semantics of the natural join are defined as follows:

r ⋈ s =  R ∪ S ( r.A1 =s.A1 ∧ r.A2 =s.A2 ∧...∧ r.An =s.An (r × s))

where :
R ∩ S = {A1, A2, . . . , An} a relational schema is the design for the table. It includes
R is a relation schema of r none of the actual data, but is like a blueprint or design for
S is a relation schema of s the table

For an example consider the tables Employee and Dept and their natural join:
Natural-Join Operation (cont.)
Ilustration of Employee ⋈ Dept operation :
Employee ⋈ Dept =  E∪ D ( Employee.DeptName = Dept.DeptName (Employee × Dept))
Where :
1 E  D = {Deptname}, E is a relation schema of Employee, D is a relation schema of Dept
Employee X Dept
Steps :
Name EmpId DeptName DeptName Manager

 Employee.DeptName = Dept.DeptName
Harry 3415 Finance Finance George 1 2a 2b 3
Harry 3415 Finance Sales Harriet
Harry 3415 Finance Production Charles 2b
Sally 2241 Sales Finance George result
Sally 2241 Sales Sales Harriet
Sally 2241 Sales Production Charles 3
George 3401 Finance Finance George
George 3401 Finance Sales Harriet  E∪ D
George 3401 Finance Production Charles Name EmpId DeptName Manager

Harriet 2202 Sales Finance George Harry 3415 Finance George


Harriet 2202 Sales Sales Harriet Sally 2241 Sales Harriet
Harriet 2202 Sales Production Charles George 3401 Finance George
Harriet 2202 Sales Harriet
E  D = {Deptname}
2a
Natural-Join Operation (cont.)
if we want to join three relations such as instructor, teaches, and course,
then the natural join operation can be written:
the order of writing the relation will determine the order of
(Instructor ⋈ Teaches ⋈ Course) operations on the relation
Or
((Instructor ⋈ Teaches) ⋈ Course)
Or associative
(Instructor ⋈ (Teaches ⋈ Course))
Instructor
Example : Query :
Teaches  name,title,semester ( Instruktor .dept_name
=“Comp. Sci.” (instructor ⋈ teaches ⋈ course))

Output ??

Course
Theta-Join Operation
The theta join operation is a variant of the natural-join operation.
Consider relations r (R) and s(S), and let  be a predicate on attributes in the schema R ∪ S. The
theta join operation r s is defined as follows:
r ⋈ s =  (r × s)
Theta join produces a relation containing tuples which criteria satisfy  of Cartesian product
of R and S relation schema. Criteria can use relational operators (≤, <, =,>, ≥). Theta join
operation is an extension of natural join.

Example 1 :

Airport ⋈ Airport.airportId=Flight.depAirport Flight

Output ??
Theta-Join Operation (Cont.)

Example 2:
Employee Job_grade

Relational Algebra :
last_name, salary, gra (Employee ⋈salary>=lowest_sal ^ salary<=highest_sal Dept)
Outer Join Operation
The outer-join operation is an extension of the join operation to deal with
missing information. The outer join operation works in a manner similar
to the natural join operation, but preserves those tuples that have no
relationship with other relation (null values).
Example :
The result of operation outer of Employee and Dept relation is :
Employee
EmpId Name DeptId
Employee Dept
3415 Harry D01 EmpId Name Employee.DeptId Dept.DeptId DeptName

2241 Sally D02


3415 Harry D01 D01 Finance
3401 Goerge D03
2241 Sally D02 D02 Sales
2202 Harriet D02
3401 Goerge D03 Null Null
2202 Harriet D02 D02 Sales
Dept
DeptId DeptName All tuples in the left relation that did not match with any tuple in the right relation
D01 Finance
D02 Sales
Outer Join Operation
There are actually three forms of the outer operation namely left outer
join, denoted ; right outer join, denoted ; and full outer join,
denoted
Left Outer Join takes all tuples in the left relation that did not match with any tuple in the right relation

Right Outer Join takes all tuples in the right relation that did not match with any tuple in the left relation

Example of the Right Outer Join :


Employee
EmpId Name DeptId
3415 Harry D01 Employee Dept
2241 Sally D02 EmpId Name Employee.DeptId Dept.DeptId DeptName

3401 Goerge D03


3415 Harry D01 D01 Finance
2202 Harriet D02
2241 Sally D02 D02 Sales
Dept 3401 Goerge D03 D03 Production
DeptId DeptName
2202 Harriet D02 D02 Sales
D01 Finance
Null Null Null D04 Accounting
D02 Sales
D03 Production
All tuples in the right relation that did not match with any tuple in the left relation
D04 Accounting
Outer Join Operation
Example of the Full Outer Join :
Customer Query :
Id_cus Match all customers and suppliers by country
Name Relational Algebra :
City_cus  Name, Customer.Country, Supplier.Country,
Country CompanyName (Customer Supplier)
Phone_cus
Name CustomerCountry SupplierCountry CompanyName
NULL NULL Australia Pavlova, Ltd.
Right outer Join NULL NULL Australia G'day, Mate
Supplier
..... ..... ...... ......
Id_sup Simpson Argentina NULL NULL
Left outer Join Moncada Argentina NULL NULL
CompanyName ..... ..... ...... ......
ContactName Batista Brazil Brazil Refrescos Americanas LTDA
Carvalho Brazil Brazil Refrescos Americanas LTDA
City_sup Join Limeira Brazil Brazil Refrescos Americanas LTDA
Lincoln Canada Canada Ma Maison
Country Lincoln Canada Canada Forêts d'érables
Phone_sup
Assignment Operation
It is convenient at times to write a relational-algebra expression by
assigning parts of it to temporary relation variables. The assignment
operation, denoted by ←,works like assignment in a programming
language.

the illustrate of this operation is used to define the natural-join operation :


r s

With the assignment operation, a query can bewritten as a sequential


program consisting of a series of assignments followed by an expression
whose value is displayed as the result of the query.
Aggregation Operation
Aggregate functions take a collection of values and return a single value as
a result. Aggregate functions denoted by (calligraphic G).

Type of the aggregate function : sum( ), avg( ),min( ), max( ), count( ) .


Instructor
Example :

sum(salary) (Instructor) =?

avg(salary) (Instructor) =?

min(salary) (Instructor) =?

max(salary) (Instructor) =?

count(Name) (Instructor) =?

dept_name avg(salary( dept_name (instructor))

The general form of the aggregation operation G is


as follows :

where E is any relational-algebra expression; G1, G2, . . . , Gn constitute a list


of attributes on which to group; each F i is an aggregate function; and each
Ai is an attribute name.
Exercise
Schema diagram for the university database
SQL data definition for part of the university database :
create table department (dept_name varchar (20), building varchar (15), budget numeric (12,2), primary
key (dept_name));

create table course (course_id varchar (7), title varchar (50), dept_name varchar (20), credits numeric
(2,0), primary key (course id), foreign key (dept_name) references department);

create table instructor (ID varchar (5), name varchar (20) not null, dept_name varchar (20), salary
numeric (8,2), primary key (ID), foreign key (dept_name) references department(dept_name));

create table section (course_id varchar (8), sec_id varchar (8), semester varchar (6), year numeric (4,0),
building varchar (15), room_number varchar (7), time_slot_id varchar (4), primary key (course_id, sec_id,
semester, year), foreign key (course_id) references course (course_id));

create table teaches (ID varchar (5), course_id varchar (8), sec_id varchar (8), semester varchar (6), year
numeric (4,0), primary key (ID, course_id, sec_id, semester, year), foreign key (course_id, sec_id,
semester, year) references section (course_id, sec_id, semester, year), foreign key (ID) references
instructor (ID));

etc....
Department
The Tuples of some tables of the university database :
Instructor

Teaches

Course

Section

Student
Practical Task :
1. Create a university database that consists of tables such as the schema diagram above
(SQL data definition and tuples of some tables as shown above)
2. Please complete SQL data definition and tuples of some tables others
3. Fill the tuple of each table at least 10 tuples
4. Write the following queries in Relational Algebra and SQL :
1. Finds the names of all instructors in the History department
2. Finds the instructor ID and department name of all instructors associated with a
department with budget of greater than $95,000
3. Finds the names of all instructors in the Comp. Sci. department together with the
course titles of all the courses that the instructors teach
4. Find the names of all students who have taken the course title of “Game Design”.
5. For each department, find the maximum salary of instructors in that department. You
may assume that every department has at least one instructor.
6. Find the lowest, across all departments, of the per-department maximum salary
computed by the preceding query.
7. Find the ID and names of all students who do not have an advisor.

the practical task will evaluate at next week, Thursday, 09.30-12.00 WIB, H.6.12

You might also like