SlideShare a Scribd company logo
1 of 20
Introduction to SQL SetIntroduction to SQL Set
OperationsOperations
Database QueryDatabase Query
LanguagesLanguages
• Use “Campus” schema
• Given a database, ask questions, get data as answers
o Ex: Get all students with GPA > 3.7 who applied to Berkeley and
Stanford and nowhere else
o Ex: Get all humanities departments at campuses in Florida with < 1000
applicants
o Ex: Get the campus with highest average accept rate over the last
five years
• Some questions are easy to pose, some are not
• Some questions are easy for DBMS to answer, some are not.
• "Query language" also used to update the database
Lecture 15© CIS 4301 - Spring 2006 3
Relational QueryRelational Query
LanguagesLanguages
• Formal: relational algebra, relational calculus,
Datalog
• Actual: SQL, Quel, Query-by-Example (QBE)
• In ALL languages, a query is executed over a set of
relations, get single relation as the result
Lecture 15© CIS 4301 - Spring 2006 4
Relational AlgebraRelational Algebra
• Notation for describing queries in the relational
model
• Relational model has concrete set of “standard”
operations
• Operations are not “Turing Complete”
o Not a defect, helps with query processing and
optimization
o FYI, a language is Turing Complete if it is powerful enough
to implement any Turing machine. It's widely believed that
Turing machines can do any calculation that can be
performed by a modern computer program
• Start by introducing operations of relational algebra,
SQL next
• Algebra applies to sets of tuples, i.e., relations
o Commercial DBMS use different notation of relations
which are multisets
Lecture 15© CIS 4301 - Spring 2006 5
Relational AlgebraRelational Algebra
• Construct new relations from old ones
o Set of operators
o Relations are operands
• Build progressively more complex expressions
by applying operators to relations or to rela.
algebra expressions (which are relations as
well)
• Query is an expression of relational algebra
o First concrete example of a query language
• Four broad classes of operations
o Set operations, selection operations, operations
that combine data from two relations, rename
operation
Lecture 15© CIS 4301 - Spring 2006 6
Sample Relational SchemaSample Relational Schema
Movie (Title,Year,length,filmType,
studioName,producerC#)
StarsIn (MovieTitle,MovieYear,StarName)
MovieStar(Name,address,gender,birthdate)
MovieExec(name,address,Cert#,netWorth)
Studio (Name,address,presC#)
Lecture 15© CIS 4301 - Spring 2006 7
BasicsBasics
Operations of traditional relational algebra fall into
four broad classes:
1.Set operations
2.Operations that remove parts of a relation
3.Operations that combine tuples of two relations
4.Renaming
Lecture 15© CIS 4301 - Spring 2006 8
Set OperationsSet Operations
Union (binary, commutative, associative)
• R ∪ S
Intersection (binary, commutative, associative)
• R ∩ S
Set Difference (binary)
• R - S
o Set of elements in R but not in S
o R-S ≠ S-R !!
• R(A1,A2,…,An), S(B1,B2,…,Bn) must be union compatible
o R and S are of the same degree
o for each i, dom(Ai) = dom(Bi)
o Columns of R and S must be ordered so that order of attributes is
same for both relations
Lecture 15© CIS 4301 - Spring 2006 9
ExampleExample
Lecture 15© CIS 4301 - Spring 2006 10
name address gender birthdate
Carrie Fisher
Mark Hamil
123 Maple St., Hollywood
456 Oak Rd., Brentwood
F
M
9/9/99
8/8/88
R
name address gender birthdate
Carrie Fisher
Harrison Ford
123 Maple St., Hollywood
789 Palm Dr., Beverly Hills
F
M
9/9/99
7/7/77
S
ProjectProject
• PROJECT can produce many tuples with same
value
o Relational algebra semantics says remove duplicates
o SQL does not -- one difference between formal and actual query
languages
Lecture 15© CIS 4301 - Spring 2006 11
Relational Operator:Relational Operator:
SelectSelect
Select or Restrict (unary, commutative)
• σ<predicate> (R)
• <predicate> is a conditional expression of the type that
we are familiar with from conventional programming
languages
o <attribute> <op> <attribute>
o <attribute> <op> <constant>
o attribute in R
o op ∈ {=,≠,<,>,≤, …, AND, OR}
• Ex: σlength≥100 (Movie) vertical restriction”
Lecture 15© CIS 4301 - Spring 2006 12
PictoriallyPictorially
Lecture 15© CIS 4301 - Spring 2006 13
A1 A2 A3 … An
...
i
A1 A2 A3 … An
...
j, i ≥ j
σ
title year length filmType
Star Wars
Mighty
Ducks
Wayne’s
World
1977
1991
1992
124
104
95
color
color
color
Movie
result set
Cartesian ProductCartesian Product
Cartesian Product (binary, commutative, associative)
• R x S
• Sets of all pairs that can be formed by choosing the first
element of the pair to be any element of R, the second any
element of S
• Relation schema is union of schemas for R and S
• Resulting schema may be ambiguous
o Use R.A or S.A to disambiguate an attribute that occurs in
both schemas
Lecture 15© CIS 4301 - Spring 2006 14
ExampleExample
Lecture 15© CIS 4301 - Spring 2006 15
A B
1 2
3 4
B C
2 5
4 7
D
6
8
9 10 11
x
A R.BS.B C D
R S
1 2 2 5 6
1 2 4 7 8
1 2 9 10 11
3 4
3 4
3 4
2 5 6
4 7 8
9 10 11
Join OperationsJoin Operations
Natural Join (binary)
• R join S
• Match only those tuples from R and S that agree in
whatever attributes are common to the schemas of R and
S
o If r and s from r(R) and s(S) are successfully paired,
result is called a joined tuple
• This join operation is the same we used in earlier section to
recombine relations that had been projected onto two
subsets of their attributes (e.g., as a result of a BCNF
decomposition)
Lecture 15© CIS 4301 - Spring 2006 16
ExampleExample
• Resulting schema has attributes from R, either R or S (i.e.,
joining attribute(s)), and
• Tuples that fail to pair with any tuple of the other relation
are called dangling tuples
Lecture 15© CIS 4301 - Spring 2006 17
A B
1 2
3 4
B C
2 5
4 7
D
6
8
9 10 11
join
A B C D
R S
1 2 5 6
3 4 7 8
Join OperationsJoin Operations
Theta Join (binary)
• R joinC S, where C is an arbitrary join condition
• Step 1: take the product of R and S
• Step 2: Select from the product only those tuples
that satisfy condition C
• As with the product operation, the schema for
the result is the union of the schemas of R and S
Lecture 15© CIS 4301 - Spring 2006 18
ExampleExample
Lecture 15© CIS 4301 - Spring 2006 19
B C
2 3
2 3
D
4
5
7 8 10
joinA<D AND U.B≠V.B
A B
1 2
6 7
C
3
8
9 7 8
U V
V.BV.C DA U.BU.C
1 2 3 7 8 10
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/sql-classes-in-
mumbai.html

More Related Content

Viewers also liked

Information Technology and Its Applications - nursing - social networks - 3
Information Technology and Its Applications - nursing - social networks - 3Information Technology and Its Applications - nursing - social networks - 3
Information Technology and Its Applications - nursing - social networks - 3
Sascha Funk
 
Fundamental elements of computer
Fundamental elements of computerFundamental elements of computer
Fundamental elements of computer
Jesus Obenita Jr.
 
The elements of a computer system
The elements of a computer systemThe elements of a computer system
The elements of a computer system
Mary Zharina
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMS
koolkampus
 
Applications of computer
Applications of computerApplications of computer
Applications of computer
hrushitpatel39
 

Viewers also liked (20)

On set operations
On set operationsOn set operations
On set operations
 
Information Technology and Its Applications - nursing - social networks - 3
Information Technology and Its Applications - nursing - social networks - 3Information Technology and Its Applications - nursing - social networks - 3
Information Technology and Its Applications - nursing - social networks - 3
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter15
Chapter15Chapter15
Chapter15
 
Information technology & its applications - 1
Information technology & its applications - 1Information technology & its applications - 1
Information technology & its applications - 1
 
Transaction Management - Lecture 11 - Introduction to Databases (1007156ANR)
Transaction Management - Lecture 11 - Introduction to Databases (1007156ANR)Transaction Management - Lecture 11 - Introduction to Databases (1007156ANR)
Transaction Management - Lecture 11 - Introduction to Databases (1007156ANR)
 
Fundamental elements of computer
Fundamental elements of computerFundamental elements of computer
Fundamental elements of computer
 
Input computer hardware notes for UCEand UACE ICT
Input computer hardware notes for UCEand UACE ICTInput computer hardware notes for UCEand UACE ICT
Input computer hardware notes for UCEand UACE ICT
 
The elements of a computer system
The elements of a computer systemThe elements of a computer system
The elements of a computer system
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
introduction to database
 introduction to database introduction to database
introduction to database
 
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
Relational Model and Relational Algebra - Lecture 3 - Introduction to Databas...
 
Computer Software & It's types.
Computer Software &  It's types.Computer Software &  It's types.
Computer Software & It's types.
 
Database Systems - Introduction (Chapter 1)
Database Systems - Introduction (Chapter 1)Database Systems - Introduction (Chapter 1)
Database Systems - Introduction (Chapter 1)
 
Chapter 01 - Introduction to Computers
Chapter 01 - Introduction to ComputersChapter 01 - Introduction to Computers
Chapter 01 - Introduction to Computers
 
Data Mining and Data Warehousing
Data Mining and Data WarehousingData Mining and Data Warehousing
Data Mining and Data Warehousing
 
13. Query Processing in DBMS
13. Query Processing in DBMS13. Query Processing in DBMS
13. Query Processing in DBMS
 
Applications of computer
Applications of computerApplications of computer
Applications of computer
 
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
 
Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 

Similar to SQL- Introduction to SQL Set Operations

Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancements
Subash John
 
Structured query language
Structured query languageStructured query language
Structured query language
Neeti Gupta
 
Structured query language
Structured query languageStructured query language
Structured query language
Neeti Gupta
 

Similar to SQL- Introduction to SQL Set Operations (20)

Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Database part2-
Database part2-Database part2-
Database part2-
 
Info_Management_report-1.pptx
Info_Management_report-1.pptxInfo_Management_report-1.pptx
Info_Management_report-1.pptx
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Natural Language to SQL Query conversion using Machine Learning Techniques on...
Natural Language to SQL Query conversion using Machine Learning Techniques on...Natural Language to SQL Query conversion using Machine Learning Techniques on...
Natural Language to SQL Query conversion using Machine Learning Techniques on...
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Algebra relacional
Algebra relacionalAlgebra relacional
Algebra relacional
 
2 rel-algebra
2 rel-algebra2 rel-algebra
2 rel-algebra
 
Adbms 40 heuristics in query optimization
Adbms 40 heuristics in query optimizationAdbms 40 heuristics in query optimization
Adbms 40 heuristics in query optimization
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancements
 
UNIT 2 Structured query language commands
UNIT 2 Structured query language commandsUNIT 2 Structured query language commands
UNIT 2 Structured query language commands
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Unit 04 dbms
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
 
Structured query language
Structured query languageStructured query language
Structured query language
 
Structured query language
Structured query languageStructured query language
Structured query language
 
PGQL: A Language for Graphs
PGQL: A Language for GraphsPGQL: A Language for Graphs
PGQL: A Language for Graphs
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 

More from Vibrant Technologies & Computers

More from Vibrant Technologies & Computers (20)

Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
SQL- Introduction to SQL database
SQL- Introduction to SQL database SQL- Introduction to SQL database
SQL- Introduction to SQL database
 
ITIL - introduction to ITIL
ITIL - introduction to ITILITIL - introduction to ITIL
ITIL - introduction to ITIL
 
Salesforce - Introduction to Security & Access
Salesforce -  Introduction to Security & Access Salesforce -  Introduction to Security & Access
Salesforce - Introduction to Security & Access
 
Data ware housing- Introduction to olap .
Data ware housing- Introduction to  olap .Data ware housing- Introduction to  olap .
Data ware housing- Introduction to olap .
 
Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.
 
Data ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housingData ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housing
 
Salesforce - classification of cloud computing
Salesforce - classification of cloud computingSalesforce - classification of cloud computing
Salesforce - classification of cloud computing
 
Salesforce - cloud computing fundamental
Salesforce - cloud computing fundamentalSalesforce - cloud computing fundamental
Salesforce - cloud computing fundamental
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
SQL- Introduction to advanced sql concepts
SQL- Introduction to  advanced sql conceptsSQL- Introduction to  advanced sql concepts
SQL- Introduction to advanced sql concepts
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
Sas - Introduction to designing the data mart
Sas - Introduction to designing the data martSas - Introduction to designing the data mart
Sas - Introduction to designing the data mart
 
Sas - Introduction to working under change management
Sas - Introduction to working under change managementSas - Introduction to working under change management
Sas - Introduction to working under change management
 
SAS - overview of SAS
SAS - overview of SASSAS - overview of SAS
SAS - overview of SAS
 
Teradata - Architecture of Teradata
Teradata - Architecture of TeradataTeradata - Architecture of Teradata
Teradata - Architecture of Teradata
 
Teradata - Restoring Data
Teradata - Restoring Data Teradata - Restoring Data
Teradata - Restoring Data
 
Datastage database design and data modeling ppt 4
Datastage database design and data modeling ppt 4Datastage database design and data modeling ppt 4
Datastage database design and data modeling ppt 4
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 

SQL- Introduction to SQL Set Operations

  • 1.
  • 2. Introduction to SQL SetIntroduction to SQL Set OperationsOperations
  • 3. Database QueryDatabase Query LanguagesLanguages • Use “Campus” schema • Given a database, ask questions, get data as answers o Ex: Get all students with GPA > 3.7 who applied to Berkeley and Stanford and nowhere else o Ex: Get all humanities departments at campuses in Florida with < 1000 applicants o Ex: Get the campus with highest average accept rate over the last five years • Some questions are easy to pose, some are not • Some questions are easy for DBMS to answer, some are not. • "Query language" also used to update the database Lecture 15© CIS 4301 - Spring 2006 3
  • 4. Relational QueryRelational Query LanguagesLanguages • Formal: relational algebra, relational calculus, Datalog • Actual: SQL, Quel, Query-by-Example (QBE) • In ALL languages, a query is executed over a set of relations, get single relation as the result Lecture 15© CIS 4301 - Spring 2006 4
  • 5. Relational AlgebraRelational Algebra • Notation for describing queries in the relational model • Relational model has concrete set of “standard” operations • Operations are not “Turing Complete” o Not a defect, helps with query processing and optimization o FYI, a language is Turing Complete if it is powerful enough to implement any Turing machine. It's widely believed that Turing machines can do any calculation that can be performed by a modern computer program • Start by introducing operations of relational algebra, SQL next • Algebra applies to sets of tuples, i.e., relations o Commercial DBMS use different notation of relations which are multisets Lecture 15© CIS 4301 - Spring 2006 5
  • 6. Relational AlgebraRelational Algebra • Construct new relations from old ones o Set of operators o Relations are operands • Build progressively more complex expressions by applying operators to relations or to rela. algebra expressions (which are relations as well) • Query is an expression of relational algebra o First concrete example of a query language • Four broad classes of operations o Set operations, selection operations, operations that combine data from two relations, rename operation Lecture 15© CIS 4301 - Spring 2006 6
  • 7. Sample Relational SchemaSample Relational Schema Movie (Title,Year,length,filmType, studioName,producerC#) StarsIn (MovieTitle,MovieYear,StarName) MovieStar(Name,address,gender,birthdate) MovieExec(name,address,Cert#,netWorth) Studio (Name,address,presC#) Lecture 15© CIS 4301 - Spring 2006 7
  • 8. BasicsBasics Operations of traditional relational algebra fall into four broad classes: 1.Set operations 2.Operations that remove parts of a relation 3.Operations that combine tuples of two relations 4.Renaming Lecture 15© CIS 4301 - Spring 2006 8
  • 9. Set OperationsSet Operations Union (binary, commutative, associative) • R ∪ S Intersection (binary, commutative, associative) • R ∩ S Set Difference (binary) • R - S o Set of elements in R but not in S o R-S ≠ S-R !! • R(A1,A2,…,An), S(B1,B2,…,Bn) must be union compatible o R and S are of the same degree o for each i, dom(Ai) = dom(Bi) o Columns of R and S must be ordered so that order of attributes is same for both relations Lecture 15© CIS 4301 - Spring 2006 9
  • 10. ExampleExample Lecture 15© CIS 4301 - Spring 2006 10 name address gender birthdate Carrie Fisher Mark Hamil 123 Maple St., Hollywood 456 Oak Rd., Brentwood F M 9/9/99 8/8/88 R name address gender birthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 S
  • 11. ProjectProject • PROJECT can produce many tuples with same value o Relational algebra semantics says remove duplicates o SQL does not -- one difference between formal and actual query languages Lecture 15© CIS 4301 - Spring 2006 11
  • 12. Relational Operator:Relational Operator: SelectSelect Select or Restrict (unary, commutative) • σ<predicate> (R) • <predicate> is a conditional expression of the type that we are familiar with from conventional programming languages o <attribute> <op> <attribute> o <attribute> <op> <constant> o attribute in R o op ∈ {=,≠,<,>,≤, …, AND, OR} • Ex: σlength≥100 (Movie) vertical restriction” Lecture 15© CIS 4301 - Spring 2006 12
  • 13. PictoriallyPictorially Lecture 15© CIS 4301 - Spring 2006 13 A1 A2 A3 … An ... i A1 A2 A3 … An ... j, i ≥ j σ title year length filmType Star Wars Mighty Ducks Wayne’s World 1977 1991 1992 124 104 95 color color color Movie result set
  • 14. Cartesian ProductCartesian Product Cartesian Product (binary, commutative, associative) • R x S • Sets of all pairs that can be formed by choosing the first element of the pair to be any element of R, the second any element of S • Relation schema is union of schemas for R and S • Resulting schema may be ambiguous o Use R.A or S.A to disambiguate an attribute that occurs in both schemas Lecture 15© CIS 4301 - Spring 2006 14
  • 15. ExampleExample Lecture 15© CIS 4301 - Spring 2006 15 A B 1 2 3 4 B C 2 5 4 7 D 6 8 9 10 11 x A R.BS.B C D R S 1 2 2 5 6 1 2 4 7 8 1 2 9 10 11 3 4 3 4 3 4 2 5 6 4 7 8 9 10 11
  • 16. Join OperationsJoin Operations Natural Join (binary) • R join S • Match only those tuples from R and S that agree in whatever attributes are common to the schemas of R and S o If r and s from r(R) and s(S) are successfully paired, result is called a joined tuple • This join operation is the same we used in earlier section to recombine relations that had been projected onto two subsets of their attributes (e.g., as a result of a BCNF decomposition) Lecture 15© CIS 4301 - Spring 2006 16
  • 17. ExampleExample • Resulting schema has attributes from R, either R or S (i.e., joining attribute(s)), and • Tuples that fail to pair with any tuple of the other relation are called dangling tuples Lecture 15© CIS 4301 - Spring 2006 17 A B 1 2 3 4 B C 2 5 4 7 D 6 8 9 10 11 join A B C D R S 1 2 5 6 3 4 7 8
  • 18. Join OperationsJoin Operations Theta Join (binary) • R joinC S, where C is an arbitrary join condition • Step 1: take the product of R and S • Step 2: Select from the product only those tuples that satisfy condition C • As with the product operation, the schema for the result is the union of the schemas of R and S Lecture 15© CIS 4301 - Spring 2006 18
  • 19. ExampleExample Lecture 15© CIS 4301 - Spring 2006 19 B C 2 3 2 3 D 4 5 7 8 10 joinA<D AND U.B≠V.B A B 1 2 6 7 C 3 8 9 7 8 U V V.BV.C DA U.BU.C 1 2 3 7 8 10
  • 20. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/sql-classes-in- mumbai.html