SlideShare una empresa de Scribd logo
1 de 44
Relational Databases
Relational Algebra (1)
Select, project , set
Theories
Originally Prepared by Jennifer Widom
What is an “Algebra”?
Mathematical system consisting of:
 Operands --- variables or values from which new values can
be constructed.
 Operators --- symbols denoting procedures that construct
new values from given values.
In R1 x R2,
R1 and R2 are the operands while x is the operator . The
operator is applied on operands and new value is
constructed.
Relational Algebra(1970)
• It is the base of SQL
• It is procedural and formal query language .
– What to do and how to do .
• It is the collection of math-e metical expression . Not
implemented anywhere .
• It's an algebra that forms the underpinnings of
implemented languages like SQL.
6
Relational Algebra Operations
Relational Algebra (1)
Examples: simple University admissions database
University(uName,city,enr)
Student(sID,sName,GPA,HS)
Apply(sID,uName,major,dec)
uName city enr sID sName GPA HS sID uName major dec
University Student Apply
Simplest query: relation name
uName city enr sID sName GPA HS sID uName major dec
University Student Apply
Relational Algebra (1)
Student
we'll get as a result a copy of the student relation
We will further use operators to filter, slice, combine
9
Selection (or Restriction)
• predicate (R)
– Works on a single relation R and defines
a relation that contains only those tuples
(rows) of R that satisfy the specified
condition (predicate).
15
Example - Selection (or Restriction)
• List all staff with a salary greater
than £10,000.
salary > 10000 (Staff)
Solution
• List all staff with a salary greater than
£10,000.
• salary > 10000 (Staff)
Select operator (σcondition R): picks certain rows
Students with GPA>3.7
σ GPA>3.7 (Students)
Student
Relational Algebra (1)
sID sName GPA HS
1 Ahmed 3.4 1200
2 Ali 3.75 2000
sID sName GPA HS
2 Ali 3.75 2000
Select operator (σcondition R): picks certain rows
Students with GPA>3.7 and HS<1000
σ GPA>3.7 ^ HS<1000 (Students)
Applications to Comsats with CS as major
σ major=‘CS’ ^ uName = ‘Comsats’ (Apply)
uName city enr sID sName GPA HS sID uName major dec
University Student Apply
Relational Algebra (1)
19
Projection
• col1, . . . , coln(R)
– Works on a single relation R and defines a
relation that contains a vertical subset of R,
extracting the values of specified attributes
and eliminating duplicates.
22
Example - Projection
• Produce a list of salaries for all staff, showing
only staffNo, salary details.
staffNo, salary(Staff)
Example (student)
Roll no Name Age
1 sara 19
2 Ali 20
3 ahad 18
Answers
• Retrieve the roll no from table
– 𝜋 𝑟𝑜𝑙𝑙 𝑛𝑜 𝑠𝑡𝑢𝑑𝑒𝑛𝑡
• Retrieve the Name of student whose roll no is
2
– 𝜋 name (𝜎 𝑟𝑜𝑙𝑙 𝑛𝑜 = 2(𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ))
Project operator (∏A1,A2,A3,…An R): picks certain columns
ID and major of all applications
∏sID,major (Apply)
Relational Algebra (1)
sID uName major dec
1 Comsats CS No
1 Comsats EE Yes
2 NUST CS No
Apply
sID major
1 CS
1 EE
2 CS
To pick both rows and columns…
ID and name of students with GPA>3.7
∏sID,sName ( σ GPA>3.7 (Students) )
Relational Algebra (1)
sID sName GPA HS
1 Ahmed 3.4 1200
2 Ali 3.75 2000
Student
sID sName
2 Ali
Duplicates
List of application’s majors and decisions
∏major,dec (Apply)
The semantics of relational algebra says that duplicates are always eliminated. So if you
run a query that would logically have a lot of duplicate values, you just get one value for
each result.
Relational Algebra (1)
sID uName major dec
1 Comsats CS No
1 Comsats EE Yes
2 NUST CS No
Apply
major dec
CS No
EE Yes
30
Union
• R  S
– Union of two relations R and S defines a relation that
contains all the tuples of R, or S, or both R and S, duplicate
tuples being eliminated.
– R and S must be union-compatible.
• If R and S have I and J tuples, respectively, union is
obtained by concatenating them into one relation with
a maximum of (I + J) tuples.
31
Example - Union
• List all cities where there is either
a branch office or a property for
rent.
city(Branch) 
city(PropertyForRent)
32
Intersection
• R  S
– Defines a relation consisting of the set
of all tuples that are in both R and S.
– R and S must be union-compatible.
• Expressed using basic operations:
R  S = R – (R – S)
33
Example - Intersection
• List all cities where there is both
a branch office and at least one
property for rent.
city(Branch) 
city(PropertyForRent)
35
Set Difference
• R – S
– Defines a relation consisting of the
tuples that are in relation R, but
not in S.
– R and S must be union-compatible.
Example
• Find the name of a person who is a student
but not instructor
• 𝜋 𝑛𝑎𝑚𝑒 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 )-𝜋 𝑛𝑎𝑚𝑒 (𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑜𝑟 )
39
Cartesian product
• R X S
– Defines a relation that is the
concatenation of every tuple of
relation R with every tuple of
relation S.
Cross-product (X): combine two relations
( Cartesian product)
X =
Student.sID sName GPA HS Apply.sID uName major dec
1 Ahmed 3.4 1200 1 Comsats CS No
1 Ahmed 3.4 1200 1 Comsats EE Yes
1 Ahmed 3.4 1200 2 NUST CS No
2 Ali 3.75 2000 1 Comsats CS No
2 Ali 3.75 2000 1 Comsats EE Yes
2 Ali 3.75 2000 2 NUST CS No
sID sName GPA HS
1 Ahmed 3.4 1200
2 Ali 3.75 2000
sID uName major dec
1 Comsats CS No
1 Comsats EE Yes
2 NUST CS No
Student Apply
Relational Algebra (1)
Cross-product (X): combine two relations
(Cartesian product)
Names and GPAs of students with HS>1000 who applied to CS
and were rejected
∏sName,GPA ( σ Student.sID=Apply.sID ^ HS>1000 ^ major=‘CS’ ^ dec=‘No’ (Students x Apply))
uName city enr sID sName GPA HS sID uName major dec
University Student Apply
Relational Algebra (1)
Relational Algebra (1)
Query (expression) on set of relations produces
relation as a result
 Simplest query: relation name
 Use operators to filter, slice, combine
 Operators so far: select, project, cross-product,
natural join, theta join , rename operators
Names and GPAs of students with HS>1000
who applied to CS
and were rejected

Más contenido relacionado

La actualidad más candente

Normalizacion boyce codd_4_fn
Normalizacion boyce codd_4_fnNormalizacion boyce codd_4_fn
Normalizacion boyce codd_4_fnLuis Jherry
 
The Graph Traversal Programming Pattern
The Graph Traversal Programming PatternThe Graph Traversal Programming Pattern
The Graph Traversal Programming PatternMarko Rodriguez
 
Data Quality for Machine Learning Tasks
Data Quality for Machine Learning TasksData Quality for Machine Learning Tasks
Data Quality for Machine Learning TasksHima Patel
 
Fundamentos de BD - Unidad 2 Modelo Entidad Relacion
Fundamentos de BD - Unidad 2 Modelo Entidad RelacionFundamentos de BD - Unidad 2 Modelo Entidad Relacion
Fundamentos de BD - Unidad 2 Modelo Entidad RelacionJosé Antonio Sandoval Acosta
 
Data mining fp growth
Data mining fp growthData mining fp growth
Data mining fp growthShihab Rahman
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With REdureka!
 
Advanced SQL
Advanced SQLAdvanced SQL
Advanced SQLSabiha M
 
Statistics for data scientists
Statistics for  data scientistsStatistics for  data scientists
Statistics for data scientistsAjay Ohri
 
RDBMS to Graph
RDBMS to GraphRDBMS to Graph
RDBMS to GraphNeo4j
 
FAIR and metadata standards - FAIRsharing and Neuroscience
FAIR and metadata standards - FAIRsharing and NeuroscienceFAIR and metadata standards - FAIRsharing and Neuroscience
FAIR and metadata standards - FAIRsharing and NeuroscienceSusanna-Assunta Sansone
 
Graph Data Science DEMO for fraud analysis
Graph Data Science DEMO for fraud analysisGraph Data Science DEMO for fraud analysis
Graph Data Science DEMO for fraud analysisNeo4j
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashingsathish sak
 
What is Apriori Algorithm | Edureka
What is Apriori Algorithm | EdurekaWhat is Apriori Algorithm | Edureka
What is Apriori Algorithm | EdurekaEdureka!
 
Database presentation
Database presentationDatabase presentation
Database presentationwebhostingguy
 
data modeling and models
data modeling and modelsdata modeling and models
data modeling and modelssabah N
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
Data and database administration(database)
Data and database administration(database)Data and database administration(database)
Data and database administration(database)welcometofacebook
 

La actualidad más candente (20)

Normalizacion boyce codd_4_fn
Normalizacion boyce codd_4_fnNormalizacion boyce codd_4_fn
Normalizacion boyce codd_4_fn
 
The Graph Traversal Programming Pattern
The Graph Traversal Programming PatternThe Graph Traversal Programming Pattern
The Graph Traversal Programming Pattern
 
Data Quality for Machine Learning Tasks
Data Quality for Machine Learning TasksData Quality for Machine Learning Tasks
Data Quality for Machine Learning Tasks
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Data models
Data modelsData models
Data models
 
Fundamentos de BD - Unidad 2 Modelo Entidad Relacion
Fundamentos de BD - Unidad 2 Modelo Entidad RelacionFundamentos de BD - Unidad 2 Modelo Entidad Relacion
Fundamentos de BD - Unidad 2 Modelo Entidad Relacion
 
Data mining fp growth
Data mining fp growthData mining fp growth
Data mining fp growth
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
 
Advanced SQL
Advanced SQLAdvanced SQL
Advanced SQL
 
Statistics for data scientists
Statistics for  data scientistsStatistics for  data scientists
Statistics for data scientists
 
RDBMS to Graph
RDBMS to GraphRDBMS to Graph
RDBMS to Graph
 
FAIR and metadata standards - FAIRsharing and Neuroscience
FAIR and metadata standards - FAIRsharing and NeuroscienceFAIR and metadata standards - FAIRsharing and Neuroscience
FAIR and metadata standards - FAIRsharing and Neuroscience
 
Graph Data Science DEMO for fraud analysis
Graph Data Science DEMO for fraud analysisGraph Data Science DEMO for fraud analysis
Graph Data Science DEMO for fraud analysis
 
Indexing and Hashing
Indexing and HashingIndexing and Hashing
Indexing and Hashing
 
What is Apriori Algorithm | Edureka
What is Apriori Algorithm | EdurekaWhat is Apriori Algorithm | Edureka
What is Apriori Algorithm | Edureka
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
data modeling and models
data modeling and modelsdata modeling and models
data modeling and models
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithm
 
Data and database administration(database)
Data and database administration(database)Data and database administration(database)
Data and database administration(database)
 

Similar a Lecture-3 Relational Algebra I.pptx

Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational modelATS SBGI MIRAJ
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculusSalman Vadsarya
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational AlgebraPyingkodi Maran
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusemailharmeet
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Raj vardhan
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATELShashi Patel
 
1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdfKavinilaa
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasexabhaysonone0
 
Relational algebra operations
Relational algebra operationsRelational algebra operations
Relational algebra operationsSanthiNivas
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.pptSreenivas R
 
3 relational model
3 relational model3 relational model
3 relational modelUtkarsh De
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbmsshekhar1991
 

Similar a Lecture-3 Relational Algebra I.pptx (20)

R Algebra.ppt
R Algebra.pptR Algebra.ppt
R Algebra.ppt
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 
Ra Revision
Ra RevisionRa Revision
Ra Revision
 
Relational+algebra (1)
Relational+algebra (1)Relational+algebra (1)
Relational+algebra (1)
 
07.04 joins
07.04 joins07.04 joins
07.04 joins
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
 
354 ch6
354 ch6354 ch6
354 ch6
 
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY  MILAN PATELCHAPTER 2 DBMS IN EASY WAY BY  MILAN PATEL
CHAPTER 2 DBMS IN EASY WAY BY MILAN PATEL
 
1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
 
Relational algebra operations
Relational algebra operationsRelational algebra operations
Relational algebra operations
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.ppt
 
3 relational model
3 relational model3 relational model
3 relational model
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Algebra
AlgebraAlgebra
Algebra
 

Más de HanzlaNaveed1

Lecture 25 Link Layer - Error detection and Multiple Access.pptx
Lecture 25 Link Layer - Error detection and Multiple Access.pptxLecture 25 Link Layer - Error detection and Multiple Access.pptx
Lecture 25 Link Layer - Error detection and Multiple Access.pptxHanzlaNaveed1
 
Lecture 23 DHCP and NAT.pptx
Lecture 23 DHCP and NAT.pptxLecture 23 DHCP and NAT.pptx
Lecture 23 DHCP and NAT.pptxHanzlaNaveed1
 
Lecture 22 What inside the Router.pptx
Lecture 22 What inside the Router.pptxLecture 22 What inside the Router.pptx
Lecture 22 What inside the Router.pptxHanzlaNaveed1
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxHanzlaNaveed1
 
Lecture 06 and 07.pptx
Lecture 06 and 07.pptxLecture 06 and 07.pptx
Lecture 06 and 07.pptxHanzlaNaveed1
 
Lecture 26 Link Layer .pptx
Lecture 26 Link Layer .pptxLecture 26 Link Layer .pptx
Lecture 26 Link Layer .pptxHanzlaNaveed1
 
Lecture 19 and 20 IP Addressing.pptx
Lecture 19 and 20 IP Addressing.pptxLecture 19 and 20 IP Addressing.pptx
Lecture 19 and 20 IP Addressing.pptxHanzlaNaveed1
 
Lecture 05 OSI Model and IP Protocol Suite.ppt
Lecture 05 OSI Model and IP Protocol Suite.pptLecture 05 OSI Model and IP Protocol Suite.ppt
Lecture 05 OSI Model and IP Protocol Suite.pptHanzlaNaveed1
 

Más de HanzlaNaveed1 (8)

Lecture 25 Link Layer - Error detection and Multiple Access.pptx
Lecture 25 Link Layer - Error detection and Multiple Access.pptxLecture 25 Link Layer - Error detection and Multiple Access.pptx
Lecture 25 Link Layer - Error detection and Multiple Access.pptx
 
Lecture 23 DHCP and NAT.pptx
Lecture 23 DHCP and NAT.pptxLecture 23 DHCP and NAT.pptx
Lecture 23 DHCP and NAT.pptx
 
Lecture 22 What inside the Router.pptx
Lecture 22 What inside the Router.pptxLecture 22 What inside the Router.pptx
Lecture 22 What inside the Router.pptx
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptx
 
Lecture 06 and 07.pptx
Lecture 06 and 07.pptxLecture 06 and 07.pptx
Lecture 06 and 07.pptx
 
Lecture 26 Link Layer .pptx
Lecture 26 Link Layer .pptxLecture 26 Link Layer .pptx
Lecture 26 Link Layer .pptx
 
Lecture 19 and 20 IP Addressing.pptx
Lecture 19 and 20 IP Addressing.pptxLecture 19 and 20 IP Addressing.pptx
Lecture 19 and 20 IP Addressing.pptx
 
Lecture 05 OSI Model and IP Protocol Suite.ppt
Lecture 05 OSI Model and IP Protocol Suite.pptLecture 05 OSI Model and IP Protocol Suite.ppt
Lecture 05 OSI Model and IP Protocol Suite.ppt
 

Último

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Último (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

Lecture-3 Relational Algebra I.pptx

  • 1. Relational Databases Relational Algebra (1) Select, project , set Theories Originally Prepared by Jennifer Widom
  • 2. What is an “Algebra”? Mathematical system consisting of:  Operands --- variables or values from which new values can be constructed.  Operators --- symbols denoting procedures that construct new values from given values. In R1 x R2, R1 and R2 are the operands while x is the operator . The operator is applied on operands and new value is constructed.
  • 3. Relational Algebra(1970) • It is the base of SQL • It is procedural and formal query language . – What to do and how to do . • It is the collection of math-e metical expression . Not implemented anywhere . • It's an algebra that forms the underpinnings of implemented languages like SQL.
  • 4.
  • 5.
  • 7. Relational Algebra (1) Examples: simple University admissions database University(uName,city,enr) Student(sID,sName,GPA,HS) Apply(sID,uName,major,dec) uName city enr sID sName GPA HS sID uName major dec University Student Apply
  • 8. Simplest query: relation name uName city enr sID sName GPA HS sID uName major dec University Student Apply Relational Algebra (1) Student we'll get as a result a copy of the student relation We will further use operators to filter, slice, combine
  • 9. 9 Selection (or Restriction) • predicate (R) – Works on a single relation R and defines a relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate).
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. 15 Example - Selection (or Restriction) • List all staff with a salary greater than £10,000. salary > 10000 (Staff)
  • 16. Solution • List all staff with a salary greater than £10,000. • salary > 10000 (Staff)
  • 17. Select operator (σcondition R): picks certain rows Students with GPA>3.7 σ GPA>3.7 (Students) Student Relational Algebra (1) sID sName GPA HS 1 Ahmed 3.4 1200 2 Ali 3.75 2000 sID sName GPA HS 2 Ali 3.75 2000
  • 18. Select operator (σcondition R): picks certain rows Students with GPA>3.7 and HS<1000 σ GPA>3.7 ^ HS<1000 (Students) Applications to Comsats with CS as major σ major=‘CS’ ^ uName = ‘Comsats’ (Apply) uName city enr sID sName GPA HS sID uName major dec University Student Apply Relational Algebra (1)
  • 19. 19 Projection • col1, . . . , coln(R) – Works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates.
  • 20.
  • 21.
  • 22. 22 Example - Projection • Produce a list of salaries for all staff, showing only staffNo, salary details. staffNo, salary(Staff)
  • 23. Example (student) Roll no Name Age 1 sara 19 2 Ali 20 3 ahad 18
  • 24. Answers • Retrieve the roll no from table – 𝜋 𝑟𝑜𝑙𝑙 𝑛𝑜 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 • Retrieve the Name of student whose roll no is 2 – 𝜋 name (𝜎 𝑟𝑜𝑙𝑙 𝑛𝑜 = 2(𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ))
  • 25. Project operator (∏A1,A2,A3,…An R): picks certain columns ID and major of all applications ∏sID,major (Apply) Relational Algebra (1) sID uName major dec 1 Comsats CS No 1 Comsats EE Yes 2 NUST CS No Apply sID major 1 CS 1 EE 2 CS
  • 26. To pick both rows and columns… ID and name of students with GPA>3.7 ∏sID,sName ( σ GPA>3.7 (Students) ) Relational Algebra (1) sID sName GPA HS 1 Ahmed 3.4 1200 2 Ali 3.75 2000 Student sID sName 2 Ali
  • 27. Duplicates List of application’s majors and decisions ∏major,dec (Apply) The semantics of relational algebra says that duplicates are always eliminated. So if you run a query that would logically have a lot of duplicate values, you just get one value for each result. Relational Algebra (1) sID uName major dec 1 Comsats CS No 1 Comsats EE Yes 2 NUST CS No Apply major dec CS No EE Yes
  • 28.
  • 29.
  • 30. 30 Union • R  S – Union of two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated. – R and S must be union-compatible. • If R and S have I and J tuples, respectively, union is obtained by concatenating them into one relation with a maximum of (I + J) tuples.
  • 31. 31 Example - Union • List all cities where there is either a branch office or a property for rent. city(Branch)  city(PropertyForRent)
  • 32. 32 Intersection • R  S – Defines a relation consisting of the set of all tuples that are in both R and S. – R and S must be union-compatible. • Expressed using basic operations: R  S = R – (R – S)
  • 33. 33 Example - Intersection • List all cities where there is both a branch office and at least one property for rent. city(Branch)  city(PropertyForRent)
  • 34.
  • 35. 35 Set Difference • R – S – Defines a relation consisting of the tuples that are in relation R, but not in S. – R and S must be union-compatible.
  • 36.
  • 37.
  • 38. Example • Find the name of a person who is a student but not instructor • 𝜋 𝑛𝑎𝑚𝑒 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 )-𝜋 𝑛𝑎𝑚𝑒 (𝐼𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑜𝑟 )
  • 39. 39 Cartesian product • R X S – Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S.
  • 40.
  • 41. Cross-product (X): combine two relations ( Cartesian product) X = Student.sID sName GPA HS Apply.sID uName major dec 1 Ahmed 3.4 1200 1 Comsats CS No 1 Ahmed 3.4 1200 1 Comsats EE Yes 1 Ahmed 3.4 1200 2 NUST CS No 2 Ali 3.75 2000 1 Comsats CS No 2 Ali 3.75 2000 1 Comsats EE Yes 2 Ali 3.75 2000 2 NUST CS No sID sName GPA HS 1 Ahmed 3.4 1200 2 Ali 3.75 2000 sID uName major dec 1 Comsats CS No 1 Comsats EE Yes 2 NUST CS No Student Apply Relational Algebra (1)
  • 42. Cross-product (X): combine two relations (Cartesian product) Names and GPAs of students with HS>1000 who applied to CS and were rejected ∏sName,GPA ( σ Student.sID=Apply.sID ^ HS>1000 ^ major=‘CS’ ^ dec=‘No’ (Students x Apply)) uName city enr sID sName GPA HS sID uName major dec University Student Apply Relational Algebra (1)
  • 43. Relational Algebra (1) Query (expression) on set of relations produces relation as a result  Simplest query: relation name  Use operators to filter, slice, combine  Operators so far: select, project, cross-product, natural join, theta join , rename operators
  • 44. Names and GPAs of students with HS>1000 who applied to CS and were rejected

