Topic: Relational Algebra
1/18
What is Relational Algebra?
• Relational Algebra is a procedural query language. It
consists of a set of operations that take one or two
relations as input and produce a new relation as their
output.
2/18
Fundamental Operation in Relational Algebra
• Selection
• Projection
• Union
• Set Difference
• Cartesian Product
• Natural Join
• Outer join
3/18
Selection
• Selects tuples or rows that satisfy a given predicate.
• Denoted by lowercase Greek letter sigma(σ).
• Syntax: σAttribute_name=Condition/predicate(Relation/Table_name)
• SQL- SELECT * FROM `cricket` WHERE sname="A“
cricket cricket
4/18
Output
Projection
• Projection operation is used to select or see any column
• Denoted by uppercase Greek letter Pi(π)
• Syntax: πAttributes_name(σAttribute_name=Condition/predicate(Relation/Table_name))
Such as πsid,sname(σscore=score>40(cricket))
• SQL- SELECT sid,sname from `cricket` WHERE score>40
5/18
Output
Union
• Performed as binary union operation between two given relation or table.
This operation must satisfy two conditions. Those are
• Each table must have same numbers of attributes or columns.
• And the have same data type.
• Denoted by uppercase Greek letter Pi(π)
• Syntax: πAttributes_name (cricket) ∪πAttributes_name (football)
Such as πsid,sname(cricket) ∪ πsid,sname(football)
• SQL- SELECT sid,sname FROM cricket UNION SELECT sid,sname FROM
football;
6/18
Union(Continue)
Example:
Cricket football
7/18
Output
Set Difference
• Allows us to compare two tables or find records that are in one table not in
other
• Denoted by Minus(-)
• Syntax: πAttributes_name (cricket) -πAttributes_name (football)
Such as πsid,sname(cricket) - πsid,sname(football)
• SQL- Select sid,sname from cricket MINUS Select sid,sname football;
8/18
Cartesian Product
• Allows us to combine information from any two relation
• Denoted by a cross(×).
• Syntax: cricket × football
• SQL-SELECT cricket.sid, cricket.sname, football.sid, football.sname
FROM cricket, football
Or, SELECT cricket.sid, cricket.sname, football.sid, football.sname
FROM cricket cross join football
9/18
Cartesian Product(continue)
Example:
cricket football
10/18
Output
Natural Join
• Allow us to combine certain selections and a Cartesian product into
one operation.
• Must be a common attribute between two table.
• Denoted by (⋈)
• Syntax:πAttributes_name(σCommon_column_name=Common_column_name (table1×table2)
• SQL-SELECT * FROM cricket natural join football;
11/18
Natural Join(continue)
• Example:
cricket football
12/18
Output
Outer Join
• Extension of Natural join.
• Deals with missing data in Natural Join.
• Normally 3 types
• Left outer Join( )
• Right outer join(⟖)
• Full outer join(⟗)
13/18
Left outer join
• Returns all values of left table
• SQL-SELECT*FROM cricket LEFT JOIN football ON cricket.sid =
football.sid;
• Example:
cricket football
14/18
Right outer join
• Returns all values of right table
• SQL-SELECT*FROM cricket RIGHT JOIN football ON cricket.sid =
football.sid;
• Example:
cricket football
15/18
Full outer join
• Returns all values of both table. If no match found it returns NULL
• SQL-SELECT*FROM cricket FULL OUTER JOIN football ON cricket.sid =
football.sid;
• Example:
cricket football
16/18
17/18

Relational algebra in DBMS

  • 1.
  • 2.
    What is RelationalAlgebra? • Relational Algebra is a procedural query language. It consists of a set of operations that take one or two relations as input and produce a new relation as their output. 2/18
  • 3.
    Fundamental Operation inRelational Algebra • Selection • Projection • Union • Set Difference • Cartesian Product • Natural Join • Outer join 3/18
  • 4.
    Selection • Selects tuplesor rows that satisfy a given predicate. • Denoted by lowercase Greek letter sigma(σ). • Syntax: σAttribute_name=Condition/predicate(Relation/Table_name) • SQL- SELECT * FROM `cricket` WHERE sname="A“ cricket cricket 4/18 Output
  • 5.
    Projection • Projection operationis used to select or see any column • Denoted by uppercase Greek letter Pi(π) • Syntax: πAttributes_name(σAttribute_name=Condition/predicate(Relation/Table_name)) Such as πsid,sname(σscore=score>40(cricket)) • SQL- SELECT sid,sname from `cricket` WHERE score>40 5/18 Output
  • 6.
    Union • Performed asbinary union operation between two given relation or table. This operation must satisfy two conditions. Those are • Each table must have same numbers of attributes or columns. • And the have same data type. • Denoted by uppercase Greek letter Pi(π) • Syntax: πAttributes_name (cricket) ∪πAttributes_name (football) Such as πsid,sname(cricket) ∪ πsid,sname(football) • SQL- SELECT sid,sname FROM cricket UNION SELECT sid,sname FROM football; 6/18
  • 7.
  • 8.
    Set Difference • Allowsus to compare two tables or find records that are in one table not in other • Denoted by Minus(-) • Syntax: πAttributes_name (cricket) -πAttributes_name (football) Such as πsid,sname(cricket) - πsid,sname(football) • SQL- Select sid,sname from cricket MINUS Select sid,sname football; 8/18
  • 9.
    Cartesian Product • Allowsus to combine information from any two relation • Denoted by a cross(×). • Syntax: cricket × football • SQL-SELECT cricket.sid, cricket.sname, football.sid, football.sname FROM cricket, football Or, SELECT cricket.sid, cricket.sname, football.sid, football.sname FROM cricket cross join football 9/18
  • 10.
  • 11.
    Natural Join • Allowus to combine certain selections and a Cartesian product into one operation. • Must be a common attribute between two table. • Denoted by (⋈) • Syntax:πAttributes_name(σCommon_column_name=Common_column_name (table1×table2) • SQL-SELECT * FROM cricket natural join football; 11/18
  • 12.
  • 13.
    Outer Join • Extensionof Natural join. • Deals with missing data in Natural Join. • Normally 3 types • Left outer Join( ) • Right outer join(⟖) • Full outer join(⟗) 13/18
  • 14.
    Left outer join •Returns all values of left table • SQL-SELECT*FROM cricket LEFT JOIN football ON cricket.sid = football.sid; • Example: cricket football 14/18
  • 15.
    Right outer join •Returns all values of right table • SQL-SELECT*FROM cricket RIGHT JOIN football ON cricket.sid = football.sid; • Example: cricket football 15/18
  • 16.
    Full outer join •Returns all values of both table. If no match found it returns NULL • SQL-SELECT*FROM cricket FULL OUTER JOIN football ON cricket.sid = football.sid; • Example: cricket football 16/18
  • 17.