0% found this document useful (0 votes)
21 views6 pages

Relational Algebra Tutorial Two

The document discusses a tutorial on relational algebra and SQL. It includes sample tables from a property rental database and tasks students with writing queries in both relational algebra and SQL to retrieve information from the tables. It also provides the table schemas and asks students to write additional SQL queries to insert and aggregate data.

Uploaded by

Nomhle Mosimane
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)
21 views6 pages

Relational Algebra Tutorial Two

The document discusses a tutorial on relational algebra and SQL. It includes sample tables from a property rental database and tasks students with writing queries in both relational algebra and SQL to retrieve information from the tables. It also provides the table schemas and asks students to write additional SQL queries to insert and aggregate data.

Uploaded by

Nomhle Mosimane
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/ 6

Tutorial 2 - Relational Algebra and SQL

Part A

Appendix 1 contains a set of tables which make up a relational database.

Using the Appendix, produce answers to the following queries in relational algebra
(using whichever notation you prefer), writing your answers on paper. Then attempt
these questions using SQL and compare your SQL queries with your algebra
queries to ensure you understand fully the relational algebra.

You can obtain the SQL file to create these tables from Canvas. The file is called
property2015.sql. It is the same database that you used when working through the
SQL booklet from session one so you should have already loaded this database into
Oracle, but if you need to you should download it again and run in again in Oracle.

You can use either Oracle SQL Developer or Oracle APEX to complete this task.

N.B. You will need to THINK about these questions! Consider first of all the table(s)
concerned and what it is you are trying to retrieve. For example, if you are trying to
retrieve some, but not all of the columns, then which operation would this be? If you
are not sure, then you will need to refer back to your lecture notes, and may also
need to do some further reading. Once you have solutions in both the algebra and
SQL, evaluate your solutions to ensure you can understand how the algebra and
SQL queries compare.

1. List all staff with a salary greater than £15,000.

2. Produce a list of salaries for all staff, showing only the Staff number, the
forename, surname and salary details.

3. Produce a list of salaries for all staff who have a salary less than
£20,000, showing only the Staff number, the forename, surname and
salary details.

4. List all towns where there is either a branch office or a property for rent.

HINT: This is a more difficult question because to begin with the two tables
concerned are not union compatible. Therefore you have to first use the
projection operation based on the town attribute from the two tables (bearing
in mind that the two columns concerned have different names), i.e. you need
to PROJECT both tables and perform another operation.
Think about it!
As a starting point you can select all towns from each table separately to see
how many rows you should retrieve if your SQL query is correct.

5. Find the list of all towns where there is a branch but no owners resident
there.

6. List all towns where there is both a branch office and a property for rent.

N.B. There are two ways of writing the SQL query. Try to find both ways.

7. List the names and comments of all clients who have viewed a property
for rent.
Part B

Complete the following (parts (a) to (c) are in Oracle SQL only, i.e. there is no
need to write the algebra for this query):

(a) Create a new table. Call your table grades. It should contain the following
columns

name varchar2(10)
code varchar2(6)
mark number(3)

(b) Insert the following data into your table

name code mark


Jane com348 52
John com325 56
John com362 72
Mark com362 43
Mark com348 35
Mary com379 60

(c) Find, in a single query, the average mark for each student and the average
mark overall. Call the individual average mark ‘average’ and call the overall
average mark ‘total’. The resulting data should look like this:

NAME AVERAGE
---------- ----------
Jane 52
John 64
Mark 39
Mary 60
total 53

N.B. This is a not a difficult query but the SQL required goes beyond the SQL
that you have learned previously. You will need to look up how to group
results.

(d) You could use the relational algebra SUMMARIZE command to find the
average mark for each student (ignoring the overall average mark). Write on
paper the algebra query to do this.
APPENDIX 1 - PROPERTY RENTAL SERVICES
CASE STUDY

TENANT STAFF BRANCH

holds Carries out manages offers

VIEWING INSPECTION

Is held by undergoes Is managed by

PROP
LEASE FOR owns OWNER
RENT
Is owned by

associated with

PROP_TYPE
LEASE
Name Null? Type
------------------------------- -------- ----
LEASE_NO NOT NULL NUMBER(5)
*PROPERTY_NO NOT NULL NUMBER(5)
*TENANT_NO NOT NULL NUMBER(5)
RENT_PM NUMBER(6,2)
PAYMENT_METHOD NOT NULL CHAR(1)
DEPOSIT_AMOUNT NUMBER(6,2)
DEPOSIT_PAID CHAR(1)
STDATE NOT NULL DATE
ENDDATE DATE

VIEWING
Name Null? Type
------------------------------- -------- ----
PROPERTY_NO NOT NULL NUMBER(5)
TENANT_NO NOT NULL NUMBER(5)
DATE_VIEW DATE
COMMENTS VARCHAR2(50)

TENANT
Name Null? Type
------------------------------- -------- ----
TENANT_NO NOT NULL NUMBER(5)
T_SURNAME NOT NULL VARCHAR2(25)
T_FORENAMES NOT NULL VARCHAR2(25)
T_STREET NOT NULL VARCHAR2(25)
T_AREA VARCHAR2(20)
T_TOWN NOT NULL VARCHAR2(20)
T_POSTCODE NOT NULL VARCHAR2(8)
T_TELNO VARCHAR2(12)
T_PREF_TYPE CHAR(1)
T_MAX_RENT NUMBER(6,2)

INSPECTION
Name Null? Type
------------------------------- -------- ----
PROPERTY_NO NOT NULL NUMBER(5)
STAFF_NO NOT NULL NUMBER(5)
INSPECT_DATE NOT NULL DATE
COMMENTS VARCHAR2(50)

PROP_FOR_RENT
Name Null? Type
------------------------------- -------- ----
PROPERTY_NO NOT NULL NUMBER(5)
PROP_STREET NOT NULL VARCHAR2(25)
PROP_AREA VARCHAR2(20)
PROP_TOWN NOT NULL VARCHAR2(20)
PROP_PCODE NOT NULL VARCHAR2(8)
PROP_TYPE NOT NULL VARCHAR(1)
PROP_ROOMS NOT NULL NUMBER(2)
PROP_RENT_PM NOT NULL NUMBER(7,2)
PROP_POLL_TAX NUMBER(6,2)
*OWNER_NO NOT NULL NUMBER(5)
*STAFF_NO NUMBER(5)
*BRANCH_NO NOT NULL NUMBER(3)
AVAILABLE CHAR(1)
COMMENTS VARCHAR2(150)

PROP_TYPE
Name Null? Type
------------------------------- -------- ----
PROP_TYPE NOT NULL VARCHAR(1)
TYPE_DESC NOT NULL VARCHAR2(15)

OWNER
Name Null? Type
------------------------------- -------- ----
OWNER_NO NOT NULL NUMBER(5)
OW_SURNAME NOT NULL VARCHAR2(25)
OW_FORENAMES NOT NULL VARCHAR2(25)
OW_STREET NOT NULL VARCHAR2(25)
OW_AREA VARCHAR2(20)
OW_TOWN NOT NULL VARCHAR2(20)
OW_PCODE NOT NULL VARCHAR2(8)
OW_FEE NUMBER(6,2)

STAFF
Name Null? Type
------------------------------- -------- ----
STAFF_NO NOT NULL NUMBER(5)
*BRANCH_NO NUMBER(3)
STAFF_SURNAME NOT NULL VARCHAR2(25)
STAFF_FORENAMES NOT NULL VARCHAR2(25)
STAFF_STREET NOT NULL VARCHAR2(25)
STAFF_AREA VARCHAR2(20)
STAFF_TOWN NOT NULL VARCHAR2(20)
STAFF_PCODE NOT NULL VARCHAR2(8)
STAFF_TELNO VARCHAR2(12)
STAFF_GENDER CHAR(1)
STAFF_SALARY NUMBER(8,2)

BRANCH
Name Null? Type
------------------------------- -------- ----
BRANCH_NO NOT NULL NUMBER(3)
BR_STREET NOT NULL VARCHAR2(25)
BR_AREA VARCHAR2(20)
BR_TOWN NOT NULL VARCHAR2(20)
BR_PCODE NOT NULL VARCHAR2(8)
BR_TELNO VARCHAR2(12)

You might also like