SlideShare a Scribd company logo
CS143 Notes: Relational Algebra



Book Chapters
(4th) Chapters 3.2
(5th) Chapters 2.2-3
(6th) Chapter 2.6


Things to Learn
   • Relational algebra

        – Select, Project, Join, . . .


Steps in Database Construction
  1. Domain Analysis

  2. Database design

  3. Table creation: DDL

  4. Load: bulk-load

  5. Query and update: DML


Database query language
What is a query?
   • Database jargon for question (complex word for simple concept)

   • Questions to get answers from a database

        – Example: Get the students who are taking all CS classes but no Physics class

   • Some queries are easy to pose, some are not

   • Some queries are easy for DBMS to answer, some are not




                                                1
Relational query languages
   • Formal: Relational algebra, relational calculus, datalog

   • Practical: SQL (← relational algebra), Quel (← relational calculus), QBE (← datalog)

   • Relational Query:

       – Data sits in a disk
       – Submit a query
       – Get an answer

                     Input relations −→ query −→ Output relation

          Excecuted against a set of relations and produces a relation
            ∗ Important to know
            ∗ Very useful: “Piping” is possible


Relational Algebra
Input relations (set) −→ query −→ Output relation (set)

   • Set semantics. no duplicate tuples. duplicates are eliminated

   • In contrast, multiset semantics for SQL (performance reason)

Examples to Use
   • School information

       – Student(sid, name, addr, age, GPA)

            sid   name     addr             age       GPA
           301    John     183 Westwood      19        2.1
           303    Elaine   301 Wilshire      17        3.9
           401    James    183 Westwood      17        3.5
           208    Esther   421 Wilshire      20        3.1

       – Class(dept, cnum, sec, unit, title, instructor)

           dept   cnum     sec   unit   title            instructor
           CS     112      01    03     Modeling         Dick Muntz
           CS     143      01    04     DB Systems       Carlo Zaniolo
           EE     143      01    03     Signal           Dick Muntz
           ME     183      02    05     Mechanics        Susan Tracey

       – Enroll(sid, dept, cnum, sec)




                                                  2
sid    dept   cnum    sec
            301    CS     112     01
            301    CS     143     01
            303    EE     143     01
            303    CS     112     01
            401    CS     112     01


Simplest query: relation name
   • Query 1: All students




SELECT operator
Select all tuples satisfying a condition

   • Query 2: Students with age < 18




   • Query 3: Students with GPA > 3.7 and age < 18




   • Notation: σC (R)

        – Filters out rows in a relation
        – C: A boolean expression with attribute names, constants, comparisons (>, ≤, =, . . . )
          and connectives (∧, ∨, ¬)
        – R can be either a relation or a result from another operator

PROJECT operator
   • Query 4: sid and GPA of all students




   • Query 5: All departments offering classes




        – Relational algebra removes duplicates (set semantics)


                                               3
– SQL does not (multiset or bag semantics)

  • Notation: πA (R)

      – Filters out columns in a relation
      – A: a set of attributes to keep

  • Query 6: sid and GPA of all students with age < 18




      – We can “compose” multiple operators

  • Q: Is it ever useful to compose two projection operators next to each other?




  • Q: Is it ever useful to compose two selection operators next to each other?




CROSS PRODUCT (CARTESIAN PRODUCT) operator
  • Example: R × S
                 A        B
            B    a1       b1
     A           a1       b2
     a1   × b1 = a1       b3
            b2
     a2          a2       b1
            b3
                 a2       b2
                 a2       b3

      – Concatenation of tuples from both relations
      – One result tuple for each pair of tuples in R and S
      – If column names conflict, prefix with the table name

  • Notation: R1 × R2

      – R1 × R2 = {t | t = t1 , t2 for t1 ∈ R1 and t2 ∈ R2 }

  • Q: Looks odd to concatenate unrelated tuples. Why use ×?

  • Query 7: Names and addresses of students who take CS courses with GPA < 3




                                               4
• Q: Can we write it differently?




      – Benefit of RDBMS. It figures out the best way to compute.

  • Q: If |R| = r and |S| = s, what is |R × S|?




NATURAL JOIN operator
  • Example: Student       Enroll




      – Shorthand for σStudent.sid=Enroll.sid (Student × Enroll)

  • Notation: R1      R2

      – Concatenate tuples horizontally
      – Enforce equality on common attributes
      – We may assume only one copy of the common attributes are kept

  • Query 8: Names of students taking CS classes




      – Explanation: start with the query requiring sid, not name

  • Query 9: Names of students taking classes offered by “Carlo Zaniolo”




  • Natural join: The most natural way to join two tables

THETA JOIN operator
  • Example: Student       Student.sid=Enroll.sid∧GP A>3.7   Enroll




  • Notation: R1     C   R2 = σC (R1 × R2 )

  • Generalization of natural join

                                                  5
• Often implemented as the basic operation in DBMS

RENAME operator
  • Query 10: Find the pairs of student names who live in the same address.

  • What about πname,name (σaddr=addr (Student × Student))?




  • Notation: ρS (R) – rename R to S

  • Notation: ρS(A1 ,A2 ) (R) for R(A1, A2) – rename R(A1, A2) to S(A1 , A2 )

  • Q: Is πStudent.name,S.name (σStudent.addr=S.addr (Student × ρS (Student))) really correct?

       – How many times (John, James) returned?




UNION operator
  • Query 11: Find all student and instructor names.

       – Q: Can we do it with cross product or join?




  • Notation: R ∪ S

       – Union of tuples from R and S
       – The schemas of R and S should be the same
       – No duplicate tuples in the result

DIFFERENCE operator
  • Query 12: Find the courses (dept, cnum, sec) that no student is taking

       – How can we find the courses that at least one student is taking?




  • Notation: R − S

       – Schemas of R and S must match exactly




                                                 6
• Query 13: What if we want to get the titles of the courses?




       – Very common. To match schemas, we lose information. We have to join back.

INTERSECT operator
  • Query 14: Find the instructors who teach both CS and EE courses

       – Q: Can we answer this using only selection and projection?




  • Notation: R ∩ S = R − (R − S)

       – Draw Venn Diagram to verify




DIVISION operator
Use the boards very carefully keeping all examples.

  • Division operator is not used directly by any one

  • But how we compute the answer for division is very important to learn

  • Learn how we computed the answer, not the operator

  • Query 15: Find the student sids who take every CS class available

       – Q: What will be the answer?




       – Q: How can we compute it?
         Give time to think about




           ∗ Step 1: We need to know which student is taking which class. Where do we get
             the student sid and the courses they take(sid, dept, cnum, sec)?




                                              7
∗ Step 2: We also need to know all CS classes. How do we get the set of all CS
          courses (dept, cnum, sec)?




        ∗ Step 3: What does the relation from Step 1 look like if all students take all CS
          courses?

            How can we compute this? Let us call this relation R




        ∗ Step 4: What does (R - Enroll) look like? What’s its meaning?




        ∗ Step 5: What is the meaning of the projection of Step 4?


        ∗ Step 6: How can we get the student sids who take all CS courses?




• Notation: R/S.

    – Query 16: Find All A values in R such that the values appear with all B values in S.

       R                   S
       A     B             B
       a1    b1            b1
       a1    b2            b2
       a2    b1
       a3    b2
      R ≈ students and classes they take
      S ≈ all CS classes R/S ≈ All students who take all CS classes
    – Formal definition:
        ∗ We assume R(A, B) and S(B)
        ∗ The set of all a ∈ R.A such that a, b ∈ R for every b ∈ S
        ∗ R/S = {a | a ∈ R.A and a, b ∈ R for all b ∈ S}
    – Result for the example:
                                     A
                                     a1
    – Q: How to compute it?



                                            8
Ans: R/S = πA (R) − πA ((πA (R) × S) − R)

         All (R.A, S.B) pairs
           ∗ πA (R): All A’s
           ∗ S: All B’s
           ∗ πA (R) × S: all R.A and S.B pairs
         πA (R) × S − R: A, B pairs that are missing in R
         πA (πA (R) × S − R): All R.A’s that do not have some S.B


      – Analogy with interger division
           ∗ In integer: R/S is the largest integer T such that T × S ≤ R
           ∗ In relational algebra: R/S is the largest relation T such that T × S ⊆ R

  • The division operator is not used often, but how to compute it is important


More questions
  • Q: sids of students who did not take any CS courses?

      – Q: Is πsid (σtitle= CS (Enroll)) correct?




      – Q: What is its complement?




  • General advice: When a query is difficult to write, think in terms of its comple-
    ment.


Relational algebra: things to remember
  • Data manipulation language (query language)

      – Relation → algebra → relation

  • Relational algebra: set semantics, SQL: bag semantics

  • Operators: σ, ×,   , ρ, ∪, −, ∩, /

  • General suggestion: If difficult to write, consider its complement




                                                9

More Related Content

What's hot

Eigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theoremEigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theorem
gidc engineering college
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
uoitc
 
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
njit-ronbrown
 
Eigenvalues and Eigenvectors
Eigenvalues and EigenvectorsEigenvalues and Eigenvectors
Eigenvalues and Eigenvectors
Vinod Srivastava
 
Infinite sequence & series 1st lecture
Infinite sequence & series 1st lecture Infinite sequence & series 1st lecture
Infinite sequence & series 1st lecture
Mohsin Ramay
 
Eigen value and vectors
Eigen value and vectorsEigen value and vectors
Eigen value and vectors
Praveen Prashant
 
Lecture11 spearman rank correlation part-2-with tied ranks
Lecture11 spearman rank correlation part-2-with tied ranksLecture11 spearman rank correlation part-2-with tied ranks
Lecture11 spearman rank correlation part-2-with tied ranks
Dr Rajeev Kumar
 
Power method
Power methodPower method
Power method
nashaat algrara
 
Eigen values and eigen vectors
Eigen values and eigen vectorsEigen values and eigen vectors
Eigen values and eigen vectors
Riddhi Patel
 
Per6 basis2_NUMBER SYSTEMS
Per6 basis2_NUMBER SYSTEMSPer6 basis2_NUMBER SYSTEMS
Per6 basis2_NUMBER SYSTEMS
Evert Sandye Taasiringan
 
Relations
RelationsRelations
Functions and Relations
Functions and RelationsFunctions and Relations
Functions and Relations
sheisirenebkm
 
My slide relational algebra
My slide  relational algebraMy slide  relational algebra
My slide relational algebraRushdi Shams
 
☆中高考综合报告[英]
☆中高考综合报告[英]☆中高考综合报告[英]
☆中高考综合报告[英]
Xu jiakon
 
Eighan values and diagonalization
Eighan values and diagonalization Eighan values and diagonalization
Eighan values and diagonalization
gandhinagar
 
Functions O level
Functions   O levelFunctions   O level
Functions O level
Sapienthall School System
 
Eigen values and eigen vectors engineering
Eigen values and eigen vectors engineeringEigen values and eigen vectors engineering
Eigen values and eigen vectors engineering
shubham211
 
13 sequences and series
13   sequences and series13   sequences and series
13 sequences and seriesKathManarang
 
Constraint satisfaction problems (csp)
Constraint satisfaction problems (csp)   Constraint satisfaction problems (csp)
Constraint satisfaction problems (csp)
Archana432045
 

What's hot (20)

Eigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theoremEigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theorem
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
 
Trc final
Trc finalTrc final
Trc final
 
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5Lecture  11   diagonalization & complex eigenvalues -  5-3 & 5-5
Lecture 11 diagonalization & complex eigenvalues - 5-3 & 5-5
 
Eigenvalues and Eigenvectors
Eigenvalues and EigenvectorsEigenvalues and Eigenvectors
Eigenvalues and Eigenvectors
 
Infinite sequence & series 1st lecture
Infinite sequence & series 1st lecture Infinite sequence & series 1st lecture
Infinite sequence & series 1st lecture
 
Eigen value and vectors
Eigen value and vectorsEigen value and vectors
Eigen value and vectors
 
Lecture11 spearman rank correlation part-2-with tied ranks
Lecture11 spearman rank correlation part-2-with tied ranksLecture11 spearman rank correlation part-2-with tied ranks
Lecture11 spearman rank correlation part-2-with tied ranks
 
Power method
Power methodPower method
Power method
 
Eigen values and eigen vectors
Eigen values and eigen vectorsEigen values and eigen vectors
Eigen values and eigen vectors
 
Per6 basis2_NUMBER SYSTEMS
Per6 basis2_NUMBER SYSTEMSPer6 basis2_NUMBER SYSTEMS
Per6 basis2_NUMBER SYSTEMS
 
Relations
RelationsRelations
Relations
 
Functions and Relations
Functions and RelationsFunctions and Relations
Functions and Relations
 
My slide relational algebra
My slide  relational algebraMy slide  relational algebra
My slide relational algebra
 
☆中高考综合报告[英]
☆中高考综合报告[英]☆中高考综合报告[英]
☆中高考综合报告[英]
 
Eighan values and diagonalization
Eighan values and diagonalization Eighan values and diagonalization
Eighan values and diagonalization
 
Functions O level
Functions   O levelFunctions   O level
Functions O level
 
Eigen values and eigen vectors engineering
Eigen values and eigen vectors engineeringEigen values and eigen vectors engineering
Eigen values and eigen vectors engineering
 
13 sequences and series
13   sequences and series13   sequences and series
13 sequences and series
 
Constraint satisfaction problems (csp)
Constraint satisfaction problems (csp)   Constraint satisfaction problems (csp)
Constraint satisfaction problems (csp)
 

Similar to Algebra

R Algebra.ppt
R Algebra.pptR Algebra.ppt
R Algebra.ppt
Faisal143524
 
presentation on artificial intelligence autosaved
presentation on artificial intelligence autosavedpresentation on artificial intelligence autosaved
presentation on artificial intelligence autosaved
Divya Somashekar
 
relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
dbmscse61
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
abhaysonone0
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
Amin Omi
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
ATS SBGI MIRAJ
 
Active Learning for Multi-relational Data Construction
Active Learning for Multi-relational Data ConstructionActive Learning for Multi-relational Data Construction
Active Learning for Multi-relational Data Construction
Hiroshi Kajino
 
Lecture-3 Relational Algebra I.pptx
Lecture-3 Relational Algebra I.pptxLecture-3 Relational Algebra I.pptx
Lecture-3 Relational Algebra I.pptx
HanzlaNaveed1
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
Vignesh Saravanan
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
SrikanthS494888
 
ConstraintSatisfaction.ppt
ConstraintSatisfaction.pptConstraintSatisfaction.ppt
ConstraintSatisfaction.ppt
MdFazleRabbi53
 
Database managment System Relational Algebra
Database managment System  Relational AlgebraDatabase managment System  Relational Algebra
Database managment System Relational Algebra
Uttara University
 
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdfLecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
ssuserf86fba
 
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
 
Relational Algebra1.ppt
Relational Algebra1.pptRelational Algebra1.ppt
Relational Algebra1.ppt
20DCE031RAJGANATRA
 
2023 St Jospeh's College Geelong
2023 St Jospeh's College Geelong2023 St Jospeh's College Geelong
2023 St Jospeh's College Geelong
Andrew Smith
 
Lecture2-DT.pptx
Lecture2-DT.pptxLecture2-DT.pptx
Lecture2-DT.pptx
INyomanSwitrayana
 
DBMS ArchitectureQuery ExecutorBuffer ManagerStora
DBMS ArchitectureQuery ExecutorBuffer ManagerStoraDBMS ArchitectureQuery ExecutorBuffer ManagerStora
DBMS ArchitectureQuery ExecutorBuffer ManagerStora
LinaCovington707
 

Similar to Algebra (20)

R Algebra.ppt
R Algebra.pptR Algebra.ppt
R Algebra.ppt
 
presentation on artificial intelligence autosaved
presentation on artificial intelligence autosavedpresentation on artificial intelligence autosaved
presentation on artificial intelligence autosaved
 
relational algebra and it's implementation
relational algebra and it's implementationrelational algebra and it's implementation
relational algebra and it's implementation
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
 
Compilation
CompilationCompilation
Compilation
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 
Active Learning for Multi-relational Data Construction
Active Learning for Multi-relational Data ConstructionActive Learning for Multi-relational Data Construction
Active Learning for Multi-relational Data Construction
 
Lecture-3 Relational Algebra I.pptx
Lecture-3 Relational Algebra I.pptxLecture-3 Relational Algebra I.pptx
Lecture-3 Relational Algebra I.pptx
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
3_Relational_Model.pdf
3_Relational_Model.pdf3_Relational_Model.pdf
3_Relational_Model.pdf
 
ConstraintSatisfaction.ppt
ConstraintSatisfaction.pptConstraintSatisfaction.ppt
ConstraintSatisfaction.ppt
 
Database managment System Relational Algebra
Database managment System  Relational AlgebraDatabase managment System  Relational Algebra
Database managment System Relational Algebra
 
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdfLecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
 
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
 
Relational Algebra1.ppt
Relational Algebra1.pptRelational Algebra1.ppt
Relational Algebra1.ppt
 
2023 St Jospeh's College Geelong
2023 St Jospeh's College Geelong2023 St Jospeh's College Geelong
2023 St Jospeh's College Geelong
 
