SlideShare a Scribd company logo
RELATIONAL ALGEBRA
Shyama Barna Bhattacharya
Lecturer
Department of Computer
Science and Engineering
•Relation schema
–Named relation defined by a set of attribute and
domain name pairs.
•Relational database schema
–Set of relation schemas, each with a distinct name.
•Each tuple is distinct; there are no duplicate tuples.
•Order of attributes has no significance.
•Order of tuples has no significance, theoretically.
•Relation name is distinct from all other relation names
in relational schema.
•Each cell of relation contains exactly one atomic
(single) value.
•Each attribute has a distinct name.
•Values of an attribute are all from the same domain.
•Each tuple is distinct; there are no duplicate tuples.
•Order of attributes has no significance.
•Order of tuples has no significance, theoretically.
Database Scheme/schema
A relational database scheme, or schema,
corresponds to a set of table definitions.
Eg: product(p_id, name, category, description)
supply(p_id, s_id, qnty_per_month)
supplier(s_id, name, address, ph#)
* remember the difference between a DB instance and
a DB scheme.
Difference Between Schema and
Instance
Schema Instance
It is the overall description of the database.
It is the collection of information stored in a database at a
particular moment.
The schema is same for the whole database.
Data in instances can be changed using addition, deletion, and
updation.
Does not change Frequently. Changes Frequently.
Defines the basic structure of the database i.e. how the data will
be stored in the database.
It is the set of Information stored at a particular time.
“Schema” and “Instance” are key ideas in a database management system (DBMS) that
help organize and manage data. Let’s begin by examining their distinctions from one
another.
Instances
An Instance is the state of an operational database with data at any given time. It contains
a snapshot of the database. The instances can be changed by certain CRUD operations,
such as like addition, and deletion of data. It may be noted that any search query will not
make any kind of changes in the instances.
Example of instances:
Let’s say a table teacher in our database whose name is School, suppose the table has 50
records so the instance of the database has 50 records for now and tomorrow we are
going to add another fifty records so tomorrow the instance has a total of 100 records.
This is called an instance.
Schema
Schema is the overall description of the database. The basic structure of how the data will
be stored in the database is called schema.
SCHEMA (**Already discussed in
class**)
Example:
Let’s say a table teacher in our database named school, the teacher table requires the name, dob, and doj in their table
so we design a structure as:
Teacher table
name: String
doj: date
dob: date
Above given is the schema of the table teacher.
Schema
Schema is the overall description of the database. The basic structure of how the
data will be stored in the database is called schema.
SCHEMA
Schema is of three types: Logical Schema,
Physical Schema and view Schema.
Logical Schema – It describes the database
designed at a logical level.
Physical Schema – It describes the database
designed at the physical level.
View Schema – It defines the design of the
database at the view level.
SAMPLE SCHEMAS AND INSTANCES
The Schemas:
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
The Instances:
Introduction
• one of the two formal query languages of
the relational model
• collection of operators for manipulating
relations
• Operators: two types of operators
– Set Operators: Union(),Intersection(),
Difference(-), Cartesian Product (x)
– New Operators: Select (), Project (), Join
(⋈)
Introduction – cont’d
• A Relational Algebra Expression: a
sequence of relational algebra operators
and operands (relations), formed
according to a set of rules.
• The result of evaluating a relational
algebra expression is a relation.
What is Relational Algebra?
• Relational algebra is a procedural query language.
• It consists of the select, project, union, set
difference, Cartesian product, and rename
operations.
• Set intersection, division, natural join, and
assignment combine the fundamental operations.
• SQL is based on relational algebra
•Relational algebra and relational
calculus are formal languages
associated with the relational
model.
•Both are equivalent to one another
• It is an abstract language. We use it to express
the set of operations that any relational query
language must perform.
• Two types of operations:
• 1.set-theoretic operations: tables are essentially
sets of rows
• 2.native relational operations: focus on the
structure of the rows Query languages are
specialized languages for asking questions,or
queries,that involve the data in database.
What are the query languages ?
Query languages
• procedural vs. non-procedural
• commercial languages have some of both
• we will study:
– relational algebra (which is procedural, i.e. tells
you how to process a query)
– relational calculus (which is non-procedural i.e.
tells what you want)
Selection Operator
•The select operation selects tuples that
satisfy a given predicate.
•It is denoted by sigma (σ).
Notation: σ p(r)
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors
like: AND OR and NOT. These relational can use as relational operators
like =, ≠, ≥, <, >, ≤.
Example of Selection
Input:
1.σ BRANCH_NAME="perryride" (LOAN)
BRANCH_NAME LOAN_NO AMOUNT
Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
Example of Selection
Output:
BRANCH_NAME LOAN_NO AMOUNT
Perryride L-15 1500
Perryride L-16 1300
SEMANTICS OF THE SAMPLE RELATIONS
• Sailors: Entity set; lists the relevant properties of sailors.
• Boats: Entity set; lists the relevant properties of boats.
• Reserves: Relationship set: links sailors and boats by describing the
boat number and date for which a sailor made a reservation.
Example of the declarative sentences for which rows stand:
Row 1: “Sailor ’22’ reserved boat number ‘101’ on 10/10/98”.
Selection and Projection
• Selection Operator: σrating>8 (S2)
Retrieves from the current instance of relation named S2 those rows
where the value of the attribute ‘rating’ is greater than 8.
Applying the above selection operator to the sample instance of S2
shown in figure 4.2 yields the relational instance on figure 4.4 as
shown below:
Select operators only select rows from relation.
• π
condition
Projection operator
•This operator shows the list of those
attributes that we wish to appear in the
result. Rest of the attributes are
eliminated from the table.
•It is denoted by ∏(pie).
•It is used to choose all columns from
relation, deletes unwanted columns for
relation.
Projection Operator πsname,rating(S2)
Retrieves from the current instance of the relation named S2 those
columns whose names are ‘sname’ and ‘rating’.
Applying the above operator to the sample instance of S2 shown in figure
4.2 yields the relational instance on figure 4.5 as shown below:
- Projection Operator (cont’d)
Similarly πage(S2) yields the following relational instance
Note here the elimination of
duplicates
SQL would yield
For πage (S2):
age
35.0
55.0
35.0
35.0
Selection
• Denoted by c(R)
• Selects the tuples (rows) from a relation R
that satisfy a certain selection condition c.
• It is a unary operator
• The resulting relation has the same
attributes as those in R.
Example 1:
SNO SNAME AGE STATE
S1 MIKE 21 IL
S2 STEVE 20 LA
S3 MARY 18 CA
S4 MING 19 NY
S5 OLGA 21 NY
S:
 state=‘IL’(S)
Example 2:
CNO CNAME CREDIT DEPT
C1
Databas
e
3 CS
C2
Statistic
s
3 MATH
C3 Tennis 1 SPORTS
C4 Violin 4 MUSIC
C5 Golf 2 SPORTS
C6 Piano 5 MUSIC
C:
 CREDIT  3(C)
Example 3
SNO CNO Grade
S1 C1 90
S1 C2 80
S1 C3 75
S1 C4 70
S1 C5 100
S1 C6 60
S2 C1 90
S2 C2 80
S3 C2 90
S4 C2 80
S4 C4 85
S4 C5 100
E:
SNO=‘S1’and CNO=‘C1’(E)
Selection - Properties
• Selection Operator is commutative
C1(C2 (R)) = C2(C1 (R))
• The Selection is an unary operator, it
cannot be used to select tuples from more
than one relations.
Projection
• Denoted by L(R), where L is list of attribute names and
R is a relation name or some other relational algebra
expression.
• The resulting relation has only those attributes of R
specified in L.
• The projection is also an unary operation.
• Duplicate rows are not permitted in relational algebra.
Duplication is removed from the result.
• Duplicate rows can occur in SQL, though they may be
controlled by explicit keywords.
Projection - Example
• Example 1: STATE (S)
SNO SNAME AGE STATE
S1 MIKE 21 IL
S2 STEVE 20 LA
S3 MARY 18 CA
S4 MING 19 NY
S5 OLGA 21 NY
STATE
IL
LA
CA
NY
Projection - Example
Example 2: CNAME, DEPT(C)
CNO CNAME CREDIT DEPT
C1
Databas
e
3 CS
C2
Statistic
s
3 MATH
C3 Tennis 1 SPORTS
C4 Violin 4 MUSIC
C5 Golf 2 SPORTS
C6 Piano 5 MUSIC
CNAME DEPT
Databas
e
CS
Statistic
s
MATH
Tennis SPORTS
Violin MUSIC
Golf SPORTS
Piano MUSIC
Projection - Example
Example 3: S#(STATE=‘NY'(S))
SNO SNAME AGE STATE
S1 MIKE 21 IL
S2 STEVE 20 LA
S3 MARY 18 CA
S4 MING 19 NY
S5 OLGA 21 NY
SNO
S4
S5
SET Operations
• UNION: R1  R2
• INTERSECTION: R1  R2
• DIFFERENCE: R1 - R2
• CARTESIAN PRODUCT: R1  R2
Union Compatibility
• For operators , , -, the operand
relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of
attributes, and the domains of the
corresponding attributes must be
compatible; that is, dom(Ai)=dom(Bi) for
i=1,2,...,n.
• The resulting relation for , , or - has the
same attribute names as the first operand
relation R1 (by convention).
Union Compatibility - Examples
• Are S(SNO, SNAME, AGE, STATE) and
C(CNO, CNAME, CREDIT, DEPT) union
compatible?
• Are S(S#, SNAME, AGE, STATE) and
C(CNO, CNAME, CREDIT_HOURS,
DEPT_NAME) union compatible?
UNION, SET DIFFERENCE & SET
INTERSECT
• Union puts all tuples of two relations in one relation. To use this operator,
two conditions must hold:
1. The two relations must be of the same arity.
2. The domain of ith attribute of the two participating relation must be
the same.
• Set difference operator computes tuples that are in one relation, but not
in another.
• Set intersect operator computes tuples that are common in two relations:
• The five fundamental operations of the relational algebra are: select,
project, cartesian product, Union, and set difference
• All other operators can be constructed using these operators
EXAMPLE
• Assume a database with the following three
relations:
Sailors (sid, sname, rating)
Boats (bid, bname, color)
Reserve (sid, bid, date)
• Query 1: Find the bid of red colored boats:
EXAMPLE
• Assume a database with the following three relations:
Sailors (sid, sname, rating)
Boats (bid, bname, color)
Reserve (sid, bid, date)
• Query 1: Find the bid of red colored boats:
– ∏bid(бcolor=red(Boats))
EXAMPLE
• Assume a database with the following three
relations:
Sailors (sid, sname, rating)
Boats (bid, bname, color)
Reserve (sid, bid, date)
• Query 1: Find the name of sailors who have
reserved Boat number 2.
EXAMPLE
• Assume a database with the following three relations:
Sailors (sid, sname, rating)
Boats (bid, bname, color)
Reserve (sid, bid, date)
• Query 1: Find the name of sailors who have reserved
Boat number 2.
– ∏sname(бbid=2(Sailors (sid)Reserve))
EXAMPLE
• Assume a database with the following three relations:
Sailors (sid, sname, rating)
Boats (bid, bname, color)
Reserve (sid, bid, date)
• Query 1: Find the name of sailors who have reserved
both a red and a green boat.
Union, Intersection, Difference
• T= R U S : A tuple t is in relation T if and
only if t is in relation R or t is in relation S
• T = R  S: A tuple t is in relation T if and
only if t is in both relations R and S
• T= R - S :A tuple t is in relation T if and
only if t is in R but not in S
Set-Intersection
• Denoted by the symbol .
• Results in a relation that contains only the
tuples that appear in both relations.
• R  S = R – (R – S)
• Since set-intersection can be written in terms
of set-difference, it is not a fundamental
operation.
Examples
A1 A2
1 Red
3 White
4 green
B1 B2
3 White
2 Blue
R S
Examples
A1 A2
1 Red
3 White
4 Green
2 Blue
A1 A2
3 White
R S
R  S
S - R
B1 B2
2 Blue
A1 A2
1 Red
4 Green
R - S
RENAME OPERATOR
• Rename operator changes the name of its
input table to its subscript,
– ρe2(Emp)
– Changes the name of Emp table to e2
RELATIONAL ALGEBRA INTRODUCTION
• Assume the following two relations:
Emp (SS#, name, age, salary, dno)
Dept (dno, dname, floor, mgrSS#)
• Relational algebra is a procedural query language, i.e., user must define
both “how” and “what” to retrieve.
• Relational algebra consists of a set of operators that consume either one
or two relations as input. An operator produces one relation as its output.
• Unary operators include: select, project, and rename
• Binary operators include: cartesian product, equality join, natural join,
join, semi-join, division, union, and set difference.
SELECT OPERATOR
• Select (б): selects tuples that satisfy a predicate; e.g., retrieve the
employees whose salary is 30,000
бSalary=30,000(Employee)
• Conjunctive ( ) and disjunctive ( ) selection predicates are allowed;
e.g., retrieve employees whose salary is higher than 30,000 and are
younger than 25 years old:
бSalary>30,000 age<25(Employee)
• Note that only selection predicates are allowed. A selection predicate
is either (1) a comparison (=, ≠, ≤, ≥, <, >) between an attribute and a
constant (e.g., salary = 30,000) or (2) a comparison between two
different attributes of the same relation (e.g., salary = age × 100).
• Note: This operator is different than the SELECT command of SQL.
<
<
<
EXAMPLE
• Emp table:
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
EXAMPLE
• Emp table:
• бSalary=30,000(Employee)
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
EXAMPLE
• Emp table:
• бSalary=30,000(Employee)
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
SS# Name Age Salary dno
4 Kathy 30 30000 5
EXAMPLE
• Emp table:
• бAge>22(Employee)
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
EXAMPLE
• Emp table:
• бAge>22(Employee)
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
SS# Name Age Salary dno
1 Joe 24 20000 2
4 Kathy 30 30000 5
PROJECT OPERATOR
• Project (∏) retrieves a column. It is a unary operator
that eliminate duplicates.
e.g., name of employees:
∏ name(Employee)
e.g., name of employees earning more than 30,000:
∏ name(бSalary>30,000(Employee))
EXAMPLE
• Emp table:
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
EXAMPLE
• Emp table:
• ∏ age(Emp)
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
Age
24
20
22
30
4
EXAMPLE
• Emp table:
• ∏ name,age(бSalary=4000 (Emp) )
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
EXAMPLE
• Emp table:
• ∏ name,age(бSalary=4000 (Emp) )
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
SS# Name Age Salary dno
5 Shideh 4 4000 1
EXAMPLE
• Emp table:
• ∏ name,age(бSalary=4000 (Emp) )
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 3
3 Bob 22 27000 4
4 Kathy 30 30000 5
5 Shideh 4 4000 1
Name Age
Shideh 4
CARTESIAN PRODUCT
• Cartesian Product (R1 × R2) combines two relations
by concatenating their tuples together, evaluating all
possible combinations. If the name of a column is
identical for two relations, this ambiguity is resolved
by attaching the name of each relation to a column.
e.g., Emp × Dept
– (SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor,
mgrSS#)
• If t(Emp) and t(Dept) is the cardinality of the
Employee and Dept relations respectively, then the
cardinality of Emp × Dept is: t(Emp) × t(Dept)
CARTESIAN PRODUCT (Cont…)
• Example:
Emp table:
Dept table:
345 John Doe 23 25,000 1
943 Jane Java 25 28,000 2
876 Joe SQL 22 32,000 1
SS# Name age salary dno
1 Toy 1 345
2 Shoe 2 943
dno dname floor mgrSS#
CARTESIAN PRODUCT (Cont…)
• Cartesian product of Emp and Dept: Emp × Dept:
34
5
John
Doe
23 25,00
0
1 1 Toy 1 345
94
3
Jane
Java
25 28,00
0
2 1 Toy 1 345
87
6
Joe SQL 22 32,00
0
1 1 Toy 1 345
34
5
John
Doe
23 25,00
0
1 2 Shoe 2 943
94
3
Jane
Java
25 28,00
0
2 2 Shoe 2 943
87
6
Joe SQL 22 32,00
0
1 2 Shoe 2 943
SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
CARTESIAN PRODUCT
• Example: retrieve the name of employees
that work in the toy department:
CARTESIAN PRODUCT
• Example: retrieve the name of employees
that work in the toy department:
– ∏name(бEmp.dno=Dept.dno(Emp × бdname=‘toy’(Dept)))
CARTESIAN PRODUCT (Cont…)
• ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept)))
345 John Doe 23 25,000 1 1 Toy 1 345
943 Jane Java 25 28,000 2 1 Toy 1 345
876 Joe SQL 22 32,000 1 1 Toy 1 345
345 John Doe 23 25,000 1 2 Shoe 2 943
943 Jane Java 25 28,000 2 2 Shoe 2 943
876 Joe SQL 22 32,000 1 2 Shoe 2 943
SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
CARTESIAN PRODUCT (Cont…)
• ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept)))
345 John
Doe
23 25,000 1 1 Toy 1 345
876 Joe SQL 22 32,000 1 1 Toy 1 345
943 Jane
Java
25 28,000 2 2 Shoe 2 943
SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
CARTESIAN PRODUCT (Cont…)
• ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept)))
345 John
Doe
23 25,000 1 1 Toy 1 345
876 Joe SQL 22 32,000 1 1 Toy 1 345
SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
CARTESIAN PRODUCT (Cont…)
• ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept)))
John Doe
Joe SQL
Name
•Join is a derivative of Cartesian product.
•Equivalent to performing a Selection, using join
predicate as selection formula, over Cartesian
product of the two operand relations.
•One of the most difficult operations to implement
efficiently in an RDBMS and one reason why
RDBMSs have intrinsic performance problems.
•Various forms of join operation
–Natural join (defined by Codd)
–Outer join
–Theta join
–Equijoin (a particular type of Theta join)
–Semijoin
EQUALITY JOIN, NATURAL JOIN,
JOIN, SEMI-JOIN,LEFT JOIN, RIGHT
JOIN
• Equality join connects tuples from two relations that match on certain
attributes. The specified joining columns are kept in the resulting relation.
– ∏name(бdname=‘toy’(Emp Dept)))
• Natural join connects tuples from two relations that match on the
specified common attributes
– ∏name(бdname=‘toy’(Emp Dept)))
• How is an equality join between Emp and Dept using dno different than a
natural join between Emp and Dept using dno?
– Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, …
– Natural join: SS#, name, age, salary, dno, dname, …
• Join is similar to equality join using different comparison operators
– A S op = {=, ≠, ≤, ≥, <, >}
att op att
(dno)
(dno)
EXAMPLE JOIN
• Equality Join, (Emp Dept)))
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 1
3 Bob 22 27000 1
4 Kathy 30 30000 2
5 Shideh 4 4000 1
EMP
dno dname floor mgrss#
1 Toy 1 5
2 Shoe 2 1
Dept
(dno)
SS# Name Age Salary EMP.dno Dept.dn
o
dname floor mgrss
#
1 Joe 24 20000 2 2 Shoe 2 1
2 Mary 20 25000 1 1 Toy 1 5
3 Bob 22 27000 1 1 Toy 1 5
4 Kathy 30 30000 2 2 Shoe 2 1
5 Shideh 4 4000 1 1 Toy 1 5
EXAMPLE JOIN
• Natural Join, (Emp Dept)
SS# Name Age Salary dno
1 Joe 24 20000 2
2 Mary 20 25000 1
3 Bob 22 27000 1
4 Kathy 30 30000 2
5 Shideh 4 4000 1
EMP
dno dname floor mgrss#
1 Toy 1 5
2 Shoe 2 1
Dept
(dno)
SS# Name Age Salary dno dname floor mgrss#
1 Joe 24 20000 2 Shoe 2 1
2 Mary 20 25000 1 Toy 1 5
3 Bob 22 27000 1 Toy 1 5
4 Kathy 30 30000 2 Shoe 2 1
5 Shideh 4 4000 1 Toy 1 5
EMP_ID EMP_NA
ME
CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washingt
on
500000 29
5 Russell Los
angels
200000 36
6 Marry Canada 600000 48
EMPLOYEE
PROJECT
NO
EMP_ID DEPARTMENT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
PROJECT
INNER JOIN
In SQL, INNER JOIN selects records that have
matching values in both tables as long as the
condition is satisfied. It returns the combination of
all rows from both the tables where the condition
satisfies.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
INNER JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID
OUTPUT of INNER JOIN
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
LEFT JOIN
SYNTAX
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
QUERY
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
OUTPUT OF LEFT JOIN
EMP_NAME DEPARTMEN
T
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
RIGHT JOIN
SYNTAX
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
QUERY
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
RIGHT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
OUTPUT OF RIGHT JOIN
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
FULL JOIN
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
FULL JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output of FULL JOIN
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL

More Related Content

Similar to relational algebra and it's implementation

DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
Karthick Panneerselvam
 
19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf
GOWTHAMR721887
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
Edward Blurock
 
Info_Management_report-1.pptx
Info_Management_report-1.pptxInfo_Management_report-1.pptx
Info_Management_report-1.pptx
ChingChingErm
 
Relational model
Relational modelRelational model
Relational model
RUpaliLohar
 
Relational model
Relational modelRelational model
Relational model
RUpaliLohar
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization
Hafiz faiz
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
TanishaKochak
 
Relational operation final
Relational operation finalRelational operation final
Relational operation final
Student
 
UNIT 2 Structured query language commands
UNIT 2 Structured query language commandsUNIT 2 Structured query language commands
UNIT 2 Structured query language commands
Bhakti Pawar
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
Shashi Patel
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
Radhika Talaviya
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
SrikanthS494888
 
Relational Model on Database management PPT
Relational Model on Database management PPTRelational Model on Database management PPT
Relational Model on Database management PPT
ssuser3e0f731
 
Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2
Eddyzulham Mahluzydde
 
14285 lecture2
14285 lecture214285 lecture2
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
emailharmeet
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
Vignesh Saravanan
 
Sql server select queries ppt 18
Sql server select queries ppt 18Sql server select queries ppt 18
Sql server select queries ppt 18
Vibrant Technologies & Computers
 

Similar to relational algebra and it's implementation (20)

DDL and DML statements.pptx
DDL and DML statements.pptxDDL and DML statements.pptx
DDL and DML statements.pptx
 
19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Info_Management_report-1.pptx
Info_Management_report-1.pptxInfo_Management_report-1.pptx
Info_Management_report-1.pptx
 
Relational model
Relational modelRelational model
Relational model
 
Relational model
Relational modelRelational model
Relational model
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
Relational operation final
Relational operation finalRelational operation final
Relational operation final
 
UNIT 2 Structured query language commands
UNIT 2 Structured query language commandsUNIT 2 Structured query language commands
UNIT 2 Structured query language commands
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
Relational Model on Database management PPT
Relational Model on Database management PPTRelational Model on Database management PPT
Relational Model on Database management PPT
 
Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2Chapter 2 Relational Data Model-part 2
Chapter 2 Relational Data Model-part 2
 
14285 lecture2
14285 lecture214285 lecture2
14285 lecture2
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Sql server select queries ppt 18
Sql server select queries ppt 18Sql server select queries ppt 18
Sql server select queries ppt 18
 

More from dbmscse61

Computer memory and types of memory.pptx
Computer memory and types of memory.pptxComputer memory and types of memory.pptx
Computer memory and types of memory.pptx
dbmscse61
 
computercodes, BCD, EBCDIC,ASCII,UNICODE
computercodes, BCD, EBCDIC,ASCII,UNICODEcomputercodes, BCD, EBCDIC,ASCII,UNICODE
computercodes, BCD, EBCDIC,ASCII,UNICODE
dbmscse61
 
Computer Arithmetic(add,sub,multiply,div).pptx
Computer Arithmetic(add,sub,multiply,div).pptxComputer Arithmetic(add,sub,multiply,div).pptx
Computer Arithmetic(add,sub,multiply,div).pptx
dbmscse61
 
ch-01numbersystems computer fundamentals
ch-01numbersystems computer fundamentalsch-01numbersystems computer fundamentals
ch-01numbersystems computer fundamentals
dbmscse61
 
different Data type Modifiers in c language
different Data type Modifiers in c languagedifferent Data type Modifiers in c language
different Data type Modifiers in c language
dbmscse61
 
dbms lecture 1.pptx , where traditional file system vs database management ar...
dbms lecture 1.pptx , where traditional file system vs database management ar...dbms lecture 1.pptx , where traditional file system vs database management ar...
dbms lecture 1.pptx , where traditional file system vs database management ar...
dbmscse61
 
Computer Fundamentals lecture 1 Basic components of computer system.pptx
Computer Fundamentals lecture 1 Basic components of computer system.pptxComputer Fundamentals lecture 1 Basic components of computer system.pptx
Computer Fundamentals lecture 1 Basic components of computer system.pptx
dbmscse61
 

More from dbmscse61 (7)

Computer memory and types of memory.pptx
Computer memory and types of memory.pptxComputer memory and types of memory.pptx
Computer memory and types of memory.pptx
 
computercodes, BCD, EBCDIC,ASCII,UNICODE
computercodes, BCD, EBCDIC,ASCII,UNICODEcomputercodes, BCD, EBCDIC,ASCII,UNICODE
computercodes, BCD, EBCDIC,ASCII,UNICODE
 
Computer Arithmetic(add,sub,multiply,div).pptx
Computer Arithmetic(add,sub,multiply,div).pptxComputer Arithmetic(add,sub,multiply,div).pptx
Computer Arithmetic(add,sub,multiply,div).pptx
 
ch-01numbersystems computer fundamentals
ch-01numbersystems computer fundamentalsch-01numbersystems computer fundamentals
ch-01numbersystems computer fundamentals
 
different Data type Modifiers in c language
different Data type Modifiers in c languagedifferent Data type Modifiers in c language
different Data type Modifiers in c language
 
dbms lecture 1.pptx , where traditional file system vs database management ar...
dbms lecture 1.pptx , where traditional file system vs database management ar...dbms lecture 1.pptx , where traditional file system vs database management ar...
dbms lecture 1.pptx , where traditional file system vs database management ar...
 
Computer Fundamentals lecture 1 Basic components of computer system.pptx
Computer Fundamentals lecture 1 Basic components of computer system.pptxComputer Fundamentals lecture 1 Basic components of computer system.pptx
Computer Fundamentals lecture 1 Basic components of computer system.pptx
 

Recently uploaded

How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 

Recently uploaded (20)

How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 