Notas del editor

  1. For the examples in this lecture we're going to be using a simple University admission relations database with three relations. The first relation, the University relation, contains information about the University name, city, and enrollment of the University. The second relation, the student relation, contains an ID for each student, the student's name, GPA and the size of the high school they attended. And, finally, the third relation contains information about students applying to Universities. Specifically, the student's ID, the University name where they're applying, the major they're applying for and the decision of that application. I've underlined the keys for these three relations. As a reminder, a key is an attribute or a set of attributes whose value is guaranteed to be unique. So, for example, we're going to assume the University names are unique, student IDs are unique and that students will only apply to each University for a particular major one time.
  2. The simplest query in relational algebra is a query that is simply the name of a relation. So, for example, we can write a query, "student" and that's a valid expression in relational algebra. If we run that query on our database we'll get as a result a copy of the student relation. Now what happens next is that we're going to use operators of the relational algebra to filter relations, slice relations, and combine relations.
  3. So, the select operator is used to pick certain rows out of a relation. The select operator is denoted by a small Sigma with a subscript--that's the condition that's used to filter the rows that we extract from the relations.
  4. So, the select operator is used to pick certain rows out of a relation. The select operator is denoted by a small Sigma with a subscript--that's the condition that's used to filter the rows that we extract from the relations.
  5. Our next operator is the Project Operator. So the select operator picks certain rows, and the project operator picks certain columns. So let's say we're interested in the applications, but all we wanted to know was the list of ID's and the decisions for those applications. The project operator is written using the Greek pi symbol, and now the subscript is a list of the column names that we would like to extract.
  6. Now, what if we're interested in picking both rows and columns at the same time. So we want only some of the rows, and we want only some of the columns. Now we're going to compose operators. Remember that relational queries produce relations . So we can write a query, say, with the select operator of the students whose GPA is greater than 3.7. And now, we can take that whole expression which produces a relation, and we can apply the project operator to that, and we can get out the student ID and the student name. And we can compose these as much as we want. We can have select over project, over select, select, project, and so on.
  7. Now let's talk about duplicate values in the results of relational algebra queries. Let's suppose we ask for a list of the majors that people have applied for and the decision for those majors. So we write that as the project of the major and the decision on the applied relation. You might think that when we get the results of this query, we're going to have a lot of duplicate values. You can imagine in a large realistic database of applications, there's going to be hundreds of people applying for majors and having a yes or a no decision. The semantics of relational algebra says that duplicates are always eliminated. So if you run a query that would logically have a lot of duplicate values, you just get one value for each result. That's actually a bit of a difference with the SQL language. So, SQL is based on what's known as multi-sets or bags and that means that we don't eliminate duplicates, whereas relational algebra is based on sets themselves and duplicates are eliminated. There is a multi-set or bad relational algebra defined as well but we'll be fine by just considering the set relational algebra in this course.
  8. Our first operator that combines two relations is the cross-product operator, also known as the Cartesian product. What this operator does, is it takes two relations and kinda glues them together so that their schema of the result is the combination of the schemas of the two relations and the contents of the result are every combination of tuples from those relations. So let's talk about, say, doing the cross products of students and apply. So if we do the cross product we'll get at the result a big relation, here, which is going to have eight attributes. The eight attributes across the student and apply now the only small little trick is that when we glue two relations together sometimes they'll have the same attribute and we can see we have SID on both sides. So just as a notational convention, when cross-product is done and there's two attributes that are named, they're prefaced with the name of the relation they came from. So this one would be referred to in the cross-product as the Student.SID where the other would be referred to as the Apply.SID.. Now let's talk about the contents of these. So let's suppose that the student relation had S-tuples in it and that's how many tuples, while the apply had A tuples in it, the result of the Cartesian products is gonna have S times A tuples, is going to have one tuple for every combination of tuples from the student relation and the apply relation. Now, the cross-product seems like it might not be that helpful, but what is interesting is when we use the cross-product together with other operators.
  9. And let's see a big example of that. Let's suppose that we want to get the names and GPAs of students with a high school size greater than a thousand who applied to CS and were rejected. Okay, so let's take a look. We're going to have to access the students and the apply records in order to run this query. So what we'll do is we'll take student cross apply as our starting point. So now we have a big relation that contains eight attributes and all of those tuples that we described previously. But now we're going to start making things more interesting, because what we're going to do is a big selection over this relation. And that selection is first of all going to make sure that it only combines student and apply tuples that are referring to the same student. So to do that, we write student dot SID equals apply dot SID. So now we've filtered the result of that cross-product to only include combinations of student and apply by couples that make sets. Now we have to do a little bit of additional filtering. We said that we want the high school size to be greater than a thousand, so we do an "and" operator in the high school. We want them to have applied to CS so that's and major equals CS. We're getting a nice big query here. And finally we want them to have been rejected, so "and decision" equals, we'll just be using No for reject. So now, we've got that gigantic query. But that gets us exactly what we want except for one more thing, which is, as I said, all we want is their names and GPAs. So finally we take a big parentheses around here and we apply to that the projection operator, getting the student name and the GPA. And that is the relational algebra expression that produces the query that we have written in English.
  10. So, in conclusion, relational algebra is a formal language. It operates on sets of relations and produces relations as a result. The simplest query is just the name of a relation and then operators are used to filter relations, slice them, and combine them. So far, we've learned the select operator for selecting rows; the project operator for selecting columns; the cross-product operator for combining every possible pair of tuples from two relations; and then two abbreviations, the natural join, which a very useful way to combine relations by enforcing a equality on certain columns; and the theta join operator.