Relational+algebra (1)
Relational+algebra (1)Relational+algebra (1)
Relational+algebra (1)
 
Lecture2-DT.pptx
Lecture2-DT.pptxLecture2-DT.pptx
Lecture2-DT.pptx
 
DBMS ArchitectureQuery ExecutorBuffer ManagerStora
DBMS ArchitectureQuery ExecutorBuffer ManagerStoraDBMS ArchitectureQuery ExecutorBuffer ManagerStora
DBMS ArchitectureQuery ExecutorBuffer ManagerStora
 

Recently uploaded

The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 

Recently uploaded (20)

The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 

Algebra

  • 1. CS143 Notes: Relational Algebra Book Chapters (4th) Chapters 3.2 (5th) Chapters 2.2-3 (6th) Chapter 2.6 Things to Learn • Relational algebra – Select, Project, Join, . . . Steps in Database Construction 1. Domain Analysis 2. Database design 3. Table creation: DDL 4. Load: bulk-load 5. Query and update: DML Database query language What is a query? • Database jargon for question (complex word for simple concept) • Questions to get answers from a database – Example: Get the students who are taking all CS classes but no Physics class • Some queries are easy to pose, some are not • Some queries are easy for DBMS to answer, some are not 1
  • 2. Relational query languages • Formal: Relational algebra, relational calculus, datalog • Practical: SQL (← relational algebra), Quel (← relational calculus), QBE (← datalog) • Relational Query: – Data sits in a disk – Submit a query – Get an answer Input relations −→ query −→ Output relation Excecuted against a set of relations and produces a relation ∗ Important to know ∗ Very useful: “Piping” is possible Relational Algebra Input relations (set) −→ query −→ Output relation (set) • Set semantics. no duplicate tuples. duplicates are eliminated • In contrast, multiset semantics for SQL (performance reason) Examples to Use • School information – Student(sid, name, addr, age, GPA) sid name addr age GPA 301 John 183 Westwood 19 2.1 303 Elaine 301 Wilshire 17 3.9 401 James 183 Westwood 17 3.5 208 Esther 421 Wilshire 20 3.1 – Class(dept, cnum, sec, unit, title, instructor) dept cnum sec unit title instructor CS 112 01 03 Modeling Dick Muntz CS 143 01 04 DB Systems Carlo Zaniolo EE 143 01 03 Signal Dick Muntz ME 183 02 05 Mechanics Susan Tracey – Enroll(sid, dept, cnum, sec) 2
  • 3. sid dept cnum sec 301 CS 112 01 301 CS 143 01 303 EE 143 01 303 CS 112 01 401 CS 112 01 Simplest query: relation name • Query 1: All students SELECT operator Select all tuples satisfying a condition • Query 2: Students with age < 18 • Query 3: Students with GPA > 3.7 and age < 18 • Notation: σC (R) – Filters out rows in a relation – C: A boolean expression with attribute names, constants, comparisons (>, ≤, =, . . . ) and connectives (∧, ∨, ¬) – R can be either a relation or a result from another operator PROJECT operator • Query 4: sid and GPA of all students • Query 5: All departments offering classes – Relational algebra removes duplicates (set semantics) 3
  • 4. – SQL does not (multiset or bag semantics) • Notation: πA (R) – Filters out columns in a relation – A: a set of attributes to keep • Query 6: sid and GPA of all students with age < 18 – We can “compose” multiple operators • Q: Is it ever useful to compose two projection operators next to each other? • Q: Is it ever useful to compose two selection operators next to each other? CROSS PRODUCT (CARTESIAN PRODUCT) operator • Example: R × S A B B a1 b1 A a1 b2 a1 × b1 = a1 b3 b2 a2 a2 b1 b3 a2 b2 a2 b3 – Concatenation of tuples from both relations – One result tuple for each pair of tuples in R and S – If column names conflict, prefix with the table name • Notation: R1 × R2 – R1 × R2 = {t | t = t1 , t2 for t1 ∈ R1 and t2 ∈ R2 } • Q: Looks odd to concatenate unrelated tuples. Why use ×? • Query 7: Names and addresses of students who take CS courses with GPA < 3 4
  • 5. • Q: Can we write it differently? – Benefit of RDBMS. It figures out the best way to compute. • Q: If |R| = r and |S| = s, what is |R × S|? NATURAL JOIN operator • Example: Student Enroll – Shorthand for σStudent.sid=Enroll.sid (Student × Enroll) • Notation: R1 R2 – Concatenate tuples horizontally – Enforce equality on common attributes – We may assume only one copy of the common attributes are kept • Query 8: Names of students taking CS classes – Explanation: start with the query requiring sid, not name • Query 9: Names of students taking classes offered by “Carlo Zaniolo” • Natural join: The most natural way to join two tables THETA JOIN operator • Example: Student Student.sid=Enroll.sid∧GP A>3.7 Enroll • Notation: R1 C R2 = σC (R1 × R2 ) • Generalization of natural join 5
  • 6. • Often implemented as the basic operation in DBMS RENAME operator • Query 10: Find the pairs of student names who live in the same address. • What about πname,name (σaddr=addr (Student × Student))? • Notation: ρS (R) – rename R to S • Notation: ρS(A1 ,A2 ) (R) for R(A1, A2) – rename R(A1, A2) to S(A1 , A2 ) • Q: Is πStudent.name,S.name (σStudent.addr=S.addr (Student × ρS (Student))) really correct? – How many times (John, James) returned? UNION operator • Query 11: Find all student and instructor names. – Q: Can we do it with cross product or join? • Notation: R ∪ S – Union of tuples from R and S – The schemas of R and S should be the same – No duplicate tuples in the result DIFFERENCE operator • Query 12: Find the courses (dept, cnum, sec) that no student is taking – How can we find the courses that at least one student is taking? • Notation: R − S – Schemas of R and S must match exactly 6
  • 7. • Query 13: What if we want to get the titles of the courses? – Very common. To match schemas, we lose information. We have to join back. INTERSECT operator • Query 14: Find the instructors who teach both CS and EE courses – Q: Can we answer this using only selection and projection? • Notation: R ∩ S = R − (R − S) – Draw Venn Diagram to verify DIVISION operator Use the boards very carefully keeping all examples. • Division operator is not used directly by any one • But how we compute the answer for division is very important to learn • Learn how we computed the answer, not the operator • Query 15: Find the student sids who take every CS class available – Q: What will be the answer? – Q: How can we compute it? Give time to think about ∗ Step 1: We need to know which student is taking which class. Where do we get the student sid and the courses they take(sid, dept, cnum, sec)? 7
  • 8. ∗ Step 2: We also need to know all CS classes. How do we get the set of all CS courses (dept, cnum, sec)? ∗ Step 3: What does the relation from Step 1 look like if all students take all CS courses? How can we compute this? Let us call this relation R ∗ Step 4: What does (R - Enroll) look like? What’s its meaning? ∗ Step 5: What is the meaning of the projection of Step 4? ∗ Step 6: How can we get the student sids who take all CS courses? • Notation: R/S. – Query 16: Find All A values in R such that the values appear with all B values in S. R S A B B a1 b1 b1 a1 b2 b2 a2 b1 a3 b2 R ≈ students and classes they take S ≈ all CS classes R/S ≈ All students who take all CS classes – Formal definition: ∗ We assume R(A, B) and S(B) ∗ The set of all a ∈ R.A such that a, b ∈ R for every b ∈ S ∗ R/S = {a | a ∈ R.A and a, b ∈ R for all b ∈ S} – Result for the example: A a1 – Q: How to compute it? 8
  • 9. Ans: R/S = πA (R) − πA ((πA (R) × S) − R) All (R.A, S.B) pairs ∗ πA (R): All A’s ∗ S: All B’s ∗ πA (R) × S: all R.A and S.B pairs πA (R) × S − R: A, B pairs that are missing in R πA (πA (R) × S − R): All R.A’s that do not have some S.B – Analogy with interger division ∗ In integer: R/S is the largest integer T such that T × S ≤ R ∗ In relational algebra: R/S is the largest relation T such that T × S ⊆ R • The division operator is not used often, but how to compute it is important More questions • Q: sids of students who did not take any CS courses? – Q: Is πsid (σtitle= CS (Enroll)) correct? – Q: What is its complement? • General advice: When a query is difficult to write, think in terms of its comple- ment. Relational algebra: things to remember • Data manipulation language (query language) – Relation → algebra → relation • Relational algebra: set semantics, SQL: bag semantics • Operators: σ, ×, , ρ, ∪, −, ∩, / • General suggestion: If difficult to write, consider its complement 9