SlideShare a Scribd company logo
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

More Related Content

What's hot

Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
Vignesh Saravanan
 
Relational model
Relational modelRelational model
Relational model
Dabbal Singh Mahara
 
DBMS & RDBMS (PPT)
DBMS & RDBMS (PPT)DBMS & RDBMS (PPT)
DBMS & RDBMS (PPT)
Krushang Thakor
 
Functional dependency and normalization
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalization
University of Potsdam
 
SQL UNION
SQL UNIONSQL UNION
SQL UNION
Ritwik Das
 
Relational algebra operations
Relational algebra operationsRelational algebra operations
Relational algebra operations
SanthiNivas
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
Abhinav Tyagi
 
embedded-static-&dynamic
embedded-static-&dynamicembedded-static-&dynamic
embedded-static-&dynamic
Saranya Natarajan
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
Salman Vadsarya
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Salah Amean
 
Query processing and Query Optimization
Query processing and Query OptimizationQuery processing and Query Optimization
Query processing and Query Optimization
Niraj Gandha
 
Advance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In DatabaseAdvance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In Database
Sonali Parab
 
Apache hive
Apache hiveApache hive
Apache hive
pradipbajpai68
 
Data Modeling PPT
Data Modeling PPTData Modeling PPT
Data Modeling PPT
Trinath
 
Data flow diagrams - DFD
Data flow diagrams - DFDData flow diagrams - DFD
Data flow diagrams - DFD
mbedlabs Technosolutions
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relationalJafar Nesargi
 

What's hot (20)

Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Dbms slides
Dbms slidesDbms slides
Dbms slides
 
Relational model
Relational modelRelational model
Relational model
 
DBMS & RDBMS (PPT)
DBMS & RDBMS (PPT)DBMS & RDBMS (PPT)
DBMS & RDBMS (PPT)
 
Functional dependency and normalization
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalization
 
SQL UNION
SQL UNIONSQL UNION
SQL UNION
 
Relational algebra operations
Relational algebra operationsRelational algebra operations
Relational algebra operations
 
Data warehousing
Data warehousingData warehousing
Data warehousing
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
 
embedded-static-&dynamic
embedded-static-&dynamicembedded-static-&dynamic
embedded-static-&dynamic
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
 
Query processing and Query Optimization
Query processing and Query OptimizationQuery processing and Query Optimization
Query processing and Query Optimization
 
Advance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In DatabaseAdvance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In Database
 
Apache hive
Apache hiveApache hive
Apache hive
 
Data Modeling PPT
Data Modeling PPTData Modeling PPT
Data Modeling PPT
 
Data flow diagrams - DFD
Data flow diagrams - DFDData flow diagrams - DFD
Data flow diagrams - DFD
 
Chapter 6 relational data model and relational
Chapter  6  relational data model and relationalChapter  6  relational data model and relational
Chapter 6 relational data model and relational
 

Similar to Lecture-3 Relational Algebra I.pptx

R Algebra.ppt
R Algebra.pptR Algebra.ppt
R Algebra.ppt
Faisal143524
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
ATS SBGI MIRAJ
 
Ra Revision
Ra RevisionRa Revision
Ra Revision
hithammohamed
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
Pyingkodi Maran
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
SrikanthS494888
 
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 PATEL
Shashi Patel
 
1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf
Kavinilaa
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
abhaysonone0
 
relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
dbmscse61
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.ppt
Sreenivas R
 
3 relational model
3 relational model3 relational model
3 relational model
Utkarsh De
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbmsshekhar1991
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
Radhika Talaviya
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
Amin Omi
 

Similar to 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 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 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 and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
 
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
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 

More from 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.pptx
HanzlaNaveed1
 
Lecture 23 DHCP and NAT.pptx
Lecture 23 DHCP and NAT.pptxLecture 23 DHCP and NAT.pptx
Lecture 23 DHCP and NAT.pptx
HanzlaNaveed1
 
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
HanzlaNaveed1
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptx
HanzlaNaveed1
 
Lecture 06 and 07.pptx
Lecture 06 and 07.pptxLecture 06 and 07.pptx
Lecture 06 and 07.pptx
HanzlaNaveed1
 
Lecture 26 Link Layer .pptx
Lecture 26 Link Layer .pptxLecture 26 Link Layer .pptx
Lecture 26 Link Layer .pptx
HanzlaNaveed1
 
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
HanzlaNaveed1
 
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
HanzlaNaveed1
 

More from 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
 

Recently uploaded

Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 

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

Editor's Notes

  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.