Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 75

RELATIONAL ALGEBRA

04/25/2024 1
Relational Model
 The relational Model of Data is based on the concept of a Relation.

 A Relation is a mathematical concept based on the ideas of sets.

 The strength of the relational approach to data management comes


from the formal foundation provided by the theory of relations.

 The model was first proposed by Dr. E.F. Codd of IBM in 1970 in the
following paper:
"A Relational Model for Large Shared Data Banks," Communications of
the ACM, June 1970

04/25/2024 2
Relation
RELATION: A table of values
 A relation may be thought of as a set of rows, alternately as a set of
columns.

 Each row represents a fact that corresponds to a real-world entity or


relationship.

 Each row has a value of an item or set of items that uniquely identifies
that row in the table.

 Sometimes row-ids or sequential numbers are assigned to identify the


rows in the table.

 Each column typically is called by its column name or column header or


attribute name.

04/25/2024 3
Schema & Domain
The Schema of a Relation: R (A1, A2, .....An)
Relation schema R is defined over attributes A1, A2, .....An

For Example -
CUSTOMER (Cust-id, Cust-name, Address, Phone_no)

Here, CUSTOMER is a relation defined over the four attributes


Cust-id, Cust-name, Address, Phone_no, each of which has a
domain or a set of valid values. For example, the domain of Cust-
id is 6 digit numbers.

04/25/2024 4
Tuple
A tuple is an ordered set of values.

Each value is derived from an appropriate domain.

Each row in the CUSTOMER table may be referred to as a


tuple in the table and would consist of four values.
 <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000">
is a tuple belonging to the CUSTOMER relation.

A relation may be regarded as a set of tuples (rows).

Columns in a table are also called attributes of the relation.

04/25/2024 5
Summary
Informal Terms Formal Terms

Table Relation
Column name Attribute

Row Tuple
Values in a column Domain

Table Definition Schema of a Relation

Populated Table Extension

04/25/2024 6
Relational Algebra
Basic set of operations for the relational model to enable a
user to specify basic retrieval requests.

The result of a retrieval is a new relation. The algebra


operations thus produce new relations, which can be further
manipulated using operations of the same algebra.

A sequence of relational algebra operations forms a relational


algebra expression, whose result will also be a relation that
represents the result of a database query (or retrieval request).

04/25/2024 7
Relational Algebra
 Procedural language

 Six basic operators


 select
 project
 union
 set difference
 Cartesian product
 Rename

 The operators take one or more relations as inputs and give a new
relation as a result.
Select operation
 SELECT operation is used to select a subset of the
tuples from a relation that satisfy a selection condition
specified on attributes. It is a filter that keeps only those tuples
that satisfy a qualifying condition.

 Defined as: p(r) = {t | t  r and p(t)}


p is called selection predicate, where p is a formula consisting of
terms connected by :  (and),  (or),  (not)

Each term is one of:


<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <. 

04/25/2024 9
Select Operation
loan
Loan_number Branch_name amount
L-10 Kolkata 200
L-11 Delhi 900
L-12 Chennai 1300
L-13 Mumbai 1500
L-15 Kolkata 1000

amount > 1200(loan)


loan

Loan_number Branch_name amount

L-12 Chennai 1300


L-13 Mumbai 1500
Select Operation – Example
• Relation r A B C D

  1 7
  5 7
  12 3
  23 10

• A=B ^ D > 5 (r)


A B C D

  1 7
  23 10
Select operation
SELECT Operation Properties
 The SELECT operation  <selection condition>(R) produces
a relation S that has the same schema as R

 The SELECT operation  is commutative; i.e.,

 <condition1>( < condition2> ( R)) =  <condition2> ( <


condition1> ( R))

A cascaded SELECT operation may be applied in any


order; i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition2> ( < condition3> ( < condition1> ( R)))
04/25/2024 12
Select operation
A cascaded SELECT operation may be replaced by a
single selection with a conjunction of all the conditions;
i.e.,
 <condition1>( < condition2> ( <condition3> ( R))
=  <condition1> AND < condition2> AND < condition3> ( R)))

04/25/2024 13
Solve:
Select Operation
Find out the loans which are given from kolkata branch.
 Ans-  branch_name =“Kolkata” (loan)
Find out the loans for amount greater than 1200 and they are given
from kolkata branch.
 Ans- amount >1200 ^ branch_name=“Kolkata” (loan)
loan
Loan_number Branch_name amount

L-10 Kolkata 200


L-11 Delhi 900
L-12 Chennai 1300
L-13 Mumbai 1500
L-15 Kolkata 2000
Project Operation
This operation selects certain columns from the table and
discards the other columns. The PROJECT creates a vertical
partitioning – one with the needed columns (attributes)
containing results of the operation and other containing the
discarded Columns.

 Notation:
A1, A2, …, Ak (r)
where A1, A2 are attribute names and r is a relation name.

 The result is defined as the relation of k columns obtained by


erasing the columns that are not listed
Project Operation – Example
Relation r: A B C

α 10 1
β 20 1
β 30 1
β 40 2

 ∏ (r) A C A C
A,C

α 1 α 1
β 1 = β 1
β 1 β 2
β 2
Project Operation
 Duplicate rows removed from result, since relations
are sets

 E.g. To eliminate the branch-name attribute of


loan
loan-number, balance (loan)

04/25/2024 17
Project Operation
loan
Loan_number Branch_name amount
L-10 Kolkata 200
L-11 Delhi 900
L-12 Chennai 1300
L-13 Mumbai 1500
L-15 Kolkata 2000
loan-number, balance (loan)
loan
Loan_number amount
L-10 200
L-11 900
L-12 1300
L-13 1500
L-15 2000
Union Operation
The result of this operation, denoted by R  S, is a relation that
includes all tuples that are either in R or in S or in both R and S.
Duplicate tuples are eliminated.
Defined as: R  S = {t | t  R or t  S}

For R  S to be valid.
1. R, S must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of R deals with the same type of values as does the 2nd
column of S)

 The resulting relation for R1R2 has the same attribute names as
the first operand relation R1 (by convention).
Union Operation – Example
 Relations r, s:
A B A1 B1

α 1 α 2
α 2 β 3
β 1 s
r

r  s: A B

α 1
α 2
β 1
β 3
Union Operation
Depositor
customer_ account_nu
name mber
customer_name
Johnson A-10
Johnson A-11 Johnson
Jones A-12 Jones
Smith A-13
Smith
Lindsay A-15
Lindsay
Borrower
Jackson
c_name loan_numbe
r Adams
Johnson L-10
Jackson L-11
Jones L-12 customer-name (depositor)  customer-name
Smith L-13 (borrower)
Lindsay L-15 (find the names of all customers with
Adams L-18
Union Example
Example: Suppose the employee relation schema is:
EMPLOYEE (NAME, SSN, SUPERSSN, DNO)
To retrieve the social security numbers of all employees
who either work in department 5 or directly supervise an
employee who works in department 5, we can use the union
operation as follows:

DEP5_EMPS  DNO=5 (EMPLOYEE)


RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)   SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2

04/25/2024 22
Set-Intersection Operation
Defined as : The result of this operation, denoted by R  S, is
a relation that includes all tuples that are in both R and S.

R  S ={ t | t  R and t  S }

Assume:
R, S have the same arity
attributes of r and s are compatible

Note: R  S = R - (R - S)

04/25/2024 23
Set-Intersection Operation -
Example
A B A B
Relation r, s:  1  2
 2  3
r  1
s

r  s A B

 2

04/25/2024 24
Example
Find the names of celebrities who are instructors.
Celebrity Instructors
First_name Last_name F_name L_name
Jyoti Basu Shubhas Bose
Sourav Ganguly Jyoti Basu
Poulami Ghatak Rajib Gandhi
Mamata Banerjee
Mamata Banerjee
Rahul Gandhi
Rahul Gandhi

Celebrity
Ans : Celebrity  Instructors
First_name Last_name
Jyoti Basu
Mamata Banerjee
Rahul Gandhi
04/25/2024 25
Set Difference Operation
The result of this operation, denoted by R - S, is a relation that
includes all tuples that are in R but not in S.

Defined as:
R– S = {t | t  R and t  S}

