2. Introduction
A query language is a computer programming language that
allows users to interact with a database management system
(DBMS) to retrieve and manipulate data.
Query languages translate human questions into commands that
the database can understand.
It serves as an interface between users and databases, enabling
users to manage data from a database management system
(DBMS)
2
3. what is a query?
A query refers to a request for data or information from a database
or a data repository system.
This request is typically made in the form of a specific question or
command, written in a query language that the database
understands.
Queries can be used to find, summarize, filter, combine, adjust,
delete, insert, and update data.
They can also answer data-related questions, analyze data from
multiple tables at once, and automate data management tasks.
3
4. Relational Algebra
Relational Algebra is a procedural query language that consists
of a set of operations that take one or two relations as input and
produce a new relation as a result.
Fundamental Operations of Relational Algebra
Unary Operators.
Selection :-
Projection :-
Rename :-
Binary Operators.
Product (Cartesian Product) :-
Union :-
Difference :- –
4
5. Unary Operations
Select Operation
The select operation selects a subset of tuples from a relation
instance that satisfies a given predicate (condition).
C (R)
represents the SELECT operator,
C is a Boolean expression of the select condition, and
R is the relation or relational algebra expression.
5
6. Example
Consider the student table given below:
6
1. Display all the records of CSE branch in student table, we
will use the following command
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A
Regno Branch Section
1 CSE A
7. Cont’s
2. To display all the records in student tables whose regno>2
7
3. To display the record of ECE branch section B students
Regno Branch Section
3 CIVIL B
4 IT A
8. … Unary Operations
Project Operation
Projection operation forms a new relation by picking certain
columns in the relation.
A (R)
represents the PROJECT operator,
A is a set of attributes in the relation R , and
R is the relation or relational algebra expression.
8
9. Example
Consider the student table given below:
9
1. To display regno column of student table, we can use the
following command
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A
Regno
1
2
3
4
10. Cont’s
10
2. To display branch, section column of student table, use
the following command
3. To display regno, section of ECE students, use the following
command
11. … Unary Operations
Rename Operation
The renaming operator can be used to explicitly rename
resulting relations of an expression.
S(A1, A2, … An) (R)
represents the RENAME operator,
S is a name for the new relation , and
A1, A2, … An are new names for the attributes in the
relation R.
11
12. Example
The name, branch column of student table are renamed as
newname and newbranch respectively
12
13. Binary Operations
Cartesian Product Operation
The Cartesian product operation (also known as Cross Product or
Cross Join or Product) is binary set operation that generates a new
relation from two relation in a combinatorial fashion.
R S
represents the PRODUCT operator, and
R and S are relations to be joined..
Degree of R X S = degree of R + degree of S
{degree = total no of columns}
13
14. Example
Consider the following tables (R1 and R2)
respectively
14
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A
Name RegNo
Bhanu 2
priya 4
16. … Binary Operations
Set Operations
Union Operation
The union operation on R and S denoted by R S results a relation
that includes all tuples either in R or in S or in both.
Intersection Operation
The intersection operation on R and S denoted by R S results a
relation that includes all tuples in both R and S.
Set Difference Operation
The result of the set difference operation on R and S denoted by R −
S is the set of elements in R but not in S.
16
20. Example (Intersection)
Find all the customers whose account is in the bank and
have taken out a loan.
The expression is as follows −
Depositor Borrower
20
Name
A
B
C
Name
B
A
D
21. Example
Consider the below tables Employee and Student. Perform Set Difference
operation as (Employee-Student).
21
23. 23
… Binary Operations
For the set operations (Union, Intersection, Set difference) the two
relational operands R and S must have same type of tuples, this
condition is known as Union Compatibility.
Two relations R(A1, A2, … An) and S(B1, B2, … Bn) are said to
be union compatible if
They have same degree n, and
Domain(Ai) = Domain(Bi) for all i = 1, 2, … n
For all set operations duplicates are eliminated from the result.
24. Relational calculus
Relational calculus is a non-procedural query language in
database management systems, guides users on what data is
needed without specifying how to obtain it.
Commonly utilized in commercial relational languages like
SQL-QBE and QUEL, relational calculus ensures a focus on
desired data without delving into procedural details, promoting a
more efficient and abstract approach to querying in relational
databases
24
25. Query By Example (QBE)
It is a graphical query language where we get a user interface and
then we fill some required fields to get our proper result.
In SQL we will get an error if the query is not correct but in the
case of QBE if the query is wrong either we get a wrong answer
or the query will not be going to execute but we will never get
any error.
In QBE we don’t write complete queries like SQL or other
database languages it comes with some blank so we need to just
fill that blanks and we will get our required result.
25
26. What is Relational Calculus?
Before understanding Relational calculus in DBMS, we need to
understand Procedural Language and Declarative Language.
Procedural Language - Those Languages which clearly define
how to get the required results from the Database are called
Procedural Language. Relational algebra is a Procedural
Language.
Declarative Language - Those Language that only cares about
What to get from the database without getting into how to get the
results are called Declarative Language. Relational Calculus is a
Declarative Language.
26
27. Types of Relational Calculus in DBMS
Relational Calculus is Two Types:
Tuple Relational Calculus (TRC)
Domain Relational Calculus (DRC)
27
28. Tuple Relational Calculus (TRC)
Tuple Relational Calculus in DBMS uses a tuple variable (t)
that goes to each row of the table and checks if the predicate
is true or false for the given row. Depending on the given
predicate condition, it returns the row or part of the row.
The Tuple Relational Calculus expression Syntax
{ t| p(t) }
Where t is the tuple variable that runs over every Row, and
P(t) is the predicate logic expression or condition.
28
30. Conts’….
Example 1: Write a TRC query to get all the data of customers
whose zip code is 12345.
TRC Query: {t | t Customer t.Zipcode = 12345}
∈ ∧ or TRC
Query: {t | Customer(t) t[Zipcode] = 12345 }
∧
Workflow of query - The tuple variable "t" will go through every
tuple of the Customer table. Each row will check whether the
Cust_Zipcode is 12345 or not and only return those rows that
satisfies the Predicate expression condition.
Result of the TRC expression above:
30
Customer_id Name Zip code
1 Rohit 12345
4 Amit 12345
31. Conts’…….
Example 2: Write a TRC query to get the customer id of all
the Customers.
TRC query: { t | s (s Customer s.Customer_id =
∃ ∈ ∧
t.customer_id) }
Result of the TRC Query:
31
Customer_ID
1
2
3
4
5
32. EXERCISES
Consider student table
FN LN AGE
A E 30
B F 31
C G 27
D H 28
32
Display the last name of those students where age is
greater than 30
{ t.LN | students(t) and t.age >30)}
33. Domain Relational Calculus (DRC)
Domain Relational Calculus uses domain Variables to get the
column values required from the database based on the
predicate expression or condition.
The Domain relational calculus expression syntax:
{ < x1, x2, x3, x4…>|p(x1, x2, x3, x4…)}
where,
<x1,x2,x3,x4...> are domain variables used to get the column
values required, and P(x1,x2,x3...) is predicate expression or
condition.
33
35. Conts’…
Example 1: Write a DRC query to get the data of all
customers with Zip code 12345.
DRC query: {<x1,x2,x3> | <x1,x2> Customer x3 =
∈ ∧
12345 }
Workflow of Query: In the above query x1,x2,x3 (ordered)
refers to the attribute or column which we need in the result,
and the predicate condition is that the first two domain
variables x1 and x2 should be present while matching the
condition for each row and the third domain variable x3
35