You are on page 1of 58

Formal Relational Query

Languages
BY: Richa Jain

1
Relational Algebra
• Relational algebra is a procedural query language.
• Relational algebra mainly provides a theoretical
foundation for relational databases and SQL. 
• Like any other mathematical system is defines a
number of operators and use relations (tables) as
operands.
•  It takes Relation as input and generates relation as
output.
• It uses various operation to perform this action.
Fundamental operations in Relational
Algebra
• Six basic operators
– select: 
– project: 
– union: 
– set difference: –
– Cartesian product: x
– rename: 
• The operators take one or two relations
as inputs and produce a new relation as a
result.
• Relation is a table with columns and rows.

3
• Select , project and rename operations are
called unary operations because they operate
on one relation
• Other three operations are called Binary
operations because they operate on pair of
relations

4
Fundamental operations in Relational
Algebra
• Six basic operators
– select: 
– project: 
– union: 
– set difference: –
– Cartesian product: x
– rename: 
• The operators take one or two relations
as inputs and produce a new relation as a
result.
• Relation is a table with columns and rows.

6
Project Operation
• Project operation is used to retrieve only a certain set of
attributes of a relation.
• Notation:  A , A ,…, A ( 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
• Duplicate rows removed from result, since relations are
sets.
• In simple words, If you want to see only the names all of
the students in the Student table, then you can use Project
Operation.
Project Operation
• Statement: Retrieve the name from Student table
• Query :name(STUDENT)
Select Operation
• Select operation selects tuples that satisfies a given predicate
• Notation:  p(r)
• p is called the selection predicate or condition

P is the prepositional logic where we specify the conditions that


must be satisfied by the data. In prepositional logic, one can use
relational operators like =, <, > etc, to specify the
conditions.Where p is a formula in propositional calculus
consisting of terms
connected by :  (and),  (or), 
(not)

9
• Statement: Select employees from EMPLOYEE table where age is 20.
• Query: σ AGE=20 (STUDENT)
Select Operation – Example
 Relation r

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

12
Project Operation – Example
• Relation r:

A,C (r)

13
Union Operation
• Notation: r  s
• Where R is the first relation and S is the second relation
• Two relations must be compatible
• 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 (example: 2nd column of r
deals with the same type of values as does the 2nd column of s).
Duplicate tuples are automatically eliminated.
• If relations don't have the same set of attributes, then the union of
such relations will result in NULL. 

14
Union Operation
• Statement: Find all names from student and employee table

• Query: ∏ Name (Studentt) ∪ ∏ Name(employee)


Union- Example

∏regno(R1) ∪ ∏regno(R2)

Union -Example
Union Operation – Example
• Relations r, s:

 r  s:

17
Set Difference Operation
• Notation r – s
• The set difference operations select all the tuples that are
present in first relation R but not in second relation R.
• 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

18
Set difference - Example
• List all customer names those who have a deposit account
but not valid loan.

•  Cust name(depositor)-  cust name (borrower)


Cust name Account no. Cust name Loan no. Cust name
Tom 101 Tom 1011 SMITH
AMY 502 RAMIT
AMY 5022
ROSE 123 JOHN 6023
JOHN 602 SMITH 5632
ROSE 7896
RAMIT 6532
Cartesian-Product Operation
• Cartesian product is fundamental operator in relational
algebra.
• Cartesian product combines information of two into
one.
• It is also called cross product.
• Generally, a Cartesian product is never a meaningful
operation when it is performed alone. However, it
becomes meaningful when it is followed by other
operations like select.
• Symbol: x
• Notation: R1*R2.

20
Cartesian-Product Operation

• Number of tuples
• Number of attributes
Cartesian-Product Operation –
Example
 Relations r, s:

 r x s:

22
Composition of Operations
• Can build expressions using multiple operations
• Example: A=C(r x s)

• rxs

• A=C(r x s)

23
Rename Operation
• The result of relational algebra are also relations but without any name.
• The rename operator is used to rename the output of a relation.
• Symbol: rho 

• Notation:  x (E)

returns the expression E under the name X


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

 x( A , A ,...,A (E)
)
1
2 n
returns the result of expression E under the name X, and with the attributes
renamed to A1 , A2 , …., An .

24
Rename operator -Example
Formal Definition
• A basic expression in the relational algebra consists of
either one of the following:
– A relation in the database
– A constant relation
• Let E1 and E2 be relational-algebra expressions;
the following are all relational-algebra expressions:
– E1  E2
– E1 – E2
– E1 x E2
– p (E1), P is a predicate on attributes in E1
– s(E1), S is a list consisting of some of the attributes in
E1
–  x (E1), x is the new name for the result of E1
26
Cartesian-Product Operation
• Cartesian product is fundamental operator in relational
algebra.
• Cartesian product combines information of two into
one.
• It is also called cross product.
• Generally, a Cartesian product is never a meaningful
operation when it is performed alone. However, it
becomes meaningful when it is followed by other
operations like select.
• Symbol: x
• Notation: R1*R2.

27
Cartesian-Product Operation

• Number of tuples
• Number of attributes
Cartesian-Product Operation –
Example
 Relations r, s:

 r x s:

29
Composition of Operations
• Can build expressions using multiple operations
• Example: A=C(r x s)

• rxs

• A=C(r x s)

30
Rename Operation
• The result of relational algebra are also relations but without any name.
• The rename operator is used to rename the output of a relation.
• Symbol: rho 

• Notation:  x (E)

returns the expression E under the name X


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

 x( A , A ,...,A (E)
)
1
2 n
returns the result of expression E under the name X, and with the attributes
renamed to A1 , A2 , …., An .

31
Rename operator -Example
Formal Definition
• A basic expression in the relational algebra consists of
either one of the following:
– A relation in the database
– A constant relation
• Let E1 and E2 be relational-algebra expressions;
the following are all relational-algebra expressions:
– E1  E2
– E1 – E2
– E1 x E2
– p (E1), P is a predicate on attributes in E1
– s(E1), S is a list consisting of some of the attributes in
E1
–  x (E1), x is the new name for the result of E1
33
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.
34
Division Operator
• Not supported as a primitive operator,
but useful for expressing queries like:
• Notation: A /B

35
Division operator -Example
Join Operation
Cartesian and Join product
Types of Join
Theta Join
• The theta join operation is a variant of the natural join
operation that allows us to combine a selection and a
Cartesian product into a single operation.
• θ in Theta join is the join condition. Theta joins combines
tuples from different relations provided they satisfy the
theta condition.
• The theta join operation r  s is defined as

– r  s =  (r x s)

Ai  Bj (A are attribute of R and Bj are attribute of S)

 = (<, >, =,<=,>=)

40
THETHA –EXAMPLE
(R A2>B1 S )
A1 A2 B1
20 25 30
30 40 50

A1 A2 B1
20 25 30
20 25 50
30 40 30
30 40 50
Natural-Join Operation
• A natural join is a binary operation that allows us to
combine certain selections and a Cartesian product
into one operation.
• NaturalJoin=cartesian product + selection+
projection
• Notation: r s
• The natural join operation forms a Cartesian
product of its two arguments, performs a selection
forcing equality on those attributes that appear in
both relational schemas, and finally removes
duplicate attributes. •
• Example: R = (A, B, C, D) S = (E, B, D)
• – Result schema = (A, B, C, D, E)
• – r s is defined as:
42
Natural Join
Natural Join Example

 courses HOD

44
Natural Join-Example
• Find the emp Names who is working in a department.

Emp no. Ename address Empno. Ename Deptno. Emp no.


1 Ram Delhi 1 Ram D1 1
2 Varun chd 1 Ram D2 2
3 Ravi chd 1 Ram D3 3
2 Varun D1 1
2 Varun D2 2
Dept no. Dept name Emp no. 2 Varun D3 3
3 Ravi D1 1
D1 Hr 1 3 Ravi D2 2
D2 IT 2 3 Ravi D3 3
D3 Marketing 3
Outer Join
• An extension of the join operation that avoids
loss of information.
• Computes the join and then adds tuples form one
relation that does 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

46
Left outer Join
Left outer join- Example
Right outer Join
Right outer Join-Example
Full outer Join
Full outer Join -Example
Set-Intersection Operation
• Notation: r  s
• Defined as:
• 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)
• Suppose that we wish to find all customers
who have both a loan and an account. Using
set intersection, we can write
Πcustomer-name (borrower ) ∩ Πcustomer-name (depositor)
53
Set-Intersection Operation – Example
• Relation r, s:

• rs
54
Outer Join – Example
• Relation instructor1
ID name dept_name
10101 Srinivasan Comp. Sci.
12121 Wu Finance
15151 Mozart Music

• Relation teaches1

ID course_id
10101 CS-101
12121 FIN-201
76766 BIO-101

55
• Join
Outer Join – Example
instructor
teaches
ID name dept_name course_id
10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201

• Left Outer Join


• instructor teaches
ID name dept_name course_id
10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
15151 Mozart Music null

56
Outer Join – Example
 Right Outer Join
instructor
teaches ID name dept_name course_id
10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
76766 null null BIO-101

 Full Outer Join


instructor teaches
ID name dept_name course_id
10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
15151 Mozart Music null
76766 null null BIO-101

57
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.
• Aggregate functions simply ignore null values (as in SQL)
• For duplicate elimination and grouping, null is treated like
any other value, and two nulls are assumed to be the
same (as in SQL)

58

You might also like