Set differences must be taken between compatible relations.


 R and S must have the same arity
 attribute domains of r and s must be compatible
Set Difference Operation – Example
 Relations r, s:
A B A B

α 1 α 2
α 2 β 3
β 1 s
r

r - s: A B

α 1
β 1
Example
Find the names of celebrities who are not instructors.
Celebrity Instructors
First_name Last_name F_name L_name
Jyoti Basu Shubhas Bose
Sourav Ganguly Jyoti Basu
Poulami Ghatak Rajib Gandhi
Mamata Banerjee
Mamata Banerjee
Rahul Gandhi
Rahul Gandhi

Ans : Celebrity - Instructors Celebrity


First_name Last_name
Sourav Ganguly
Poulami Ghatak

04/25/2024 28
Example
Find the names of instructors who are not celebrities.
Celebrity Instructors
First_name Last_name F_name L_name
Jyoti Basu Shubhas Bose
Sourav Ganguly Jyoti Basu
Poulami Ghatak Rajib Gandhi
Mamata Banerjee
Mamata Banerjee
Rahul Gandhi
Rahul Gandhi

Ans : Instructors-celebrity Persons


First_name Last_name
Shubhas Bose
Rsjib Gandhi
04/25/2024 29
Relational Algebra Operations From
Set Theory (cont.)
Notice that both union and intersection are commutative
operations; that is
R  S = S  R, and R  S = S  R
Both union and intersection can be treated as n-ary operations
applicable to any number of relations as both are associative
operations; that is
R  (S  T) = (R  S)  T, and (R  S)  T = R  (S  T)

The minus operation is not commutative; that is, in general


R-S≠S–R
04/25/2024 30
Cartesian-Product Operation
Notation R x S
Defined as:
R x S = {t q | t  R and q  S}

Assume that attributes of R(r) and S(s) are disjoint. (That


is,
r s = ).

If attributes of R(r) and S(s) are not disjoint, then


renaming must be used.
Cartesian-Product Operation-
Example
Relations R, S: A B C D E

α 1 α 10 a
β 10 a
β 2 β 20 b
r γ 10 b
s
R x S:
A B C D E

α 1 α 10 a
α 1 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
Composition of Operations
Can build expressions using multiple operations
Example: A=C(r x s)
r x s A B C D E
 1  10 a
 1  10 a
 1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b

A B C D E
A=C(r x s)
 1  10 a
 2  20 a
 2  20 b
Rename Operation
Allows us to name, and therefore to refer to, the results of
relational-algebra expressions.

Allows us to refer to a relation by more than one name.

• Example: x (E), returns the expression E under the name X

• If a relational-algebra expression E has arity n, then


x (A1, A2, …, An) (E), returns the result of expression E under the
name X, and with the attributes renamed to A1, A2, …., An.
Banking Example
branch (bname, bcity, asset)

customer (cname, cstreet, conly)

account (accno, bname, blnc)

loan (loan-no, bname, amnt)

depositor (cname, accn0)

borrower (cname, loan-no)


Example Queries
1. Find all loans of over $1200

amnt > 1200 (loan)

2. Find the loan number for each loan of an amount


greater than $1200

loan-no (amnt > 1200 (loan))

04/25/2024 36
Example Queries
3.Find the names of all customers who have a loan,
an account, or both, from the bank
cname (borrower)  cname (depositor)

4.Find the names of all customers who have a loan


and an account at bank.

cname (borrower)  cname (depositor)

04/25/2024 37
Example Queries
Find the names of all customers who have a loan at the kolkata
branch.
- Query 1
cname(bname = “Kolkata”

( borrower.loan-no = loan.loan-no(borrower x loan) ) )

 Query 2

cname(loan.loan-no = borrower.loan-no

(bname = “Kolkata”(loan)) x borrower) )

04/25/2024 38
Example Queries
Find the names of all customers who have a loan at the
Bangalore branch but do not have an account at any
branch of the bank.

cname (bname = “Bangalore”

(borrower.loan-no= loan.loan-no(borrower x loan) ) )

– cname(depositor)

04/25/2024 39
Example Queries
Find the largest account balance.

The query is:

blnc(account) –

account.blnc (account.blnc < d.blnc (account x


rd(account)))

