Lecture 11 - Relational Algebra Flashcards
1
Q
Query
A
- A query in relational algebra is an expression composed of various operators that extract or manipulate data from one or more relations.
- The result of a query is always a relation: a query (expression) on a set of relations produces a relation as a result
2
Q
Simplest Query
A
The simplest query is a retrieval of all data from a single relation (table) without any filtering or transformation. It’s equivalent to a SELECT *
operation in SQL.
3
Q
Select Operator (σ)
A
- The Select operator (σ) is used to filter rows in a relation based on a given condition.
Example: - (σGPA > 3.7(Student) selects all students with a GPA greater than 3.7.
- SQL Equivalent:
SELECT * FROM Student WHERE GPA > 3.7;
4
Q
Project Operator (π)
A
- The Project operator (
π
) selects specific columns from a relation, effectively reducing the number of attributes. -
Example: (πname, GPA(Student)) projects the
Name
andGPA
columns from theStudent
relation. -
SQL Equivalent:
SELECT Name, GPA FROM Student;
5
Q
Duplicates
A
Relational Algebra inherently operates on sets, which do not allow duplicates. The output of relational algebra operations is a set, meaning it is automatically duplicate-free.
6
Q
Cross Product (x)
A
- The Cross Product (
×
) combines every row of one relation with every row of another. It’s also known as the Cartesian product. - Example: (Student x Course) pairs each student with every course.
-
SQL Equivalent:
SELECT * FROM Student, Course; (without a WHERE clause).
7
Q
Natural Join (⨝)
A
- The Natural Join (
⨝
) combines two relations by matching rows based on common attributes (columns with the same name). -
Example: (Student ⨝ Apply) joins
Student
andApply
where the common attributes (e.g.,Student.ID
andApply.ID
) match. -
SQL Equivalent:
SELECT * FROM Student
NATURAL JOIN Apply;
8
Q
Union Operator (∪)
A
- The Union operator (
∪
) combines the results of two relations, returning all distinct tuples (rows) present in either relation. -
Example: (R ∪ S) returns all unique rows from relations
R
andS
. -
SQL Equivalent:
SELECT * FROM R
UNION
SELECT * FROM S;
9
Q
Difference Operator (-)
A
- The Difference operator (
−
) returns all tuples that are in one relation but not in the other. -
Example: (R - S) returns rows in
R
that are not inS
. -
SQL Equivalent:
SELECT * FROM R
EXCEPT
SELECT * FROM S;
10
Q
Intersection Operator (∩)
A
- The Intersection operator (
∩
) returns only the tuples that are present in both relations. -
Example: (R ∩ S) returns rows common to both
R
andS
. -
SQL Equivalent:
SELECT * FROM R
INTERSECT
SELECT * FROM S;
11
Q
Rename Operator (ρ)
A
- The Rename operator (
ρ
) changes the name of a relation or its attributes. -
Example: (ρs(name)(Student)) renames the
Student
relation toS
and itsName
attribute toStudentName
. -
SQL Equivalent:
SELECT Name AS StudentName FROM Student;