Relational Algebra and SQL Overview
Relational Algebra and SQL Overview
Module-3 Overview
Relational Algebra
Unary Relational Operations
Relational Algebra Operations From Set Theory
Binary Relational Operations
Additional Relational Operations
Examples of Queries in Relational Algebra
Relational Calculus
Tuple Relational Calculus
Domain Relational Calculus
Query
Language
Procedural Non-Procedural
Query Language Query Language
Relational Relational
Algebra Calculus
Structure Query
Language
22-02-2025
Relational Algebra
Relational Algebra is a procedural query language which takes a relation as an
input and generates a relation as an output.
Relational Algebra is a language for expressing relational database queries.
It uses operators to perform queries. An operator can be either unary or binary.
Types of operators in relational algebra:
1. Basic/Fundamental Operators
2. Additional/Derived Operators
Relational algebra operations work on one or more relations to define another
relation without changing the original relations.
Relational algebra is performed recursively.
22-02-2025
Example
In relational algebra, input is a relation (table from which data has to be accessed) and
output is also a relation (a temporary table holding the data asked for by the user).
Relational algebra is performed recursively on a relation and intermediate results are also
considered relations.
σAge>17(Student)
Student
Roll_no Name Age Address
Query-3: Select Student whose age is greater
1 A 20 Bhopal than 17
2 B 17 Mumbai
3 C 16 Mumbai
4 D 19 Delhi
5 E 18 Delhi
Note: In Selection operation, schema of resulting relation is identical to schema of input relation
22-02-2025
Student
Roll_no Name Age Address
Query-3: Select Student whose age is greater
1 A 20 Bhopal than 17
2 B 17 Mumbai
3 C 16 Mumbai
σAge>17(Student)
4 D 19 Delhi
Roll_no Name Age Address
5 E 18 Delhi
1 A 20 Bhopal
Query-1: Select Student whose roll no is 2 4 D 19 Delhi
σRoll_no=2(Student) 5 E 18 Delhi
Roll_no Name Age Address Query-4: Select Student whose age is greater
2 B 17 Mumbai than 17 who lives in Delhi
σAge>17 ^ Address=“Delhi”(Student)
Query-2: Select Student whose name is D
σName=“D”(Student) Roll_no Name Age Address
Roll_no Name Age Address 4 D 19 Delhi
4 D 19 Delhi 5 E 18 Delhi
Note: In Selection operation, schema of resulting relation is identical to schema of input relation
Examples
Select tuples from a relation "Books" where subject is "database" and price is "450“
Select tuples from a relation "Books" where subject is "database" and price is "450" or have
a publication year after 2010
22-02-2025
Examples
Example: Display the columns roll_no and name from the relation Student.
Π roll no, name(Student)
22-02-2025
Set Operators
Set Operators
Set
Set Difference
Union (∪) Intersection
(-)
(∩)
Set Operators
Set operators: Union, intersection and difference, binary operators as they takes
two input relations
To use set operators on two relations,
The two relations must be Compatible
Two relations are Compatible if –
1. Both the relations must have same number of attributes (or columns).
2. Corresponding attribute (or column) have the same domain (or type).
Duplicate tuples are automatically eliminated
22-02-2025
1 2
2
2 3 1 4
3
3 4
R S RUS
Name
A Que-1: Find the names of the authors who have either written a
B book or an article or both.
C
D Πauthor(Books) U Πauthor(Articles)
G
H
22-02-2025
1 2
2
2 3 1 4
3
3 4
R S R∩S
Student Employee
(Student) ∩ (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 2 B
3 C 9 H
4 D Note: Union is
commutative: A ∩ B =
B∩A
Student Employee
(Student) ∩ (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 2 B
3 C 9 H
4 D Note: Union is
commutative: A ∩ B =
Πname(Student) ∩ Πname(Employee) B∩A
Name
B Que-1: Find the names of the authors who have written a book
and an article both.
Πauthor(Books) ∩ Πauthor(Articles)
22-02-2025
1 2
2
2 3 1 4
3
3 4
R S R-S
Symbol: -
Syntax: R-S
RA: R-S
SQL: SELECT * FROM R
MINUS
SELECT * FROM S;
22-02-2025
Student Employee
(Student) - (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 1 A
3 C 9 H 3 C
4 D 4 D
Note:
1. Set Difference is non-commutative: A-B ≠ B-A
2. R-(R-S) = R ∩ S
Intersection can be is derived from set difference that’s why intersection is derived operator.
Student Employee
(Student) - (Employee)
Roll_no Name Emp_no Name
1 A 2 B Roll_no Name
2 B 8 G 1 A
3 C 9 H 3 C
4 D 4 D
Note:
1. Set Difference is non-commutative: A-B ≠ B-A
2. R-(R-S) = R ∩ S
Intersection can be is derived from set difference that’s why intersection is derived operator.
Que-1: Find the names of the authors who have written books but not article.
Πauthor(Books) - Πauthor(Articles)
22-02-2025
R1 R2 R1 x R2
A1 A2 B1 B2 B3 A1 A2 B1 B2 B3
X 1 X 10 A X 1 X 10 A
Y 2 Y 10 A X 1 Y 10 A
Y 20 B X 1 Y 20 B
Z 10 B X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
22-02-2025
Characteristics
If relation R1 and R2 have a & b attributes respectively, then resulting relation will have a + b
attributes from both the input relations.
If relation R1 and R2 have nl & n2 tuples respectively, then resulting relation will have n1 x
n2 tuples, combining each possible pair of tuples from both the relations.
R1 R2 R1 X R2
Attributes a B a+b
Tuples n1 n2 n1 X n2
If both input relation have some attribute having same name, change the name of the
attribute with the name of the relation "relation_name.attribute_name"
If both input relation have some attribute having same name, change the name of the
attribute with the name of the relation "relation_name.attribute_name"
R1 R2 R1 x R2
A1 A2 A1 B2 B3 R1.A1 A2 R2.A1 B2 B3
X 1 X 10 A X 1 X 10 A
Y 2 Y 10 A X 1 Y 10 A
Y 20 B X 1 Y 20 B
Z 10 B X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
22-02-2025
Composition Operations
σA1=B1(R1 X R2)
R1 x R2
A1 A2 B1 B2 B3 A1 A2 B1 B2 B3
X 1 X 10 A X 1 X 10 A
Y 2 Y 10 A X 1 Y 10 A
Y 2 Y 20 B X 1 Y 20 B
X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
Composition Operations
ΠA1(σB2=20(R1 X R2))
R1 x R2
A1 A1 A2 B1 B2 B3
X X 1 X 10 A
Y X 1 Y 10 A
X 1 Y 20 B
X 1 Z 10 B
Y 2 X 10 A
Y 2 Y 10 A
Y 2 Y 20 B
Y 2 Z 10 B
22-02-2025
Rename Operators
The results of relational algebra are also relations but without any name.
The RENAME operator is used to rename the output of a relation.
Sometimes it is simple and suitable to break a complicated sequence of operations and
rename it as a relation with different names. Reasons to rename a relation can be many, like:
1. We may want to save the result of a relational algebra expression as a relation so that
we can use it later.
2. We may want to join (or cartesian product) a relation with itself, in that case, it
becomes too confusing to specify which one of the tables we are talking about, in
that case, we rename one of the tables and perform join operations on them.
Symbol: rho ρ
Notation 1: ρX(E)
Where the symbol ' ρ' is used to denote the RENAME operator and E is the result of
expression or sequence of operation which is saved with the name X
SQL: Use the AS keyword in the SELECT clause to rename attributes (columns)
(Eg: RollNo AS SNo renames RollNo to SNo)
SELECT column_name AS new_column_name
FROM tablename
WHERE condition
22-02-2025
Example:
Suppose we want to do Cartesian product between same table then one of the table should be
renamed with another name.
R X R (Ambiguity means confusion will be there) RxR
Rename Operation
Notation 1: ρX(A1,A2,….An)(E)
It returns the result of expression E under the name X, and with the attributes renamed
to A1, A2,.....A_{n}.
Notation 2: ρ(A1,A2,….An)(E)
It returns the result of expression E with the attributes renamed to A1, A2, ...., An.
22-02-2025
Example
Example-1: Query to find the female students from Student relation and rename the
relation Student as Female Student and the attributes of Student RollNo, SName as Sno,
Name.
Student FemaleStudent
RollNo Sname Gender SNo Name
1 Neha F 1 Neha
Rename to
2 Swati F 2 Swati
3 Rohan M 4 Puja
4 Puja F
5 Rahul M
Example
Example-1: Query to find the female students from Student relation and rename the
relation Student as Female Student and the attributes of Student RollNo, SName as Sno,
Name.
Student FemaleStudent
RollNo Sname Gender SNo Name
1 Neha F 1 Neha
Rename to
2 Swati F 2 Swati
3 Rohan M 4 Puja
4 Puja F
5 Rahul M
ρFemaleStudent(Sno,Name)(ΠRollNo,Sname(σGender=‘F’(Student)))
22-02-2025
Examples
Example-2: Query to rename the attributes Name, Age of table Person to N, A.
Example-3: Query to rename the table name Project to Work and its attributes to P, Q,
R.
Example-4: Query to rename the first attribute of the table Student with attributes A, B,
C to P.
Examples
Example-2: Query to rename the attributes Name, Age of table Person to N, A.
ρ (N,A)(Person)
Example-3: Query to rename the table name Project to Work and its attributes to P, Q,
R.
ρ Work(P,Q,R)(Project)
Example-4: Query to rename the first attribute of the table Student with attributes A, B,
C. From A to P.
ρ (P,B,C)(Student)
Example-5: Query to rename the table name Loan to L.
ρ L(Loan)
22-02-2025
σbranch-name=“Perryridge”(loan) L-11
L-14
Round Hill
Downtown
900
1500
loan L-15 Perryridge 1500
loan-number branch-name amount L-16 Perryridge 1300
L-15 Perryridge 1500 Output L-17 Downtown 1000
L-16 Perryridge 1300 L-23 Redwood 2000
L-93 Mianus 500
Πcustomer-name(σcustomer-city=“Harrison”(Customer)) Curry
Glenn
North
Sand Hill
Rye
Woodside
Green Walnut Stamford
Hayes Main Harrison
customer Johnson Alma Palo alto
customer- Jones Main Harrison
name Lindsay Park Pittsfield
Output
Hayes Smith North Rye
Jones Turner Putnam Stamford
Williams Nassau Princeton
Πcustomer-name(borrower) ∩ Πcustomer-name(depositor)
customer-name
branch (branch-name, branch-city, assets)
Hayes customer (customer-name, customer-street, customer-city)
Output
Jones account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
Smith depositor (customer-name, account-number)
borrower (customer-name, loan-number)
22-02-2025
Πcustomer-name(depositor) - Πcustomer-name(borrower)
customer-name branch (branch-name, branch-city, assets)
Lindsay customer (customer-name, customer-street, customer-city)
Output account (account-number, branch-name, balance)
Johnson loan (loan-number, branch-name, amount)
Turner depositor (customer-name, account-number)
borrower (customer-name, loan-number)
Πcustomer-name (σbranch-name=“Perryridge”
(σ[Link]-number=[Link]-number
(borrower x loan)))
customer-name
Output
Adams
Hayes
customer-name
Output
Adams
Strategy:
1. Find those balances that are not largest (as a temporary relation).
• Rename account relation as d so that we can compare each account balance with all others
Π[Link](σ[Link]<[Link](account x ρd(account)))
2. Use set difference to find those account balances that were not found in the earlier step.
• Take set difference between relation Πbalance(account) and temporary relation just computed,
to obtain the result
22-02-2025
Introduction
Cartesian product of two relations (A × B), gives us all the possible tuples that are
paired together.
But it might not be feasible in certain cases to take a Cartesian product
where we encounter huge relations with thousands of tuples having a
considerable large number of attributes.
22-02-2025
Join Operation( )
Join is an Additional / Derived operator which simplify the queries, but does not
add any new power to the basic relational algebra.
Join is a combination of a Cartesian product followed by a selection process.
Join = Cartesian Product + Selection
A Join operation pairs two tuples from different relations, if and only if a given
join condition is satisfied.
Symbol: ⋈
A ⋈c B=σc(ΑΧΒ)
Difference
Cross Join/Cross Product/Cartesian
Joins (⋈) Product (X)
• Combination of tuples that satisfy • All possible combination of tuples
the filtering/matching conditions from the relations
• Fewer tuples then cross product, • Huge number of tuples and costly
might be able to compute efficiently to manage
RXS
R ⋈ S (Natural Join)
R S
A R.B S.B C
A B C A B B C 1 a a 3
1 a 3 Output 1 a a 3 Output 1 a b 4
2 b 4 2 b a 3
2 b b 4
2 b b 4
22-02-2025
Types of JOINS
JOINS
Types of JOINS
1. Inner Join 2. Outer Join
Contains only those tuples that satisfy the Contains matching tuples that satisfy
matching condition the matching condition, along with some
Theta(θ) / Conditional Join or all tuples that do not satisfy the
matching Condition
A ⋈θ B
uses all kinds of comparison operators(<,>, <=, Contains all rows from either one or
>=,=,≠) both relation
Equi Join Left Outer Join
Special case of theta join Left relation tuples will always be in result
uses only equality (=) comparison operator whether the value is matched or not
Natural join Right Outer Join
A⋈B Right relation tuples will always be in result
whether the value is matched or not
Based on common attributes in both relation
does not use any comparison operator Full Outer Join
Tuples from both relations are present in
result, whether the value is matched or not
22-02-2025
Inner Join
An Inner join includes only those tuples that satisfy the matching criteria, while the
rest of tuples are excluded.
Theta Join, Equi join, and Natural Join are called inner joins.
S1 [Link]<[Link] R1
[Link] sname rating age [Link] bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.0 58 103 11/12/96
Join:- S1 [Link]<[Link] R1
SQL:-
22-02-2025
2. Equi Join
When a theta join uses only equivalence (=) condition, it becomes a Equi join.
Equi join is a special case of theta (or conditional) join where condition contains equalities
(=).
S1 [Link]=[Link] R1
[Link] sname rating age [Link] bid day
22 dustin 7 45.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96
Join: S1 [Link]=[Link] R1
SQL :
22-02-2025
3. Natural Join
Natural join can only be performed if there is at least one common attribute (column) that
exist between two relations. In addition, the attributes must have the same name and
domain.
Natural join does not use any comparison operator.
It is same as equi join which occurs implicitly by comparing all the common attributes
(columns) in both relation, but difference is that in Natural Join the common attributes
appears only once. The resulting schema will change.
Notation: A ⋈ B
The result of the natural join is the set of all combinations of tuples in two relations A and B
that are equal on their common attribute names.
22-02-2025
3. Natural Join…
The Natural Join of two relations can be obtained by applying a Projection operation to Equi
join of two relations. In terms of basic operators:
R=(A,B,C,D) S=(B,D,E)
Resulting Schema of R ⋈ S =(A,B,C,D,E)
S1 R1
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
Join: S1 R1
SQL:
Equivalent to:
Π CID, Course,[Link],Head(σ[Link]=[Link](Courses X HOD))
22-02-2025
Natural Join
Natural join can only be performed if there is at least one common attribute
(column) that exist between two relations. In addition, the attributes must have the
same name and domain.
In Natural Join resulting relation the common attributes appears only once.
Notation: A ⋈ B (Natural join does not use any comparison operator)
The result of the natural join is the set of all combinations of tuples in two relations
A and B that are equal on their common attribute names.
The Natural Join of two relations can be obtained by applying a projection operation
to equi join of two relations. And in terms of basic operators:
Πcustomer-name,[Link]-number,amount(σ[Link]-number=[Link]-number(borrowerX
loan))
Using Natural Join
Πcustomer-name(borrower ⋈ depositor))
Πcustomer-name(borrower) ∩ Πcustomer-name(depositor)
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
22-02-2025
Πcustomer-name(σbranch-name=“Perryridge”(borrower⋈loan))
-
Πcustomer-name (depositor) branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
22-02-2025
Outer Join
An Inner join includes only those tuples with matching attributes and the rest are
discarded in the resulting relation. Therefore, we need to use outer joins to include
all the rest of the tuples from the participating relations in the resulting relation.
The outer join operation is an extension of the join operation that avoids loss of
information.
Outer Join contains matching tuples that satisfy the matching condition, along with
some or all tuples that do not satisfy the matching condition.
It is based on both matched or unmatched tuple.
It contains all rows from either one or both relations are present
It uses NULL values.
NULL signifies that the value is unknown or does not exist
Outer Join
Outer Join = Natural Join + Extra information (from left table, right table or both table)
In Full outer join, all the tuples from both Left relation R1 and right relation R2 are
included in the resulting relation. The tuples of both relations R1 and R2 which do
not satisfy join condition, their respective unmatched attributes are made NULL.
In short:
All record from all table
Symbol: ⟗
Notation: R1 ⟗ R2
22-02-2025
A 1 1 output A
÷
B 2 2
A 2
D 4
22-02-2025
Example
Example 2:
Example
Example 2:
22-02-2025
Idea:
For A/B, compute all x values that are not 'disqualified' by some y value in B.
x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A.
Disqualified x values : Π X((Π X(A) X B)-A)
A B A÷B
So X Y Y X
A/B = Π X(A)- all Disqualified tuples 1 output
A 1 ÷ A
A/B = Π X(A)- Π X((Π X(A) X B)-A) B 2 2
A 2
D 4
S2 P2 S1 P4 S2 P1
Assignment Operator
The assignment operation (←) provides a convenient way to express complex queries.
It writes 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.
Division Operator: A/B = Π X(A)- Π X((Π X(A) X B)-A)
Write A/B as :
Temp1 ← Π X(A)
Temp2 ← Π X((Temp1 X B)-A)
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.
Relational Calculus
Relational calculus is a non-procedural query language(or declarative language).
It uses mathematical predicate calculus(or first-order logic instead of algebra.)
Relational calculus tells what to do but never explain how to do.
Relational calculus provides description about the query to get the result where as Relational
algebra gives the method to get the result.
When applied to database, It comes in two flavors:
1. Tuple Relational Calculus(TRC):
Proposed by codd in the year 1972.
Works on tuples(or rows) like SQL.
2. Domain Relational Calculus(DRC):
Proposed by lacroix & pirotte in the year 1972.
Works on domain of attributes (or columns).
22-02-2025
Relational Calculus
Calculus has variable constraints, comparison operator, logical connectives and quantifiers.
TRC: Variables range over tuples.
Like SQL
DRC: Variables range over domain element.
Like Query-By-Example(QBE)
Expressions in the calculus are called formulas.
Resulting tuple is an assignment of constants to variables that make the formula evaluate to
true.
It selects all the tuples t from relation loan such that the resulting loan tuples will have amount greater than $1200
Find the loan number for each loan of an amount greater than $1200
{t | ∃ s ∈ loan (t[loan-number] = s[loan-number] ∧ s[amount]> 1200)} (Projection)
It selects the set of tuples t such that there exists a tuple s in relation loan for which the values of t & s for the
loan – number attribute are equal and the value of s for the amount attribute is greater than $1200.
branch(branch-name, branch-city, assets)
customer(customer-name, customer-street, customer-city)
account(account-number, branch-name, balance)
loan(loan-number, branch-name, amount)
depositor(customer-name, account-number)
borrower(customer-name, loan-number)
22-02-2025
Find the loan number for each loan of an amount greater than $1200: