0% found this document useful (0 votes)
3 views45 pages

Notes DBMS

The document provides an overview of SQL operators, categorizing them into arithmetic, logical, and string functions. It details various SQL functions for date manipulation, string handling, and their respective examples and outputs. Additionally, it touches on relational algebra operators, emphasizing their role in data retrieval and manipulation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views45 pages

Notes DBMS

The document provides an overview of SQL operators, categorizing them into arithmetic, logical, and string functions. It details various SQL functions for date manipulation, string handling, and their respective examples and outputs. Additionally, it touches on relational algebra operators, emphasizing their role in data retrieval and manipulation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DBMS

What are SQL operators?

SQL operators are reserved keywords used in the WHERE clause of a SQL
statement to perform arithmetic, logical and comparison operations.
Operators act as conjunctions in SQL statements to fulfill multiple conditions
in a statement.

Since, there are different types of operators in SQL, let us understand the
same in the next section of this article on SQL operators.

Types of SQL Operators

Arithmetic Operators

These operators are used to perform operations such as addition,


multiplication, subtraction etc.

Operato
Operation Description
r
+ Addition Add values on either side of the operator
Used to subtract the right hand side value from the
– Subtraction
left hand side value
Multiplicatio Multiples the values present on each side of the
*
n operator
Divides the left hand side value by the right hand
/ Division
side value
Divides the left hand side value by the right hand
% Modulus
side value; and returns the remainder
Example:

1SELECT 40 + 20;
2
3SELECT 40 - 20;
4
5SELECT 40 * 20;
6
7SELECT 40 / 20;

1
DBMS

8
9SELECT 40 % 20;
60

20

800

Logical Operators

The logical operators are used to perform operations such as ALL, ANY, NOT,
BETWEEN etc.

Operato
Description
r

ALL Used to compare a specific value to all other values in a set

ANY Compares a specific value to any of the values present in a set.

IN Used to compare a specific value to the literal values mentioned.

BETWEE
Searches for values within the range mentioned.
N

AND Allows the user to mention multiple conditions in a WHERE clause.

OR Combines multiple conditions in a WHERE clause.

A negate operators, used to reverse the output of the logical


NOT
operator.

EXISTS Used to search for the row’s presence in the table.

LIKE Compares a pattern using wildcard operators.

SOME Similar to the ANY operator, and is used compares a specific value

2
DBMS

to some of the values present in a set.

Example:

I am going to consider the Students table considered above, to perform a few


of the operations.

Example[ANY]

1SELECT * FROM Students

2WHERE Age > ANY (SELECT Age FROM Students WHERE Age > 21);

Output:

StudentID FirstName LastName Age

1 Atul Mishra 23

5 Vaibhav Gupta 25

Example[BETWEEN & AND]

1SELECT * FROM Students

2WHERE Age BETWEEN 22 AND 25;

Output:

StudentID FirstName LastName Age

1 Atul Mishra 23

Example[IN]

1SELECT * FROM Students

2WHERE Age IN('23', '20');

Output:

StudentID FirstName LastName Age

1 Atul Mishra 23

4 Akanksha Jain 20

SQL | Date Functions

Last Updated : 22 Nov, 2025

3
DBMS

SQL Date Functions are built-in tools used to handle, modify, and analyze
date/time values in a database. They help perform tasks like retrieving
current dates, calculating differences, and formatting results effectively.

 Extract specific parts of a date (year, month, day).

 Format dates for user-friendly display.

 Track trends, deadlines, or schedules in business processes.

Let's use the sales table as the base for demonstrating each of the SQL Date
Functions mentioned. Here’s the structure of the sales table:

sales Table

1. NOW()

The NOW() function retrieves the server’s current date and time, making it
useful for capturing exact event moments such as transaction timestamps,
as well as for logging and comparing time-based records.

Query:

SELECT NOW() AS current_datetime;

Output:

 Returns the current system date and time.

4
DBMS

 Shows both date and time in one value.

2. CURDATE()

The CURDATE() function returns today’s date in YYYY-MM-DD format and is


useful when only the current date is needed, especially for reporting or
filtering records by date.

Query:

SELECT CURDATE() AS current_date;

Output:

 Returns today’s date only.

 Time part is not included.

3. CURTIME()

The CURTIME() function returns the current time in HH:MM:SS format and is
useful for time-based operations, such as scheduling or performing precise
time comparisons.

Query:

SELECT CURTIME() AS current_time;

Output:

 Returns the current system time.

 Shows hours, minutes, and seconds only.

4. DATE()

The DATE() function extracts only the date from a date or datetime value,
making it useful for situations where the time component should be ignored,
such as date-only comparisons or aggregations.

Query:

5
DBMS

SELECT sale_id, product_name,


DATE(sale_date) AS sale_date_only
FROM sales;

Output

 Extracts only the date part from sale_date.

 Removes any time portion if present.

5. EXTRACT()

The EXTRACT() function retrieves a specific part of a date such as the year,
month, or day, making it useful for grouping, filtering, or performing time-
based analysis including year-over-year reports.

Query:

SELECT sale_id, product_name,


EXTRACT(YEAR FROM sale_date)
AS sale_year FROM sales;

Output:

 Pulls out the year from the sale date.

 Useful for grouping or filtering by year.

6. DATE_ADD()

6
DBMS

The DATE_ADD() function adds a chosen time interval such as days, months,
or years to a date, making it useful for calculating future dates and
simplifying planning or scheduling tasks.

Query:

SELECT sale_id, product_name,


DATE_ADD(sale_date, INTERVAL 7 DAY) AS sale_date_plus_7_days
FROM sales;

Output:

 Adds 7 days to the sale date.

 Returns the new future date.

7. DATE_SUB()

The DATE_SUB() function subtracts a chosen time interval from a date,


making it useful for determining past dates and performing retrospective
data analysis.

Query:

SELECT sale_id, product_name,


DATE_SUB(sale_date, INTERVAL 3 DAY)
AS sale_date_minus_3_days
FROM sales;

Output:

7
DBMS

 Subtracts 3 days from the sale date.

 Returns an earlier date.

8. DATEDIFF()

The DATEDIFF() function returns the number of days between two dates,
making it useful for calculating durations such as deadlines or overdue
periods. In this case, it shows how many days remain from each sale date
until August 15, 2024.

Query:

SELECT sale_id, product_name, sale_date,


DATEDIFF('2024-08-15', sale_date) AS days_until_aug15
FROM sales;

Output:

 Calculates days between two dates.

 Shows how many days until 15-Aug-2024.

9. DATE_FORMAT()

The DATE_FORMAT() function formats a date using a specified pattern,


allowing customized output such as full day or month names, and is useful
for making reports clearer and more readable.

8
DBMS

Query:

SELECT sale_id, product_name,


DATE_FORMAT(sale_date, '%W, %M %d, %Y')
AS formatted_sale_date FROM sales;

Output:

 Converts date into a readable text format.

 Displays weekday, month name, day, and year.

10. ADDDATE()

The ADDDATE() function adds a specified time interval to a date. It is useful


for calculating future or past dates based on a given date.

Query:

SELECT sale_id, product_name,


ADDDATE(sale_date, 10)
AS sale_date_plus_10_days
FROM sales;

Output:

 Adds 10 days to the sale date.

 Same as DATE_ADD but without INTERVAL.

9
DBMS

11. ADDTIME()

The ADDTIME() function adds a specified time interval to a time or datetime


value. It is useful for adjusting times by adding hours, minutes or seconds.

Query:

SELECT sale_id, product_name,


ADDTIME('10:30:00', '02:30:00') AS sale_time_plus_2hrs_30min
FROM sales;

Output:

 Adds two time values together.

 Returns the new time after addition.

SQL | String functions

Last Updated : 1 Dec, 2025

SQL string functions help manipulate and format text data efficiently. They
are widely used for cleaning, comparing, and extracting meaningful
information from textual fields.

 Useful for handling names, addresses, and other text-based data.

 Essential for organizing, analyzing, and improving data quality.

SQL String Functions

String functions are used to perform an operation on input string and return
an output string. Below are some of the most commonly used SQL string
functions:

10
DBMS

1. CONCAT()

The CONCAT() function is used to concatenate (combine) two or more strings


into one string. It is useful when we want to merge fields like first and last
names into a full name.

Query:

SELECT CONCAT('John', ' ', 'Doe') AS FullName;

Output:

John Doe

2. CHAR_LENGTH() / CHARACTER_LENGTH()

The CHAR_LENGTH() or LENGTH() function returns the length of a string in


characters. It’s essential for validating or manipulating text data, especially
when you need to know how many characters a string contains.

Query:

SELECT CHAR_LENGTH('Hello') AS StringLength;

Output:

3. UPPER() and LOWER()

These UPPER() and LOWER() functions convert the text to uppercase or


lowercase, respectively. They are useful for normalizing the case of text in a
database.

Query:

SELECT UPPER('hello') AS UpperCase;


SELECT LOWER('HELLO') AS LowerCase;

Output:

HELLO
hello

4. LENGTH()

LENGTH() returns the length of a string in bytes. This can be useful for
working with multi-byte character sets.

Query:

11
DBMS

SELECT LENGTH('Hello') AS LengthInBytes;

Output:

5. REPLACE()

The REPLACE() function replaces occurrences of a substring within a string


with another substring. This is useful for cleaning up data, such as replacing
invalid characters or formatting errors.

Query:

SELECT REPLACE('Hello World', 'World', 'SQL') AS UpdatedString;

Output:

Hello SQL

6. SUBSTRING() / SUBSTR()

The SUBSTRING() (or SUBSTR()) function is used to extract a substring from a


string, starting from a specified position. It is especially useful when we need
to extract a specific part of a string, like extracting the domain from an email
address.

Query:

SELECT SUBSTRING('Hello World', 1, 5) AS SubStringExample;

Output:

Hello

7. LEFT() and RIGHT()

The LEFT() and RIGHT() functions allow you to extract a specified number of
characters from the left or right side of a string, respectively. It is used for
truncating strings for display.

Query:

SELECT LEFT('Hello World', 5) AS LeftString;


SELECT RIGHT('Hello World', 5) AS RightString;

Output:

Hello
World

12
DBMS

8. INSTR()

The INSTR() function is used to find the position of the first occurrence of a
substring within a string. It returns the position (1-based index) of the
substring. If the substring is not found, it returns 0. This function is
particularly useful for locating specific characters or substrings in text data.

Query:

SELECT INSTR('Hello World', 'World') AS SubstringPosition;

Output:

9. TRIM()

The TRIM() function removes leading and trailing spaces (or other specified
characters) from a string. By default, it trims spaces but can also remove
specific characters using TRIM(character FROM string). This is helpful for
cleaning text data, such as user inputs or database records.

Query:

SELECT TRIM(' ' FROM ' Hello World ') AS TrimmedString;

Output:

Hello World

10. REVERSE()

The REVERSE() function reverses the characters in a string. It’s useful in


situations where we need to process data backward, such as for password
validation or certain pattern matching.

Query:

SELECT REVERSE('Hello') AS ReversedString;

Output:

olleH

Advanced String Functions

In SQL, beyond the basic string functions, there are several advanced string
functions that can help you manipulate and process string data more
effectively. These are the some additional SQL Functions.

13
DBMS

11. ASCII()

The ASCII() function returns the ASCII value of a single character. This is
helpful when we need to find the numeric code corresponding to a character,
often used in encoding and decoding text.

Query:

SELECT ascii('t');

Output:

116

12. CONCAT_WS()

CONCAT_WS() stands for "Concatenate With Separator." It allows us to join


multiple strings with a specific separator between them. This is ideal when
we need to merge columns like first name and last name with a custom
separator.

Query:

SELECT CONCAT_WS('_', 'geeks', 'for', 'geeks');

Output:

geeks_for_geeks

13. FIND_IN_SET()

The FIND_IN_SET() function returns the position of a value within a comma-


separated list. This is especially useful for finding out where an element
exists in a string of values (e.g., tags, categories).

Query:

SELECT FIND_IN_SET('b', 'a, b, c, d, e, f');

Output:

14. FORMAT()

The FORMAT() function is used to format a number as a string in a specific


way, often with commas for thousands or with a specific number of decimal
places. It is handy when you need to display numbers in a user-friendly
format.

14
DBMS

Query:

SELECT FORMAT(0.981 * 100, 'N2') + '%' AS PercentageOutput;

Output:

‘98.10%’

15. LCASE()

The LCASE() function converts all characters in a string to lowercase. It helps


standardize text data, especially when comparing strings in a case-
insensitive way.

Query:

SELECT LCASE ("GeeksFor Geeks To Learn");

Output:

geeksforgeeks to learn

16. LOCATE()

LOCATE() allows you to find the nth occurrence of a substring in a string. This
is especially useful when you need to locate a specific substring based on its
position.

Query:

SELECT LOCATE('for', 'geeksforgeeks', 1);

Output:

17. LPAD()

LPAD() is used to pad a string to a certain length by adding characters to the


left side of the original string. It is useful when you need to format data to a
fixed length.

Query:

SELECT LPAD('geeks', 8, '0');

Output:

000geeks

18. MID()

15
DBMS

MID() extracts a substring starting from a given position in a string and for a
specified length. It is useful when you want to extract a specific portion of a
string.

Query:

SELECT Mid ("geeksforgeeks", 6, 2);

Output:

fo

19. POSITION()

The POSITION() function finds the position of the first occurrence of a


specified character in a string.

Query:

SELECT POSITION('e' IN 'geeksforgeeks');

Output:

20. REPEAT()

The REPEAT() function repeats a string a specified number of times. It's


useful when you need to duplicate a string or pattern for certain operations.

Query:

SELECT REPEAT('geeks', 2);

Output:

geeksgeeks

21. RPAD()

RPAD() pads the right side of a string with specified characters to a fixed
length. This is often used to format text or numbers to a desired size.

Query:

RPAD('geeks', 8, '0');

Output:

‘geeks000’

16
DBMS

22. RTRIM()

RTRIM() removes trailing characters from the right side of a string. By


default, it removes spaces, but you can specify other characters as well.

Query:

RTRIM('geeksxyxzyyy', 'xyz');

Output:

‘geeks’

23. SPACE()

The SPACE() function generates a string consisting of a specified number of


spaces. This is useful when you need to format output or create padding in
your queries.

Query:

SELECT SPACE(7);

Output:

‘ ‘

24. STRCMP()

STRCMP() compares two strings and returns an integer value based on their
lexicographical comparison. This is useful for sorting or checking equality
between two strings. STRCMP(string1, string2) returns:

 0 if both strings are equal.

 A negative value if string1 is less than string2.

 A positive value if string1 is greater than string2.

Query:

SELECT STRCMP('[Link]', '[Link]');

Output:

Basic Operators in Relational Algebra

Relational algebra consists of various basic operators that help us to fetch


and manipulate data from relational tables in the database to perform

17
DBMS

certain operations on relational data. Basic operators are fundamental


operations that include selection (σ), projection (π), union (U), set difference
(−), Cartesian product (×), and rename (ρ).

Operators in Relational Algebra

1. Selection(σ)

The Selection Operation is basically used to filter out rows from a given table
based on certain given condition. It basically allows us to retrieve only those
rows that match the condition as per condition passed during SQL Query.

Example: If we have a relation R with attributes A, B, and C, and we want to


select tuples where C > 3, we write:

A B C

1 2 4

18
DBMS

A B C

2 2 3

3 2 3

4 3 4

σ(c>3)(R) will select the tuples which have c more than 3.

Output:

A B C

1 2 4

4 3 4

Explanation: The selection operation only filters rows but does not display
or change their order. The projection operator is used for displaying specific
columns.

2. Projection(π)

While Selection operation works on rows, similarly projection operation of


relational algebra works on columns. It basically allows us to pick specific
columns from a given relational table based on the given condition and
ignoring all the other remaining columns.

Example: Suppose we want columns B and C from Relation R.

π(B,C)(R) will show following columns.

Output:

19
DBMS

B C

2 4

2 3

3 4

Explanation: By Default, projection operation removes duplicate values.

3. Union(U)

The Union Operator is basically used to combine the results of two queries
into a single result. The only condition is that both queries must return same
number of columns with same data types. Union operation in relational
algebra is the same as union operation in set theory.

Example: Consider the following table of Students having different optional


subjects in their course.

FRENCH

Student_Nam Roll_Numbe
e r

Ram 01

Mohan 02

Vivek 13

Geeta 17

GERMAN

20
DBMS

Student_Nam Roll_Numbe
e r

Vivek 13

Geeta 17

Shyam 21

Rohan 25

If FRENCH and GERMAN relations represent student names in two subjects,


we can combine their student names as follows:

π(Student_Name)(FRENCH) U π(Student_Name)(GERMAN)

Output:

Student_Na
me

Ram

Mohan

Vivek

Geeta

Shyam

Rohan

21
DBMS

Student_Na
me

Explanation: The only constraint in the union of two relations is that both
relations must have the same set of Attributes.

4. Set Difference(-)

Set difference basically provides the rows that are present in one table, but
not in another tables. Set Difference in relational algebra is the same set
difference operation as in set theory.

Example: To find students enrolled only in FRENCH but not in GERMAN, we


write:

π(Student_Name)(FRENCH) - π(Student_Name)(GERMAN)

Student_Na
me

Ram

Mohan

Explanation: The only constraint in the Set Difference between two


relations is that both relations must have the same set of Attributes.

5. Rename(ρ)

Rename operator basically allows you to give a temporary name to a specific


relational table or to its columns. It is very useful when we want to avoid
ambiguity, especially in complex Queries. Rename is a unary operation used
for renaming attributes of a relation.

Example: We can rename an attribute B in relation R to D

22
DBMS

A B C

1 2 4

2 2 3

3 2 3

4 3 4

ρ(D/B)R will rename the attribute 'B' of the relation by "D".

Output Table:

A D C

1 2 4

2 2 3

3 2 3

4 3 4

6. Cartesian Product(X)

The Cartesian product combines every row of one table with every row of
another table, producing all the possible combination. It's mostly used as a
precursor to more complex operation like joins. Let’s say A and B, so the
cross product between A X B will result in all the attributes of A followed by
each attribute of B. Each record of A will pair with every record of B.

Relation A:

23
DBMS

Name Age Sex

Ram 14 M

Sona 15 F

Kim 20 M

Relation B:

ID Course

1 DS

2 DBMS

Output: If relation A has 3 rows and relation B has 2 rows, the Cartesian
product A × B will result in 6 rows.

Name Age Sex ID Course

Ram 14 M 1 DS

Ram 14 M 2 DBMS

Sona 15 F 1 DS

Sona 15 F 2 DBMS

Kim 20 M 1 DS

24
DBMS

Name Age Sex ID Course

Kim 20 M 2 DBMS

Explanation: If A has 'n' tuples and B has 'm' tuples then A X B will have
'n*m' tuples.

Read more about Basic Operators

Derived Operators in Relational Algebra

Derived operators are built using basic operators and include operations like
join, intersection, and division. These operators help perform more complex
queries by combining basic operations to meet specific data retrieval needs.

1. Join Operators

Join operations in relational algebra combine data from two or more relations
based on a related attribute, allowing for more complex queries and data
retrieval. Different types of joins include:

1.1 Inner Join

An inner join combines rows from two relations based on a matching


condition and only returns rows where there is a match in both relations. If a
record in one relation doesn't have a corresponding match in the other, it is
excluded from the result. This is the most common type of join.

a. Conditional Join:

 A conditional join is an inner join where the matching condition can


involve any comparison operator like equals (=), greater than (>), etc.

 Example: Joining Employees and Departments on DepartmentID wher


e Salary > 50000 will return employees in departments with a salary
greater than 50,000

b. Equi Join:

 An equi join is a type of conditional join where the condition is


specifically equality (=) between columns from both relations.

 Example: Joining Customers and Orders on CustomerID where both


relations have this column, returning only matching records.

c. Natural Join:

25
DBMS

 A natural join automatically combines relations based on columns with


the same name and type, removing duplicate columns in the result. It’s
a more efficient way of joining.

 Example: Joining Students and Enrollments where StudentID is


common in both, and the result contains only unique columns.

1.2 Outer Join

An outer join returns all rows from one relation, and the matching rows from
the other relation. If there is no match, the result will still include all rows
from the outer relation with NULL values in the columns from the unmatched
relation.

a. Left Outer Join:

 A left outer join returns all rows from the left relation and the matching
rows from the right relation.

 If there is no match, the result will include NULL values for the right
relation’s attributes.

 Example: Joining Employees with Departments using a left outer join


ensures all employees are listed, even those who aren't assigned to
any department, with NULL values for the department columns.

b. Right Outer Join:

 A right outer join returns all rows from the right relation and the
matching rows from the left relation.

 If no match exists, the left relation's columns will contain NULL values.

 Example: Joining Departments with Employees using a right outer join


includes all departments, even those with no employees assigned,
filling unmatched employee columns with NULL.

c. Full Outer Join:

 A full outer join returns all rows when there is a match in either the left
or right relation.

 If a row from one relation does not have a match in the


other, NULL values are included for the missing side.

26
DBMS

 Example: Joining Customers and Orders using a full outer join will
return all customers and orders, even if there’s no corresponding order
for a customer or no customer for an order.

2. Set Intersection(∩)

Set Intersection basically allows to fetches only those rows of data that are
common between two sets of relational tables. Set Intersection in relational
algebra is the same set intersection operation in set theory.

Example: Consider the following table of Students having different optional


subjects in their course.

Relation FRENCH

Student_Nam Roll_Numbe
e r

Ram 01

Mohan 02

Vivek 13

Geeta 17

Relation GERMAN

Student_Nam Roll_Numbe
e r

Vivek 13

Geeta 17

Shyam 21

27
DBMS

Student_Nam Roll_Numbe
e r

Rohan 25

From the above table of FRENCH and GERMAN, the Set Intersection is used
as follows:

π(Student_Name)(FRENCH ∩ π(Student_Name)(GERMAN)

Output:

Student_Na
me

Vivek

Geeta

Explanation: The only constraint in the Set Difference between two


relations is that both relations must have the same set of Attributes.

3. Division (÷)

The Division Operator is used to find tuples in one relation that are related to
all tuples in another relation. It’s typically used for "for all" queries.

Student_Course (Dividend Table):

Student_ Course_
ID ID

101 C1

101 C2

28
DBMS

Student_ Course_
ID ID

102 C1

103 C1

103 C2

Course (Divisor Table):

Course_
ID

C1

C2

Example: Query is to find students who are enrolled in all courses listed in
the Course table. In this case, students must be enrolled in both C1 and C2.

Student_Course(Student_ID, Course_ID)÷ Course(Course_ID)

Output:

Student_
ID

101

103

29
DBMS

elational Calculus

Relational calculus is a non-procedural query language used in the context of


relational algebra. It focuses on what data to retrieve, rather than how to
retrieve it, making it different from relational algebra, which is procedural. In
relational calculus, queries are expressed using logical formulas that describe
the desired result, without specifying the exact steps to get there.

There are two types of Relational Calculus

1. Tuple Relational Calculus(TRC)

2. Domain Relational Calculus(DRC)

Tuple Relational Calculus (TRC) in DBMS

Last Updated : 24 Jul, 2025

Tuple Relational Calculus (TRC) is a non-procedural query language used


to retrieve data from relational databases by describing the properties of
the required data (not how to fetch it).
It is based on first-order predicate logic and uses tuple variables to
represent rows of tables.

Syntax: The basic syntax of TRC is as follows:

{ t | P(t) }

 t: Tuple variable (row placeholder)

 P(t): Predicate condition to satisfy

 {}: Denotes a set of result tuples

Logical Operators in TRC:

 ∧: AND

 ∨: OR

 ¬: NOT

30
DBMS

Quantifiers:

 ∃ t ∈ r (Q(t)) → There exists a tuple t in relation r satisfying predicate


Q(t)

 ∀ t ∈ r (Q(t)) → For all tuples t in relation r, predicate Q(t) holds

For example, let's say we have a table called "Employees" with the
following attributes:

Employee ID

Name

Salary

Department
ID

To retrieve the names of all employees who earn more than $50,000 per
year, we can use the following TRC query:

{ t | Employees(t) ∧ [Link] > 50000 }

Explanation:

 Employees(t) means t is a tuple from the Employees table.

 ∧ (AND) is used to add a condition on salary.

 The result is a set of tuples where each employee earns more than
$50,000.

TRC is non-procedural - it specifies what data to retrieve, not how to retrieve


it.

While expressive, TRC is more abstract and mainly used in academic or


theoretical contexts, not practical database systems.

Tuple Relational Query

In Tuple Calculus, a query is expressed as

31
DBMS

{t| P(t)}

 t represents the resulting tuples.

 P(t) is a predicate (a condition that must be true for t to be included in


the resul

P(t) may have various conditions logically combined with OR (∨), AND (∧),
NOT(¬).

It also uses quantifiers:

 ∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that


predicate Q(t) is true.

 ∀ t ∈ r (Q(t)) = Q(t) is true "for all" tuples in relation r.

Domain Relational Calculus (DRC)

Domain Relational Calculus is similar to Tuple Relational Calculus, where it


makes a list of the attributes that are to be chosen from the relations as per
the conditions.

{<a1,a2,a3,.....an> | P(a1,a2,a3,.....an)}

where a1,a2,...an are the attributes of the relation and P is the condition.

Tuple Relational Calculus Examples

Table Customer

Customer Stre
name et City

Saurabh A7 Patiala

Jalandh
Mehak B6
ar

Ludhian
Sumiti D9
a

32
DBMS

Customer Stre
name et City

Ria A5 Patiala

Table Branch

Branch Branch
name City

ABC Patiala

DEF Ludhiana

GHI Jalandhar

Table Account

Account Branch Balan


number name ce

1111 ABC 50000

1112 DEF 10000

1113 GHI 9000

1114 ABC 7000

Table Loan

33
DBMS

Loan Branch Amou


number name nt

L33 ABC 10000

L35 DEF 15000

L49 GHI 9000

L98 DEF 65000

Table Borrower

Customer Loan
name number

Saurabh L33

Mehak L49

Ria L98

Table Depositor

Customer Account
name number

Saurabh 1111

Mehak 1113

34
DBMS

Customer Account
name number

Suniti 1114

Example 1: Find the loan number, branch, and amount of loans greater than
or equal to 10000 amount.

{t| t ∈ loan ∧ t[amount]>=10000}

Resulting relation:

Loan Branch Amou


number name nt

L33 ABC 10000

L35 DEF 15000

L98 DEF 65000

In the above query, t[amount] is known as a tuple variable.

Example 2: Find the loan number for each loan of an amount greater or
equal to 10000.

{t| ∃ s ∈ loan(t[loan number] = s[loan number]


∧ s[amount]>=10000)}
Resulting relation:

Loan
number

L33

35
DBMS

Loan
number

L35

L98

Example 3: Find the names of all customers who have a loan and an
account at the bank.

{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name])


∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])}
Resulting relation:

Customer
name

Saurabh

Mehak

Example 4: Find the names of all customers having a loan at the "ABC"
branch.

{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-
number]))}

Resulting relation:

Customer
name

Saurabh

Key Concepts:

36
DBMS

 TRC does not specify execution steps, only the condition of result.

 It focuses on what to retrieve, not how.

 Based on variables, predicates, and quantifiers.

 More theoretical, often used in database theory, formal methods,


and GATE questions.

Comparison: TRC vs Relational Algebra

Relationa
Feature TRC l Algebra

Type Non-procedural Procedural

How to
What to retrieve
Focus retrieve

Set-based
Logical expressions
Expression Style operators

Directly
Abstract, not directly
convertible
executable
Execution to query

Basis for
Theoretical foundation query
Use in DBMS execution

Tuple Relational Calculus (TRC) in DBMS

Last Updated : 24 Jul, 2025

37
DBMS

Tuple Relational Calculus (TRC) is a non-procedural query language used


to retrieve data from relational databases by describing the properties of
the required data (not how to fetch it).
It is based on first-order predicate logic and uses tuple variables to
represent rows of tables.

Syntax: The basic syntax of TRC is as follows:

{ t | P(t) }

 t: Tuple variable (row placeholder)

 P(t): Predicate condition to satisfy

 {}: Denotes a set of result tuples

Logical Operators in TRC:

 ∧: AND

 ∨: OR

 ¬: NOT

Quantifiers:

 ∃ t ∈ r (Q(t)) → There exists a tuple t in relation r satisfying predicate


Q(t)

 ∀ t ∈ r (Q(t)) → For all tuples t in relation r, predicate Q(t) holds

For example, let's say we have a table called "Employees" with the
following attributes:

Employee ID

Name

Salary

Department
ID

38
DBMS

To retrieve the names of all employees who earn more than $50,000 per
year, we can use the following TRC query:

{ t | Employees(t) ∧ [Link] > 50000 }

Explanation:

 Employees(t) means t is a tuple from the Employees table.

 ∧ (AND) is used to add a condition on salary.

 The result is a set of tuples where each employee earns more than
$50,000.

TRC is non-procedural - it specifies what data to retrieve, not how to retrieve


it.

While expressive, TRC is more abstract and mainly used in academic or


theoretical contexts, not practical database systems.

Tuple Relational Query

In Tuple Calculus, a query is expressed as

{t| P(t)}

 t represents the resulting tuples.

 P(t) is a predicate (a condition that must be true for t to be included in


the resul

P(t) may have various conditions logically combined with OR (∨), AND (∧),
NOT(¬).

It also uses quantifiers:

 ∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that


predicate Q(t) is true.

 ∀ t ∈ r (Q(t)) = Q(t) is true "for all" tuples in relation r.

Domain Relational Calculus (DRC)

Domain Relational Calculus is similar to Tuple Relational Calculus, where it


makes a list of the attributes that are to be chosen from the relations as per
the conditions.

{<a1,a2,a3,.....an> | P(a1,a2,a3,.....an)}

where a1,a2,...an are the attributes of the relation and P is the condition.

39
DBMS

Tuple Relational Calculus Examples

Table Customer

Customer Stre
name et City

Saurabh A7 Patiala

Jalandh
Mehak B6
ar

Ludhian
Sumiti D9
a

Ria A5 Patiala

Table Branch

Branch Branch
name City

ABC Patiala

DEF Ludhiana

GHI Jalandhar

Table Account

Account Branch Balan


number name ce

1111 ABC 50000

40
DBMS

Account Branch Balan


number name ce

1112 DEF 10000

1113 GHI 9000

1114 ABC 7000

Table Loan

Loan Branch Amou


number name nt

L33 ABC 10000

L35 DEF 15000

L49 GHI 9000

L98 DEF 65000

Table Borrower

Customer Loan
name number

Saurabh L33

41
DBMS

Customer Loan
name number

Mehak L49

Ria L98

Table Depositor

Customer Account
name number

Saurabh 1111

Mehak 1113

Suniti 1114

Example 1: Find the loan number, branch, and amount of loans greater than
or equal to 10000 amount.

{t| t ∈ loan ∧ t[amount]>=10000}

Resulting relation:

Loan Branch Amou


number name nt

L33 ABC 10000

L35 DEF 15000

L98 DEF 65000

42
DBMS

In the above query, t[amount] is known as a tuple variable.

Example 2: Find the loan number for each loan of an amount greater or
equal to 10000.

{t| ∃ s ∈ loan(t[loan number] = s[loan number]


∧ s[amount]>=10000)}
Resulting relation:

Loan
number

L33

L35

L98

Example 3: Find the names of all customers who have a loan and an
account at the bank.

{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name])


∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])}
Resulting relation:

Customer
name

Saurabh

Mehak

Example 4: Find the names of all customers having a loan at the "ABC"
branch.

43
DBMS

{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-
number]))}

Resulting relation:

Customer
name

Saurabh

Key Concepts:

 TRC does not specify execution steps, only the condition of result.

 It focuses on what to retrieve, not how.

 Based on variables, predicates, and quantifiers.

 More theoretical, often used in database theory, formal methods,


and GATE questions.

Comparison: TRC vs Relational Algebra

Relation
al
Feature TRC Algebra

Procedur
Non-procedural
Type al

How to
What to retrieve
Focus retrieve

Set-
Logical expressions based
Expression Style operators

44
DBMS

Relation
al
Feature TRC Algebra

Directly
Abstract, not directly convertib
executable le to
Execution query

Basis for
query
Theoretical foundation
executio
Use in DBMS n

45

You might also like