Formal Relational Query Language Part 2
Formal Relational Query Language Part 2
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:
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
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 :
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
Right Outer Join takes all tuples in the right relation that did not match with any tuple in the left relation
sum(salary) (Instructor) =?
avg(salary) (Instructor) =?
min(salary) (Instructor) =?
max(salary) (Instructor) =?
count(Name) (Instructor) =?
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