04/25/2024 40
Additional Operations

We define additional operations that do not add any power


to the
relational algebra, but that simplify common queries.

 Join
Division
Assignment

04/25/2024 41
Join operation
Definition:
The sequence of cartesian product followed by select
operation is used quite commonly to identify and select
related tuples from two relations, is a special operation, called
JOIN.

It allows us to process relationships among relations.

The general form of a join operation on two relations R(A 1,


A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition> S
where R and S can be any relations.

04/25/2024 42
Join Operation
Form of join condition: Ai Ѳ Bj
where i) Ai is an attribute of R and Bj is an attribute
of S, have same domain and
ii) Ѳ is one of the comparison operators {=, ≠,
≤, ≥, <, >}

The join operation with such a general join condition is called


THETA (Ѳ) JOIN.

Tuples whose attributes are null do not appear in the result.

Join does not preserve all information of participating relation .


04/25/2024 43
Example
Suppose, we have two tables DEPARTMENT and EMPLOYEE.
The schemas are:
DEPARTMENT (DNAME, DNUMBER, MGRSSN)
EMPLOYEE (NAME, SSN, SUPERSSN, DNO)
we want to retrieve the name of the manager of each department.

 To get the manager’s name, we need to combine each


DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value
matches the MGRSSN value in the department tuple. We do this by
using the join operation.

DEPT_MGR  DEPARTMENT MGRSSN=SSN


EMPLOYEE
Result  ∏name (DEPT_MGR)
04/25/2024 44
Equi Join
 EQUIJOIN Operation
The most common use of join involves join conditions with equality
comparisons only. Such a join, where the only comparison operator
used is =, is called an EQUIJOIN.

In the result of an EQUIJOIN we always have one or more pairs of


attributes (whose names need not be identical) that have identical
values in every tuple.

04/25/2024 45
NATURAL JOIN Operation
Because one of each pair of attributes with identical values is
superfluous, a new operation called NATURAL join, was created to
get rid of the second (superfluous) attribute in an equi join condition.

Denoted by: R*S

The standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have
the same name in both relations. If this is not the case, a renaming
operation is applied first.

04/25/2024 46
Example
 DEPARTMENT (DNAME, DNUMBER, MGRSSN)
EMPLOYEE (NAME, SSN, SUPERSSN, DNO)
From these two tables we want to retrieve the name of the manager
of each department using natural join.

Solution:
1. Rename MGRSSN to SSN.
2. Apply natural join operation.

DEPT   DNAME, DNUMBER, SSN (DEPARTMENT)


MANAGERS  DEPT * EMPLOYEE

04/25/2024 47
Outer Join
An extension of the join operation that avoids loss of
information.

Computes the natural join and then adds tuples form one
relation that do not match tuples in the other relation to the
result of the join.

Uses null values:


null signifies that the value is unknown or does not exist
All comparisons involving null are (roughly speaking) false
by definition.
Left outer join
 Takes all the tuples in the left relation that did not match with
any tuple in the right relation
and
 Pads the tuples with null values for all other attributes from
the right relation.
and
 Adds them to the result of natural join.

All info to the left relation is present in the result of left outer
join.

04/25/2024 49
Inner Join – Example
Relation loan loan-numberbranch-nameamount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer-nameloan-number
Jones L-170
Smith L-230
Hayes L-155

 Inner Join : loan * Borrower

loan-number branch-name amountcustomer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
Left Outer Join – Example
Relation loan loan-numberbranch-nameamount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer-nameloan-number
Jones L-170
Smith L-230
Hayes L-155

 Left Outer Join : loan Borrower

loan-number branch-name amountcustomer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
Right Outer Join – Example
Relation loan loan-numberbranch-nameamount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer-nameloan-number
Jones L-170
Smith L-230
Hayes L-155

 Right Outer Join : loan Borrower

loan-number branch-name amountcustomer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes
Full Outer Join – Example
Relation loan loan-numberbranch-nameamount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer-nameloan-number
Jones L-170
Smith L-230
Hayes L-155

 Full Outer Join : loan Borrower


loan-number branch-name amountcustomer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes
Null Values
 It is possible for tuples to have a null value, denoted by null, for