relational algebra and it's implementation

  • 1. RELATIONAL ALGEBRA Shyama Barna Bhattacharya Lecturer Department of Computer Science and Engineering
  • 2. •Relation schema –Named relation defined by a set of attribute and domain name pairs. •Relational database schema –Set of relation schemas, each with a distinct name. •Each tuple is distinct; there are no duplicate tuples. •Order of attributes has no significance. •Order of tuples has no significance, theoretically. •Relation name is distinct from all other relation names in relational schema. •Each cell of relation contains exactly one atomic (single) value. •Each attribute has a distinct name. •Values of an attribute are all from the same domain. •Each tuple is distinct; there are no duplicate tuples. •Order of attributes has no significance. •Order of tuples has no significance, theoretically.
  • 3. Database Scheme/schema A relational database scheme, or schema, corresponds to a set of table definitions. Eg: product(p_id, name, category, description) supply(p_id, s_id, qnty_per_month) supplier(s_id, name, address, ph#) * remember the difference between a DB instance and a DB scheme.
  • 4. Difference Between Schema and Instance Schema Instance It is the overall description of the database. It is the collection of information stored in a database at a particular moment. The schema is same for the whole database. Data in instances can be changed using addition, deletion, and updation. Does not change Frequently. Changes Frequently. Defines the basic structure of the database i.e. how the data will be stored in the database. It is the set of Information stored at a particular time.
  • 5. “Schema” and “Instance” are key ideas in a database management system (DBMS) that help organize and manage data. Let’s begin by examining their distinctions from one another. Instances An Instance is the state of an operational database with data at any given time. It contains a snapshot of the database. The instances can be changed by certain CRUD operations, such as like addition, and deletion of data. It may be noted that any search query will not make any kind of changes in the instances. Example of instances: Let’s say a table teacher in our database whose name is School, suppose the table has 50 records so the instance of the database has 50 records for now and tomorrow we are going to add another fifty records so tomorrow the instance has a total of 100 records. This is called an instance. Schema Schema is the overall description of the database. The basic structure of how the data will be stored in the database is called schema.
  • 6. SCHEMA (**Already discussed in class**) Example: Let’s say a table teacher in our database named school, the teacher table requires the name, dob, and doj in their table so we design a structure as: Teacher table name: String doj: date dob: date Above given is the schema of the table teacher. Schema Schema is the overall description of the database. The basic structure of how the data will be stored in the database is called schema. SCHEMA Schema is of three types: Logical Schema, Physical Schema and view Schema. Logical Schema – It describes the database designed at a logical level. Physical Schema – It describes the database designed at the physical level. View Schema – It defines the design of the database at the view level.
  • 7. SAMPLE SCHEMAS AND INSTANCES The Schemas: Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) The Instances:
  • 8. Introduction • one of the two formal query languages of the relational model • collection of operators for manipulating relations • Operators: two types of operators – Set Operators: Union(),Intersection(), Difference(-), Cartesian Product (x) – New Operators: Select (), Project (), Join (⋈)
  • 9. Introduction – cont’d • A Relational Algebra Expression: a sequence of relational algebra operators and operands (relations), formed according to a set of rules. • The result of evaluating a relational algebra expression is a relation.
  • 10. What is Relational Algebra? • Relational algebra is a procedural query language. • It consists of the select, project, union, set difference, Cartesian product, and rename operations. • Set intersection, division, natural join, and assignment combine the fundamental operations. • SQL is based on relational algebra
  • 11. •Relational algebra and relational calculus are formal languages associated with the relational model. •Both are equivalent to one another
  • 12. • It is an abstract language. We use it to express the set of operations that any relational query language must perform. • Two types of operations: • 1.set-theoretic operations: tables are essentially sets of rows • 2.native relational operations: focus on the structure of the rows Query languages are specialized languages for asking questions,or queries,that involve the data in database. What are the query languages ?
  • 13. Query languages • procedural vs. non-procedural • commercial languages have some of both • we will study: – relational algebra (which is procedural, i.e. tells you how to process a query) – relational calculus (which is non-procedural i.e. tells what you want)
  • 14. Selection Operator •The select operation selects tuples that satisfy a given predicate. •It is denoted by sigma (σ). Notation: σ p(r) Where: σ is used for selection prediction r is used for relation p is used as a propositional logic formula which may use connectors like: AND OR and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
  • 15. Example of Selection Input: 1.σ BRANCH_NAME="perryride" (LOAN) BRANCH_NAME LOAN_NO AMOUNT Downtown L-17 1000 Redwood L-23 2000 Perryride L-15 1500 Downtown L-14 1500 Mianus L-13 500 Roundhill L-11 900 Perryride L-16 1300
  • 16. Example of Selection Output: BRANCH_NAME LOAN_NO AMOUNT Perryride L-15 1500 Perryride L-16 1300
  • 17. SEMANTICS OF THE SAMPLE RELATIONS • Sailors: Entity set; lists the relevant properties of sailors. • Boats: Entity set; lists the relevant properties of boats. • Reserves: Relationship set: links sailors and boats by describing the boat number and date for which a sailor made a reservation. Example of the declarative sentences for which rows stand: Row 1: “Sailor ’22’ reserved boat number ‘101’ on 10/10/98”.
  • 18. Selection and Projection • Selection Operator: σrating>8 (S2) Retrieves from the current instance of relation named S2 those rows where the value of the attribute ‘rating’ is greater than 8. Applying the above selection operator to the sample instance of S2 shown in figure 4.2 yields the relational instance on figure 4.4 as shown below: Select operators only select rows from relation. • π condition
  • 19. Projection operator •This operator shows the list of those attributes that we wish to appear in the result. Rest of the attributes are eliminated from the table. •It is denoted by ∏(pie). •It is used to choose all columns from relation, deletes unwanted columns for relation.
  • 20. Projection Operator πsname,rating(S2) Retrieves from the current instance of the relation named S2 those columns whose names are ‘sname’ and ‘rating’. Applying the above operator to the sample instance of S2 shown in figure 4.2 yields the relational instance on figure 4.5 as shown below:
  • 21. - Projection Operator (cont’d) Similarly πage(S2) yields the following relational instance Note here the elimination of duplicates SQL would yield For πage (S2): age 35.0 55.0 35.0 35.0
  • 22. Selection • Denoted by c(R) • Selects the tuples (rows) from a relation R that satisfy a certain selection condition c. • It is a unary operator • The resulting relation has the same attributes as those in R.
  • 23. Example 1: SNO SNAME AGE STATE S1 MIKE 21 IL S2 STEVE 20 LA S3 MARY 18 CA S4 MING 19 NY S5 OLGA 21 NY S:  state=‘IL’(S)
  • 24. Example 2: CNO CNAME CREDIT DEPT C1 Databas e 3 CS C2 Statistic s 3 MATH C3 Tennis 1 SPORTS C4 Violin 4 MUSIC C5 Golf 2 SPORTS C6 Piano 5 MUSIC C:  CREDIT  3(C)
  • 25. Example 3 SNO CNO Grade S1 C1 90 S1 C2 80 S1 C3 75 S1 C4 70 S1 C5 100 S1 C6 60 S2 C1 90 S2 C2 80 S3 C2 90 S4 C2 80 S4 C4 85 S4 C5 100 E: SNO=‘S1’and CNO=‘C1’(E)
  • 26.
  • 27. Selection - Properties • Selection Operator is commutative C1(C2 (R)) = C2(C1 (R)) • The Selection is an unary operator, it cannot be used to select tuples from more than one relations.
  • 28. Projection • Denoted by L(R), where L is list of attribute names and R is a relation name or some other relational algebra expression. • The resulting relation has only those attributes of R specified in L. • The projection is also an unary operation. • Duplicate rows are not permitted in relational algebra. Duplication is removed from the result. • Duplicate rows can occur in SQL, though they may be controlled by explicit keywords.
  • 29. Projection - Example • Example 1: STATE (S) SNO SNAME AGE STATE S1 MIKE 21 IL S2 STEVE 20 LA S3 MARY 18 CA S4 MING 19 NY S5 OLGA 21 NY STATE IL LA CA NY
  • 30. Projection - Example Example 2: CNAME, DEPT(C) CNO CNAME CREDIT DEPT C1 Databas e 3 CS C2 Statistic s 3 MATH C3 Tennis 1 SPORTS C4 Violin 4 MUSIC C5 Golf 2 SPORTS C6 Piano 5 MUSIC CNAME DEPT Databas e CS Statistic s MATH Tennis SPORTS Violin MUSIC Golf SPORTS Piano MUSIC
  • 31. Projection - Example Example 3: S#(STATE=‘NY'(S)) SNO SNAME AGE STATE S1 MIKE 21 IL S2 STEVE 20 LA S3 MARY 18 CA S4 MING 19 NY S5 OLGA 21 NY SNO S4 S5
  • 32. SET Operations • UNION: R1  R2 • INTERSECTION: R1  R2 • DIFFERENCE: R1 - R2 • CARTESIAN PRODUCT: R1  R2
  • 33. Union Compatibility • For operators , , -, the operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of the corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1,2,...,n. • The resulting relation for , , or - has the same attribute names as the first operand relation R1 (by convention).
  • 34. Union Compatibility - Examples • Are S(SNO, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT, DEPT) union compatible? • Are S(S#, SNAME, AGE, STATE) and C(CNO, CNAME, CREDIT_HOURS, DEPT_NAME) union compatible?
  • 35.
  • 36. UNION, SET DIFFERENCE & SET INTERSECT • Union puts all tuples of two relations in one relation. To use this operator, two conditions must hold: 1. The two relations must be of the same arity. 2. The domain of ith attribute of the two participating relation must be the same. • Set difference operator computes tuples that are in one relation, but not in another. • Set intersect operator computes tuples that are common in two relations: • The five fundamental operations of the relational algebra are: select, project, cartesian product, Union, and set difference • All other operators can be constructed using these operators
  • 37. EXAMPLE • Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) • Query 1: Find the bid of red colored boats:
  • 38. EXAMPLE • Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) • Query 1: Find the bid of red colored boats: – ∏bid(бcolor=red(Boats))
  • 39. EXAMPLE • Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) • Query 1: Find the name of sailors who have reserved Boat number 2.
  • 40. EXAMPLE • Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) • Query 1: Find the name of sailors who have reserved Boat number 2. – ∏sname(бbid=2(Sailors (sid)Reserve))
  • 41. EXAMPLE • Assume a database with the following three relations: Sailors (sid, sname, rating) Boats (bid, bname, color) Reserve (sid, bid, date) • Query 1: Find the name of sailors who have reserved both a red and a green boat.
  • 42. Union, Intersection, Difference • T= R U S : A tuple t is in relation T if and only if t is in relation R or t is in relation S • T = R  S: A tuple t is in relation T if and only if t is in both relations R and S • T= R - S :A tuple t is in relation T if and only if t is in R but not in S
  • 43. Set-Intersection • Denoted by the symbol . • Results in a relation that contains only the tuples that appear in both relations. • R  S = R – (R – S) • Since set-intersection can be written in terms of set-difference, it is not a fundamental operation.
  • 44. Examples A1 A2 1 Red 3 White 4 green B1 B2 3 White 2 Blue R S
  • 45. Examples A1 A2 1 Red 3 White 4 Green 2 Blue A1 A2 3 White R S R  S S - R B1 B2 2 Blue A1 A2 1 Red 4 Green R - S
  • 46. RENAME OPERATOR • Rename operator changes the name of its input table to its subscript, – ρe2(Emp) – Changes the name of Emp table to e2
  • 47. RELATIONAL ALGEBRA INTRODUCTION • Assume the following two relations: Emp (SS#, name, age, salary, dno) Dept (dno, dname, floor, mgrSS#) • Relational algebra is a procedural query language, i.e., user must define both “how” and “what” to retrieve. • Relational algebra consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output. • Unary operators include: select, project, and rename • Binary operators include: cartesian product, equality join, natural join, join, semi-join, division, union, and set difference.
  • 48. SELECT OPERATOR • Select (б): selects tuples that satisfy a predicate; e.g., retrieve the employees whose salary is 30,000 бSalary=30,000(Employee) • Conjunctive ( ) and disjunctive ( ) selection predicates are allowed; e.g., retrieve employees whose salary is higher than 30,000 and are younger than 25 years old: бSalary>30,000 age<25(Employee) • Note that only selection predicates are allowed. A selection predicate is either (1) a comparison (=, ≠, ≤, ≥, <, >) between an attribute and a constant (e.g., salary = 30,000) or (2) a comparison between two different attributes of the same relation (e.g., salary = age × 100). • Note: This operator is different than the SELECT command of SQL. < < <
  • 49. EXAMPLE • Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1
  • 50. EXAMPLE • Emp table: • бSalary=30,000(Employee) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1
  • 51. EXAMPLE • Emp table: • бSalary=30,000(Employee) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1 SS# Name Age Salary dno 4 Kathy 30 30000 5
  • 52. EXAMPLE • Emp table: • бAge>22(Employee) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1
  • 53. EXAMPLE • Emp table: • бAge>22(Employee) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1 SS# Name Age Salary dno 1 Joe 24 20000 2 4 Kathy 30 30000 5
  • 54. PROJECT OPERATOR • Project (∏) retrieves a column. It is a unary operator that eliminate duplicates. e.g., name of employees: ∏ name(Employee) e.g., name of employees earning more than 30,000: ∏ name(бSalary>30,000(Employee))
  • 55. EXAMPLE • Emp table: SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1
  • 56. EXAMPLE • Emp table: • ∏ age(Emp) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1 Age 24 20 22 30 4
  • 57. EXAMPLE • Emp table: • ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1
  • 58. EXAMPLE • Emp table: • ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1 SS# Name Age Salary dno 5 Shideh 4 4000 1
  • 59. EXAMPLE • Emp table: • ∏ name,age(бSalary=4000 (Emp) ) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 3 3 Bob 22 27000 4 4 Kathy 30 30000 5 5 Shideh 4 4000 1 Name Age Shideh 4
  • 60.
  • 61. CARTESIAN PRODUCT • Cartesian Product (R1 × R2) combines two relations by concatenating their tuples together, evaluating all possible combinations. If the name of a column is identical for two relations, this ambiguity is resolved by attaching the name of each relation to a column. e.g., Emp × Dept – (SS#, name, age, salary, Emp.dno, Dept.dno, dname, floor, mgrSS#) • If t(Emp) and t(Dept) is the cardinality of the Employee and Dept relations respectively, then the cardinality of Emp × Dept is: t(Emp) × t(Dept)
  • 62. CARTESIAN PRODUCT (Cont…) • Example: Emp table: Dept table: 345 John Doe 23 25,000 1 943 Jane Java 25 28,000 2 876 Joe SQL 22 32,000 1 SS# Name age salary dno 1 Toy 1 345 2 Shoe 2 943 dno dname floor mgrSS#
  • 63. CARTESIAN PRODUCT (Cont…) • Cartesian product of Emp and Dept: Emp × Dept: 34 5 John Doe 23 25,00 0 1 1 Toy 1 345 94 3 Jane Java 25 28,00 0 2 1 Toy 1 345 87 6 Joe SQL 22 32,00 0 1 1 Toy 1 345 34 5 John Doe 23 25,00 0 1 2 Shoe 2 943 94 3 Jane Java 25 28,00 0 2 2 Shoe 2 943 87 6 Joe SQL 22 32,00 0 1 2 Shoe 2 943 SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
  • 64. CARTESIAN PRODUCT • Example: retrieve the name of employees that work in the toy department:
  • 65. CARTESIAN PRODUCT • Example: retrieve the name of employees that work in the toy department: – ∏name(бEmp.dno=Dept.dno(Emp × бdname=‘toy’(Dept)))
  • 66. CARTESIAN PRODUCT (Cont…) • ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept))) 345 John Doe 23 25,000 1 1 Toy 1 345 943 Jane Java 25 28,000 2 1 Toy 1 345 876 Joe SQL 22 32,000 1 1 Toy 1 345 345 John Doe 23 25,000 1 2 Shoe 2 943 943 Jane Java 25 28,000 2 2 Shoe 2 943 876 Joe SQL 22 32,000 1 2 Shoe 2 943 SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
  • 67. CARTESIAN PRODUCT (Cont…) • ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept))) 345 John Doe 23 25,000 1 1 Toy 1 345 876 Joe SQL 22 32,000 1 1 Toy 1 345 943 Jane Java 25 28,000 2 2 Shoe 2 943 SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
  • 68. CARTESIAN PRODUCT (Cont…) • ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept))) 345 John Doe 23 25,000 1 1 Toy 1 345 876 Joe SQL 22 32,000 1 1 Toy 1 345 SS# Name age salary Emp.dno Dept.dno dname floor mgrSS#
  • 69. CARTESIAN PRODUCT (Cont…) • ∏name(бdname=‘toy’ (бEmp.dno=Dept.dno(Emp × Dept))) John Doe Joe SQL Name
  • 70.
  • 71. •Join is a derivative of Cartesian product. •Equivalent to performing a Selection, using join predicate as selection formula, over Cartesian product of the two operand relations. •One of the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMSs have intrinsic performance problems. •Various forms of join operation –Natural join (defined by Codd) –Outer join –Theta join –Equijoin (a particular type of Theta join) –Semijoin
  • 72. EQUALITY JOIN, NATURAL JOIN, JOIN, SEMI-JOIN,LEFT JOIN, RIGHT JOIN • Equality join connects tuples from two relations that match on certain attributes. The specified joining columns are kept in the resulting relation. – ∏name(бdname=‘toy’(Emp Dept))) • Natural join connects tuples from two relations that match on the specified common attributes – ∏name(бdname=‘toy’(Emp Dept))) • How is an equality join between Emp and Dept using dno different than a natural join between Emp and Dept using dno? – Equality join: SS#, name, age, salary, Emp.dno, Dept.dno, … – Natural join: SS#, name, age, salary, dno, dname, … • Join is similar to equality join using different comparison operators – A S op = {=, ≠, ≤, ≥, <, >} att op att (dno) (dno)
  • 73. EXAMPLE JOIN • Equality Join, (Emp Dept))) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 1 3 Bob 22 27000 1 4 Kathy 30 30000 2 5 Shideh 4 4000 1 EMP dno dname floor mgrss# 1 Toy 1 5 2 Shoe 2 1 Dept (dno) SS# Name Age Salary EMP.dno Dept.dn o dname floor mgrss # 1 Joe 24 20000 2 2 Shoe 2 1 2 Mary 20 25000 1 1 Toy 1 5 3 Bob 22 27000 1 1 Toy 1 5 4 Kathy 30 30000 2 2 Shoe 2 1 5 Shideh 4 4000 1 1 Toy 1 5
  • 74. EXAMPLE JOIN • Natural Join, (Emp Dept) SS# Name Age Salary dno 1 Joe 24 20000 2 2 Mary 20 25000 1 3 Bob 22 27000 1 4 Kathy 30 30000 2 5 Shideh 4 4000 1 EMP dno dname floor mgrss# 1 Toy 1 5 2 Shoe 2 1 Dept (dno) SS# Name Age Salary dno dname floor mgrss# 1 Joe 24 20000 2 Shoe 2 1 2 Mary 20 25000 1 Toy 1 5 3 Bob 22 27000 1 Toy 1 5 4 Kathy 30 30000 2 Shoe 2 1 5 Shideh 4 4000 1 Toy 1 5
  • 75. EMP_ID EMP_NA ME CITY SALARY AGE 1 Angelina Chicago 200000 30 2 Robert Austin 300000 26 3 Christian Denver 100000 42 4 Kristen Washingt on 500000 29 5 Russell Los angels 200000 36 6 Marry Canada 600000 48 EMPLOYEE PROJECT NO EMP_ID DEPARTMENT 101 1 Testing 102 2 Development 103 3 Designing 104 4 Development PROJECT
  • 76. INNER JOIN In SQL, INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It returns the combination of all rows from both the tables where the condition satisfies. Syntax SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 INNER JOIN table2 ON table1.matching_column = table2.matching_column;
  • 77. Query SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM EMPLOYEE INNER JOIN PROJECT ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID
  • 78. OUTPUT of INNER JOIN EMP_NAME DEPARTMENT Angelina Testing Robert Development Christian Designing Kristen Development
  • 79. LEFT JOIN SYNTAX SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 LEFT JOIN table2 ON table1.matching_column = table2.matching_column; QUERY SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM EMPLOYEE LEFT JOIN PROJECT ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
  • 80. OUTPUT OF LEFT JOIN EMP_NAME DEPARTMEN T Angelina Testing Robert Development Christian Designing Kristen Development Russell NULL Marry NULL
  • 81. RIGHT JOIN SYNTAX SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 RIGHT JOIN table2 ON table1.matching_column = table2.matching_column; QUERY SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM EMPLOYEE RIGHT JOIN PROJECT ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
  • 82. OUTPUT OF RIGHT JOIN EMP_NAME DEPARTMENT Angelina Testing Robert Development Christian Designing Kristen Development
  • 83. FULL JOIN Syntax SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 FULL JOIN table2 ON table1.matching_column = table2.matching_column; Query SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM EMPLOYEE FULL JOIN PROJECT ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
  • 84. Output of FULL JOIN EMP_NAME DEPARTMENT Angelina Testing Robert Development Christian Designing Kristen Development Russell NULL Marry NULL