Relational Query Languages
Relational Algebra (procedural)
Relational Calculus (non-procedural)
1
Relational Languages
• Relational Algebra (procedural)
– defines operations on tables
• Relational Calculus (declarative)
– based on first-order predicate calculus
• Every relational algebra query can be translated to relational
calculus
• Every safe relational calculus query can be translated to relational
algebra.
• Any language that is at least as expressive as relational algebra is
said to be relationally complete.
Relational Algebra Operators
• Select: given a relation R and a predicate P, select tuples from R that
satisfy P.
• Project: given a relation
R and a subset of its attributes X, return
a relation which is the same as R except that all columns not in X are
left out.
• Rename: given a relation R and a name N, return a relation that is
exactly the same as R except that it has a name N.
• Cartesian Product: Given 2 relations R1and R2,.return a relation
R3 whose tuples are the concatenation of tuples in R1 and R2
• Union: Given relations R1 and R2, return a relation R3 which
contains all tuples in R1 and R2
• Set Difference: Given relations R1 and R2, return a relation R3
containing all tuples in R1 that are not in R2
3
Selection Operation
•
sselection cond(R) or select[selection cond]R
• Example:
Employee
Employee(name, dept, sal)
select [sal > 20,000] Employee
sal
name dept
sal
name
dept
jane
pharmacy
30,000
jack
hardware
30,000
jill
pharmacy
75,000
joe
toy
20,000
bill
toy
12,000
jane
jack
jill
pharmacy 30,000
hardware 30,000
pharmacy 75,000
select [(dept = toy) or (sal < 20,000)]
Employee
name
joe
bill
dept
toy
toy
sal
20,000
12,000
4
Projection
•
Proj [list of attr of R] (R ) or P
R
A
B
C
Jane Toy
10,000
Jim
Toy
20,000
June
Complaint 20,000
Proj[A]R
A
Jane
Jim
June
list of attr of R (R)
S A
C
Jane Toy
John Complaint
Proj[CB]R
C
10,000
20,000
20,000
B
Toy
Complaint
Toy
5
Cartesian Product
• Denoted by R x S
R:
A
joe
jack
B
toy
com
C
10K
20K
S:
A
joe
jack
RxS:
R.A
joe
joe
jack
jack
B
toy
toy
com
com
C
10K
10K
20K
20K
S.A
joe
jack
joe
jack
D
jill
jill
jill
jill
D
jill
jill
attributes get the name,
R.A, where A is attrib
name, and R is the
relation name from which
attrib originates. If there is
no possible ambiguity,
relation name is dropped!
• Notice attribute naming strategy to disambiguate attribute names
6
Set Difference
• Denoted by R - S
( Illegal if R & S have different numbers of attributes or if respective
domains mismatch!)
R
A
B
S
Jane Toy
Jim
Toy
June
Complaint
R-S=
A
Jim
June
A
C
Jane Toy
John Complaint
B
Toy
Complaint
Note attributes in resulting relation take name from the first relation
7
Rename Operator
• Strategy used to disambiguate attribute names:
– For union and set difference operators, the resulting relation takes the
attribute names of the first relation
– For cartesian product, attributes are named as Relationname.attribute-name, where Relation name refers to the relation from
which the attribute originally came.
– Strategy will not disambiguate when the same relation appears
multiple times in the relational query.
• Let R(A,B) be a relation. Consider R x R
the attributes of the resulting relation?
• Define a rename operator:
• denoted by rename[N]R or by r N(R)
---- what to name
– returns a relation which is exactly same as R except it has the name N
8
Rename Operator
•
•
•
•
•
•
•
Consider relation Employee(name, dept, sal)
List all the employees who work in the same department as Jill
We first find the department(s) for which Jill works
– Proj[dept](select[name = Jill] Employee) ---list of departments for
which Jill works
To find out about all Employees working for this department, we need to
reference the Employee table again:
– select[P] ( Employee x Proj[dept](select[name = Jill] Employee) )
– where P is a selection predicate which requires dept values to be
equal.
If we use Employee.dept in P it is ambiguous which instance of Employee
relation in the query the attribute refers to.
To disambiguate, use rename operator:
Proj[Employee.name](select[Employee.dept = Employee2.dept]
Employee x (Proj[dept] (select[name = Jill](
9
rename[Employee2](Employee))))
Formal Definition of Relational Algebra
• Basic Expressions:
– Relation in a database
– Constant Relations
• General Expressions: constructed from basic ones. Let E1 and E2
be relational algebra expressons. Then the following are also
expressions:
– E1 U E2, if arity of E1 = E2 and corresponding attributes are of the
same type
– E1 - E2, if arity of E1 = E2 and corresponding attributes are of the
same type
– E1 x E2, if attributes in E1 and E2 have different names
– Select[P](E1), where P is a predicate on attributes in E1
– Proj[S](E1), where S is a list of some attributes in E1
– rename[X](E1), where X is a new name for relation E1
10
Additional Operators
Basic Relational Algebra operators are like assembly
language.
Define more powerful operators that make the task of writing
relational algebra queries easier
Each of these operators can be expressed in relational
algebra and do not increase the expressibility of the
language
Example: Intersection
R S = R - (R - S)
= { t | t R & t S }
11
Joins
R
join condition S = select[ join condition] (R x S)
join condition is of the form:
<condition> AND <condition> AND <condition>
where each condiition is of the form Ai q Bj, where
– Ai is attribute of R
– Bj is attribute of S
– q is a comparison operator {=, <, >, <=, >=, <>}
Example:
E(emp, dept)
M(dept, mgr)
List all employees and their managers.
Proj[emp, mgr](select[E.dept = M.dept] (ExM))
can be represented as:
Proj[emp,mgr] ( E
E.dept = M.dept M )
12
Types of Joins
• Theta-Join: if a join condition uses some comparison operator other
than equality.
– E.g., list names of all managers who manage departments other than
Jill’s
– Proj[mgr]( select[emp = Jill](E )
(E.dept M.dept) M)
• Equi-Join: if join conditions use only equality operator.
– E.g., list the manager’s name of Jill’s department
– Proj[mgr]( select[emp = Jill](E )
(E.dept = M.dept) M)
• Natural Join: special type of equi-join..
– Let R and S be relations. Let attributes of R and S be denoted by R
and S respectively.
– Denote by R U S the union of the list of attributes of R and S
– Let list of attributes common to both R and S be {A1, A2, …, An}
– Natural join of R and S (denoted R
S) is:
– Proj[R U S ] (R
R.A1 = S.A1 and R.A2 = S.A2 and … and R.An = S.An S)
– E.g., Proj[mgr]( select[emp = Jill](E )
M)
13
Assignment Operator
• Lots of time convenient to write relational algebra expressions in
parts using assignment to temporary relational variables.
• For this purpose use assignment operator, denoted by :=
• E.g., Who makes more than their manager?
E(emp, dept, sal)
M(mgr, dept)
ESM(emp, sal, mgr) := Proj[emp, sal, mgr] (E M)
(Proj[ESM.emp](ESM [mgr = E.emp & ESM.sal >E.sal] E) )
• With the assignment operator, a query can be written as a
sequential program consisting of a series of assignments followed
by an expression whose value is the result of the query.
14
Examples
• A query is a composition of basic relational algebra operators
• Consider relations:
– customer(ssno, name, street, city)
– account(acctno, custid, balance)
• list account balance of Sharad
balance
(s
custid= ssno
(account customer))
Diag
Pat
Winslett
Liu
Harandi
Harandi
Liu
Outcome Pat
Test
Winslett a
Winslett b
Liu
b
Harandi
f
Winslett e
Harandi
e
Harandi
g
Winslett
e
Dis
Strep
Mono
Meningitis
Hepatitis
Hepatitis
Outcome
T
F
T
T
F
F
F
T
ToDiag
Dis
Test
Strep
A
Mono
B
Meningitis
C
Hepatitis
D
Encehhalitis E
Meningitis
F
Meningitis
G
16
1. Who has what disease?
Diag
2. Who has a disease they have been tested for?
– Proj[pat](Diag | | ToDiag | | Outcome)
3. Who has a disease they tested positively for?
Proj[pat](Diag | | ToDiag | | (select[outcome = ‘T’])Outcome))
4. Who has a disease that they tested both positively & negatively for?
Temp1(pat, dis) := Proj[pat,dis](Diag | | ToDiag
select[outcome = ‘T’])Outcome)
Temp2(pat, dis) : = Proj[pat,dis](Diag | | ToDiag
select[outcome = ‘T’])Outcome)
Proj[pat](Temp1 Temp2)
Use better names!!
17
Example of Queries in Relational Algebra
5. Who tested both positively and negatively for a disease, whether or not they
have it?
Testpos(pat, dis) = Proj[pat,dis](ToDiag | | select[outcome = ‘T’])
Outcome)
Testneg(pat, dis) = Proj[pat,dis](ToDiag | | select[outcome = ‘T’])
Outcome)
(Testpos Testneg)[pat]
6. Who tested both positively & negatively for the same test? (Winslett)
Proj[pat](Outcome | | condition (rename[Outcome2](Outcome))
where condition is:
[Outcome.pat = Outcome2.pat & Outcome.test = Outcome2.test &
Outcome.outcome = Outcome2. outcome]
18
7. What testable disease does no one have?
(encephalitis)
Proj[dis]ToDiag - Proj[dis]Diag
Note technique: compute opposite of what you want, take a
difference. Use in hard queries with negation (‘no one’)
8. What disease does more than one person have?
Proj[dis](Diag
condition rename[Diag2](Diag))
where, condition is [Diag.pat Diag2.pat & Diag.dis = Diag2dis]
9. What disease does everyone have?
clue that query is very hard
Disease(dis) := diag[dis]
Patients(pat) := diag[pat]
DiseasesNotEveryoneHas(dis) := Proj[dis]((Patients x Disease) Diag)
Disease - Diseases Not Everyone Has
Note technique used! A very hard query might require taking the
difference several times.
19
© Copyright 2026 Paperzz