some of their attributes

 null signifies an unknown value or that a value does not exist.

 The result of any arithmetic expression involving null is null.

 Project operation treats the null as other values.

 Aggregate functions simply ignore null values.

 For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be the same
Null Values
 Comparisons with null values return the special truth value unknown
 Three-valued logic using the truth value unknown:
 OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown

 AND: (true and unknown) = unknown,


(false and unknown) = false,
(unknown and unknown) = unknown

 NOT: (not unknown) = unknown

 In SQL “P is unknown” evaluates to true if predicate P evaluates


to unknown

 Result of select predicate is treated as false if it evaluates to unknown


Division Operation
RS
 The division operation is applied to two relations
R(Z) ÷S(X), where X subset Z.

 Let Y = Z – X that is, let Y be the set of attributes of


R that are not attributes of S.

 Division can be expressed as a sequence of


Π(project), × (Cartesian product), – (difference)
operations as follows:
1. R1 Π Y (R)
2. R2 Π Y ((S × R1)-R)
3. Result  R1 – R2
04/25/2024 56
Division Operation – Example
Relations R, S:
A B B
 1
1
 2
 3 2
 1 S
 1
R  1
 3
 4
 6
 1
 2 R A
S:

04/25/2024 57
Another Division Example
Relations r, s:
A B C D E D E

 a  a 1 a 1
 a  a 1 b 1
 a  b 1 s
 a  a 1
 a  b 3
 a  a 1
 a  b 1
 a  b 1
r

r  s: A B C

 a 
 a 

04/25/2024 58
Division Operation-Query
 Retrieve the names of employees who works on all the
projects that ‘John Smith’ works on. Given schemas are:
1. Employee( Fname, Lname, ssn, Addr, salary, bdate)
2. Project( pname, pno, ploc, dno)
3. Works-on( ssn, pno, hours)

 Solution:

John  σ Fname=“John” AND Lname=“Smith”(Employee)


J-projectΠ pno (John * Works-on)
Work  Π pno, ssn (Works-on)
R Work ÷ J-project
ResultΠ Fname, Lname(R* Employee)
04/25/2024 59
Assignment Operation
 The assignment operation () provides a convenient way to express
complex queries.
 Write query as a sequential program consisting of
 a series of assignments
 followed by an expression whose value is displayed as a result of
the query.
 Assignment must always be made to a temporary relation variable.
 Example: Write r  s as

temp1  R-S (r)


temp2  R-S ((temp1 x s) – R-S,S (r))
result = temp1 – temp2
 The result to the right of the  is assigned to the relation variable on

the left of the .


 May use variable in subsequent expressions.

04/25/2024 60
Banking Example
branch (branch-name, branch-city, assets)

account (account-number, branch-name,


balance)

depositor (customer-name, account-number)

borrower (customer-name, loan-number)


Example Queries
Find all customers who have an account from at least the
“Downtown” and the Uptown” branches.
Query 1

CN(BN=“Downtown”(depositor account)) 

CN(BN=“Uptown”(depositor account))

where CN denotes customer-name and BN denotes


branch-name.

Query 2

customer-name, branch-name (depositor


account)
 temp(branch-name) ({(“Downtown”),
(“Uptown”)})
04/25/2024 62
Example Queries
 Find all customers names who have an account at all branches
located in Bangalore city.

customer-name, branch-name (depositor * account)


 branch-name (branch-city = “Bangalore” (branch))

04/25/2024 63
Extended Relational-Algebra-
Operations
Generalized Projection
Aggregate Functions

04/25/2024 64
Generalized Projection
 Extends the projection operation by allowing arithmetic functions to
be used in the projection list.

 F1, F2, …, Fn(E)

 E is any relational-algebra expression

 Each of F1, F2, …, Fn are are arithmetic expressions involving


constants and attributes in the schema of E.

 Given relation credit-info(customer-name, limit, credit-balance), find


how much more each person can spend:
customer-name, limit – credit-balance (credit-info)

04/25/2024 65
Aggregate Functions and
Operations
 Aggregation function takes a collection of values and returns a single
value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
 Aggregate operation in relational algebra

G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)


 E is any relational-algebra expression
 G1, G2 …, Gn is a list of attributes on which to group (can be empty)
 Each Fi is an aggregate function
 Each Ai is an attribute name

04/25/2024 66
Aggregate Operation – Example
 Relation r: A B C

  7
  7
  3
  10

sum-C
g sum(c) (r)
27

04/25/2024 67
Aggregate Operation – Example
Relation account grouped by branch-name:

branch-nameaccount-number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch-name g sum(balance) (account)


branch-name balance
Perryridge 1300
Brighton 1500
Redwood 700
04/25/2024 68
Aggregate Functions (Cont.)

Result of aggregation does not have a name


Can use rename operation to give it a name

branch-name g sum(balance) as sum-balance (account)


For convenience, we permit renaming as part of aggregate
operation
branch-nameSum-balance
Perryridge 1300
Brighton 1500
Redwood 700

04/25/2024 69
Aggregation
branch-nameaccount-number balance
Perryridge A-102 400
Perryridge A-201 900
Relation schema Brighton A-217 750
Account Brighton A-215 750
Redwood A-222 700

1. g count-distinct(branch-name) (account)

Ans: 3
2. g max(balance) Max-balance
(account)
900
Ans:
04/25/2024 70
Problem Set 1:
Consider the following relational database , where the primary keys are
underlined.
 employee (person-name, street, city)
 works (person-name, company-name, salary)
 company (company-name, city)
 manages (person-name, manager-name)

 Give an expression in the relational algebra to express each of the


following queries:
a. Find the names of all employees who work for First Bank Corporation.
b. Find the names and cities of residence of all employees who work for First
Bank Corporation.
c. Find the names, street address, and cities of residence of all employees
who work for First Bank Corporation and earn more than $10,000 per
annum.
d. Find the names of all employees in this database who live in the same city
as the company for which they work. 71
 e. Find the names of all employees who live in the same city and on the
same street as do their managers.
 f. Find the names of all employees in this database who do not work for
First Bank Corporation.
 g. Find the names of all employees who earn more than every
employee of Small Bank Corporation.
 h. Assume the companies may be located in several cities. Find all
companies located in every city in which Small Bank Corporation is
located.

04/25/2024 72
Consider the above relational database. Give a relational-algebra
expression for each of the following queries:
a. Modify the database so that Jones now lives in Newtown.
b. Give all employees of First Bank Corporation a 10 percent salary raise.
c. Give all managers in this database a 10 percent salary raise.
d. Give all managers in this database a 10 percent salary raise, unless the
salary
would be greater than $100,000. In such cases, give only a 3 percent raise.
e. Delete all tuples in the works relation for employees of Small Bank
Corporation.
f. Find the company with the most employees.
g. Find the company with the smallest payroll.
h. Find those companies whose employees earn a higher salary, on average,
than the average salary at First Bank Corporation.
g. Find the accounts held by more than two customers using aggregate
function.

04/25/2024 73
PROBLEM SET -2
Specify the following queries on the COMPANY
relational database schema given below.

Employee(Fname,Minit,Lname,SSn,Bdate,Address,Se
x,Salary,Super_ssn,Dno)
Department(Dname,Dnumber,Mgrssn,Mgr_startdate
)
Dept_location(Dnumber,Dlocation)
Project(Pname, Pnumber, Plocation,Dnum)
Works_on(essn,pno,hours)
Dependent(essn,dependent_ name, sex, bdate,
Relationship)

04/25/2024 74
(a) Retrieve the names of employees in department 5 who work more
than 10 hours per week on the 'ProductX' project.
(b) List the names of employees who have a dependent with the same first
name as
themselves.
(c) Find the names of employees that are directly supervised by 'Franklin
Wong'.
(d) For each project, list the project name and the total hours per week
(by all employees) spent on that project.
(e) Retrieve the names of employees who work on every project.
(f) Retrieve the names of employees who do not work on any project.
(g) For each department, retrieve the department name, and the average
salary of employees working in that department.
(h) Retrieve the average salary of all female employees.
(i) Find the names and addresses of employees who work on at least one
project located in Houston but whose department has no location in
Houston.
(j) List the last names of department managers who have no dependents.
04/25/2024 75

You might also like