0% found this document useful (0 votes)
15 views5 pages

Relational Algebra Notes

The document provides an overview of relational algebra, detailing its basic operations such as selection, projection, union, intersection, and set difference, along with join operations and the rename operator. It also covers advanced concepts like division, aggregation, and grouping, including examples of queries for each operation. Additionally, it includes practice questions and tips for quiz preparation on the subject.

Uploaded by

parniaghumman99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Relational Algebra Notes

The document provides an overview of relational algebra, detailing its basic operations such as selection, projection, union, intersection, and set difference, along with join operations and the rename operator. It also covers advanced concepts like division, aggregation, and grouping, including examples of queries for each operation. Additionally, it includes practice questions and tips for quiz preparation on the subject.

Uploaded by

parniaghumman99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Relational Algebra Notes & Practice Questions

Lecture 3: Relational Algebra I (Basic Operations)

1. What is Relational Algebra?

 A mathematical query language that forms the foundation of SQL.

 It is procedural (specifies both what to do and how to do it).

 Not implemented directly but used as a foundation for SQL.

2. Basic Operations in Relational Algebra

Selection (σ) - Filters Rows

 Picks specific rows that satisfy a condition.

 Example: Get students with GPA > 3.7


Query: σ GPA>3.7 (Students)

Query: σ major='CS' ∧ uName='Comsats' (Apply)


 Example: Get applications for CS at COMSATS

Projection (π) - Selects Columns

 Picks specific columns, removes duplicates.

 Example: Show only student IDs and majors


Query: π sID, major (Apply)

 Example: Show student ID and name for students with GPA > 3.7
Query: π sID, sName (σ GPA>3.7 (Students))

Union (R ∪ S) - Combines Two Relations

 Combines two tables and removes duplicates.

Query: π city (Branch) ∪ π city (PropertyForRent)


 Example: List all cities with either a branch office or property for rent

Intersection (R ∩ S) - Common Tuples

 Finds common records between two relations.

 Example: List all cities that have both a branch office and property for
rent
Query: π city (Branch) ∩ π city (PropertyForRent)

Set Difference (R - S) - Finds Exclusive Tuples


 Returns tuples in R but not in S.

Cartesian Product (R × S) - Combines All Tuples

 Every row in R combines with every row in S.

 Example: Names and GPAs of students with HS > 1000 who applied to
CS and were rejected
Query:

sql

CopyEdit

π sName, GPA (σ Student.sID = Apply.sID ∧ HS>1000 ∧ major='CS' ∧


dec='No' (Students × Apply))

Lecture 4: Relational Algebra II (Joins & Rename)

1. Join Operations

Joins are used to combine related tuples from two relations.

Natural Join (⋈)

 Combines relations based on common attributes, removes duplicate


columns.

 Example: Names & GPAs of students with HS > 1000 who applied to
CS and were rejected
Query:

sql

CopyEdit

π sName, GPA (σ HS>1000 ∧ major='CS' ∧ dec='No' (Students ⋈ Apply))

Theta Join (⋈θ)

 A general join with a condition.

 Example: Names & GPAs of students applying to CS in Islamabad


Query:

bash

CopyEdit
π GPA (σ city='ISB' ∧ major='CS' (Students ⋈ Apply ⋈ University))

Equi-Join

 A theta join where the condition is only equality (=).

2. Rename Operator (ρ)

 Used when renaming relations or attributes (especially in self-joins).

 Example: Pairs of universities in the same city

scss

CopyEdit

π n1, n2 (σ c1=c2 ∧ n1<n2 (ρ U1(n1,c1,e1) University × ρ U2(n2,c2,e2)


University))

3. Alternate Notations

 Assignment (:=): Saves results to a variable.

 Expression Tree: Graphical way of representing queries.

Lecture 5: Relational Algebra III (Division, Aggregation & Grouping)

1. Division Operator (÷)

 Used to find records that match all items in another set.

 Example: Find students who applied to all universities


Query: Students ÷ University

2. Aggregate Functions (⨁)

 Perform calculations on groups of data.

 Main functions:

o COUNT

o SUM

o AVG

o MIN

o MAX
 Example: Find the total salary and number of employees in each
branch

scss

CopyEdit

ρ R (branchNo, myCount, mySum) (branchNo ⨁ COUNT staffNo, SUM salary


(Staff))

3. Grouping (GA ⨁ AL)

 Groups tuples before applying aggregate functions.

 Example: Find GPA of students applying to CS in Islamabad

bash

CopyEdit

π GPA (σ city='ISB' ∧ major='CS' (Students ⋈ Apply ⋈ University))

Practice Questions & Possible Answers

Q1: List all students who have a GPA greater than 3.5

Query: σ GPA > 3.5 (Students)

Q2: Display only student names and their GPA

Query: π sName, GPA (Students)

Q3: List students who applied to "COMSATS" for CS major

Query: σ major='CS' ∧ uName='Comsats' (Apply)

Q4: Find students who applied to both COMSATS and FAST

Query:

bash

CopyEdit

π sID (σ uName='Comsats' (Apply)) ∩ π sID (σ uName='FAST' (Apply))

Q5: List the number of students in each university

Query: uName ⨁ COUNT sID (Apply)


Q6: List universities where at least one student applied

Query: π uName (Apply)

Final Tips for Quiz Preparation

1. Understand Operators (Selection, Projection, Joins, Aggregation).

2. Memorize Notations (σ, π, ⋈, ∩, ∪, ⨁).

3. Practice Writing Queries – Focus on filtering rows & selecting


columns.

4. Revise Examples from slides and these notes.

5. Attempt Practice Questions to build confidence.

Let me know if you need more practice questions! 🚀

4o

You might also like