Chapter 6 DB Student Module
Chapter 6 DB Student Module
Query Languages
1. Relational algebra:
Relational Algebra
RELATIONAL ALGEBRA is a widely used procedural query language. It collects
instances of relations as input and gives occurrences of relations as output. It uses
various operations to perform this action. SQL Relational algebra query operations
are performed recursively on a relation. The output of these operations is a new
relation, which might be formed from one or more input relations.
Unary Relational Operations
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
UNION (υ)
INTERSECTION ( ),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
JOIN
DIVISION
SELECT (σ)
The SELECT operation is used for selecting a subset of the tuples according to a
given selection condition. Sigma(σ)Symbol denotes it. It is used as an expression to
choose tuples which meet the selection condition. Select operator selects tuples
that satisfy a given predicate.
σp(r)
σ is the predicate
r stands for relation which is the name of the table
p is prepositional logic
Example 1
Example 2
Example 3
Output – Selects tuples from Customers where sales is greater than 50000
Projection(π)
The projection eliminates all attributes of the input relation but those mentioned in
the projection list. The projection method defines a relation that contains a vertical
subset of Relation.
Example of Projection:
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active
Rename (ρ)
Rename is a unary operation used for renaming attributes of a relation.
Example
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
Example
A-B
Table A – B
column 1 column 2
1 2
Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are in both A and B. However,
A and B must be union-compatible.
Visual
Definition of Intersection
Example:
A∩B
Table A ∩ B
column 1 column 2
1 1
Join Operations
Join operation is essentially a cartesian product followed by a selection criterion.
JOIN operation also allows joining variously related tuples from different relations.
Types of JOIN:
Outer join:
Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included,
while the rest are excluded. Let’s study various types of Inner Joins:
Example
C
Num Square
2 4
3 9
D
Num Cube
2 8
3 27
C⋈D
C⋈D
Num Square Cube
2 4 8
3 9 27
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also
include some or all tuples that do not match the criteria.
A
Num Square
2 4
3 9
4 16
B
Num Cube
2 8
3 18
5 75
A B
A⋈B
Num Square Cube
2 4 8
3 9 18
4 16 –
A B
A⋈B
Num Cube Square
2 8 4
3 18 9
5 75 –
A B
A⋈B
Num Cube Square
2 4 8
3 9 18
4 16 –
5 – 75
2. Relational Calculus:
It is of two types
It uses a tuple variable (t) that goes to each row of the table and
checks if the predicate is true/false for the given row.
Syntax: { t|p(t)}
t- tuple variable
Eg: Customer
1 ABC 121
2 DEF 152
3 GHI 121
4 XYZ 134
Tuple variable will flow to each and every row in the table and check
the pincode is 121 or not. It will display customers where pincode is 121.
{ t|customer(t) ∧ t.pincode=121}.
{ t.custname|customer(t) ∧ t.pincode=121}.
It uses the domain variable to get the column values required from the
database based on the predicate expression/condition.
Syntax:
{<a1,a2,a3….an> | p(<a1,a2,a3…..,an>)}
{<custid,custname>|customer ∧ pincode=121}
3. SQL
History of SQL:
During the parsing stage SQL Server performs basic checks on the source
code (your T-SQL batch). This parsing looks for invalid SQL syntax, such as
incorrect use of reserved words, column and table names, and so on.
Query
Eg:
Select studid, studname from student1 where age>20;
Query
Translation:
It is used to translate the given SQL query to low level language query
using relational algebra operations.
Basic operation:
Eg:
Π empname (σ salary>10000(employee))
Optimization:
Execution Plan:
Evaluation: