SlideShare a Scribd company logo
CHAPTER SIX
QUERY LANGUAGES
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
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
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
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
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
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
… 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
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
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
… 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
Example
 The name, branch column of student table are renamed as
newname and newbranch respectively
12
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
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
15
… 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
Example
 Union operation combines values in R1, R2 by
removing duplicate ones.
17
18
19
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
Example
 Consider the below tables Employee and Student. Perform Set Difference
operation as (Employee-Student).
21
Result after performing Set difference (Employee-Student)
22
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.
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
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
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
Types of Relational Calculus in DBMS
Relational Calculus is Two Types:
 Tuple Relational Calculus (TRC)
 Domain Relational Calculus (DRC)
27
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
Example
29
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
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
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)}
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
Example
34
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
Conts’…
36
Result of the DRC query will be:
THANK YOU !
37

More Related Content

PPTX
BASICS OF STRUCTURED QUEERY LANGUAGE.PPT
PPTX
Relational Algebra in Database Systems.pptx
PPTX
Relational algebra calculus
PPTX
Relational Model,relational calulus.pptx
PPTX
UNIT-2 Relation algebra&RelationalCalculus.pptx
PPTX
U-2.pptx igxitditdursruzyezrzmyayeTeYrsyrsurzursursursursursrusursruzruRuUeYe...
PPT
Query Decomposition and data localization
PDF
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdf
BASICS OF STRUCTURED QUEERY LANGUAGE.PPT
Relational Algebra in Database Systems.pptx
Relational algebra calculus
Relational Model,relational calulus.pptx
UNIT-2 Relation algebra&RelationalCalculus.pptx
U-2.pptx igxitditdursruzyezrzmyayeTeYrsyrsurzursursursursursrusursruzruRuUeYe...
Query Decomposition and data localization
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdf

Similar to database chapter 6.pptx advanced database (20)

PPTX
Relational data model
PDF
RelationalAlgebra-RelationalCalculus-SQL.pdf
PPTX
Relational Algebra.Pptxjejejjdjdh jsjsjd
PDF
chapter 6 Relational Algebra and calculus.pdf
PPT
Relational Algebra
PPT
Mapping of Multivalued attributesER-to-Relational Mapping Algorithm.ppt
PDF
Cs501 rel algebra
PDF
Relational_Algebra_Calculus Operations.pdf
PPTX
Lect 3 Relational Calculus.pptx from mmu
PDF
Relational Database and Relational Algebra
PPT
Intro to relational model
PPT
Relational algebra-and-relational-calculus
PDF
Dbms module ii
PPT
relational algebra and it's implementation
PPTX
Chapter-6 Relational Algebra
PPTX
DBMS UNIT 3
PPTX
Query and optimizing operating system.pptx
PPTX
Lect - 12 solve d.pptx
PPT
Relational algebra.pptx
Relational data model
RelationalAlgebra-RelationalCalculus-SQL.pdf
Relational Algebra.Pptxjejejjdjdh jsjsjd
chapter 6 Relational Algebra and calculus.pdf
Relational Algebra
Mapping of Multivalued attributesER-to-Relational Mapping Algorithm.ppt
Cs501 rel algebra
Relational_Algebra_Calculus Operations.pdf
Lect 3 Relational Calculus.pptx from mmu
Relational Database and Relational Algebra
Intro to relational model
Relational algebra-and-relational-calculus
Dbms module ii
relational algebra and it's implementation
Chapter-6 Relational Algebra
DBMS UNIT 3
Query and optimizing operating system.pptx
Lect - 12 solve d.pptx
Relational algebra.pptx
Ad

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
web development for engineering and engineering
PPTX
Sustainable Sites - Green Building Construction
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
composite construction of structures.pdf
PPT
Project quality management in manufacturing
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
web development for engineering and engineering
Sustainable Sites - Green Building Construction
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
bas. eng. economics group 4 presentation 1.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
composite construction of structures.pdf
Project quality management in manufacturing
Internet of Things (IOT) - A guide to understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Ad

database chapter 6.pptx advanced database

  • 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
  • 15. 15
  • 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
  • 17. Example  Union operation combines values in R1, R2 by removing duplicate ones. 17
  • 18. 18
  • 19. 19
  • 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
  • 22. Result after performing Set difference (Employee-Student) 22
  • 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
  • 36. Conts’… 36 Result of the DRC query will be: