You are on page 1of 5

RELATIONAL ALGEBRA EXAMPLES

1. A supermarket chain uses the following relations in its


database:

Supermarket (Shop_no, shop_name, location,


manager_number)

Manager (manager_number, manager_name, salary)

Give relational algebra solutions for the following queries:

(i) Obtain the shop details for all shops based in


Newcastle.

Select Supermarket where location = Newcastle


giving result

(ii) Obtain the shop name and location of all those shops
where the shop manager earns $20,000 or more.

Select manager where salary ≥ $20,000 giving T1

Join T1, Supermarket over manager_no giving T2

Project T2 over shop_name, location giving result


(iii) Obtain the shop details for all shops not based in
Newcastle or Durham

Select Supermarket where location = Newcastle or


location = Durham giving T1

Supermarket – T1 giving result (the difference


operator)

alternative solution

Select Supermarket where location = Newcastle


giving T1

Select Supermarket where location = Durham giving


T2

Union T1 and T2 giving T3

Supermarket – T3 giving result


2. Consider the following tables:

Record (Record_no, name, artiste)

Borrower (Borrower_no, name, address)

Loan (Record_no, Borrower_no)

Produce Relational Algebra commands to satisfy the


following queries:

(i) Get the numbers and names of all records borrowed


by Borrower no 12.

Select loan where borrower_no = 12 giving T1

Join T1, Record over record_no giving T2

Project T2 over record_no, name giving result

(ii) Get the names and address of the borrowers who


have on loan a record by Cliff Richards (i.e. Cliff
Richards is the artiste).

Select record where artiste = Cliff Richards giving T1

Join T1, Loan over record_no giving T2

Join T2, Borrower over borrower_no giving T3

Project T3 over borrower.name, address


(iii) Get the names and addresses of borrowers who do
not have any records on loan.

Project Loan over borrower_no giving T1

Project Borrower over borrower_no giving T2

T2 – T1 giving T3

Join T3, Borrower over borrower_no giving T4

Project T4 over name, address giving result


3. A relational database contains the following tables:

Department (deptno, dname, location)

Employee (empno, ename, job, mgr_empno, hiredate,


sal, deptno)

Give relational algebra solutions for the following:

(i) Obtain employee name, salary and department


number where job = manager.

Select Employee where job = Manager giving T1

Project T1 over ename, sal, deptno giving result

(ii) Obtain employee name, department name and


location for employees managed by a manager
earning above $20,000.

Select Employee where job = Manager and sal >


20,000 giving T1

Join T1, Employee over T1.empno and


employee.mgr_empno giving T2

Join T2, Department over deptno giving T3

Project T3 over ename, dname, location giving result

You might also like