This is a slide on relational algebra. i have discussed some common operation of relational algebra. most impotently i have add all the sql of corresponding operation and also the syntax.
2. 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
3. Fundamental Operation in Relational Algebra
• Selection
• Projection
• Union
• Set Difference
• Cartesian Product
• Natural Join
• Outer join
3/18
4. 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
5. 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
6. 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
8. 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
9. 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
11. 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
13. 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
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