3.9 Relational Algebra Flashcards
What is the equivalent of a relational algebra expression?
An SQL query defining a single result table.
- What operation does the symbol
σ
represent in relational algebra? - What is the operation written as?
Select
Greek Letter: sigma
- What operation does the symbol
π
represent in relational algebra? - What is the operation written as?
Project
Greek Letter: Pi
- What operation does the symbol
×
represent in relational algebra? - What is the operation written as?
Product
- What operation does the symbol
⨝
represent in relational algebra? - What is the operation written as?
Join
- What operation does the symbol
∪
represent in relational algebra? - What is the operation written as?
Union
- What operation does the symbol
∩
represent in relational algebra? - What is the operation written as?
Intersect
- What operation does the symbol
−
represent in relational algebra? - What is the operation written as?
Difference
- What operation does the symbol
ρ
represent in relational algebra? - What is the operation written as?
Rename
Greek Letter: rho
- What operation does the symbol
γ
represent in relational algebra? - What si the operation written as?
Aggregate
Greek Letter: gamma
Fill in the blank: The symbol σ
corresponds to the operation _______ in relational algebra.
Select
Fill in the blank: The symbol π
corresponds to the operation _______ in relational algebra.
Project
Fill in the blank: The symbol ρ
corresponds to the operation _______ in relational algebra.
Rename
Fill in the blank: The symbol γ
corresponds to the operation _______ in relational algebra.
Aggregate
What is the operation equivalent to SQL SELECT
?
SELECT * FROM Table WHERE expression
This operation retrieves rows from a table that meet a specified condition.
What is the relational algebra operation equivalent to SQL PROJECT
?
SELECT Column1, Column2
This operation selects specific columns from a table.
What is the relational algebra operation equivalent to SQL PRODUCT
?
SELECT * FROM Table1 CROSS JOIN Table2
This operation combines all rows from Table1 with all rows from Table2.
What is the relational algebra operation equivalent to SQL JOIN
?
SELECT * FROM Table1 INNER JOIN Table2 ON expression
This operation combines rows from two tables based on a related column.
What is the relational algebra operation equivalent to SQL UNION
?
SELECT * FROM Table1 UNION SELECT * FROM Table2
This operation combines the results of two queries, removing duplicates.
What is the relational algebra operation equivalent to SQL INTERSECT
?
SELECT * FROM Table1 INTERSECT SELECT * FROM Table2
This operation retrieves rows that are common to both tables.
What is the relational algebra operation equivalent to SQL DIFFERENCE
?
SELECT * FROM Table1 MINUS SELECT * FROM Table2
This operation retrieves rows from Table1 that are not in Table2.
What is the relational algebra operation equivalent to SQL RENAME
?
No direct SQL syntax; typically done by aliasing
This operation allows renaming tables and columns for clarity.
What is the relational algebra operation equivalent to SQL AGGREGATE
?
SELECT GroupColumn, Function(Column) FROM Table GROUP BY GroupColumn
aggregate functions is typically expressed using the Group By operation along with an Aggregation operation
Set operations in MYSQL
What operations are part of the SQL standard?
- The
UNION
,INTERSECT
, andMINUS
keywords are part of the SQL standard. - MySQL supports
UNION
but notINTERSECT
andMINUS
.
Set operations in MYSQL
A ∩ B
What is this implememented as in SQL
Set operations in MYSQL
A - B
What is this implememented as in SQL
SELECT A1, A2 FROM A LEFT JOIN B ON (A1 = B1 AND A2 = B2) WHERE B1 IS NULL;
What are equivalent expressions in relational algebra?
Relational algebra expressions that operate on the same tables and yield the same result.
What is the primary role of a query optimizer?
To convert an SQL query into a sequence of lower-level database actions known as a query execution plan.
List the steps involved in the query optimization process.
Convert, generate, est, determine, convert
- Convert SQL query into a relational algebra expression
- Generate equivalent expressions
- Estimate the cost associated with each operation
- Determine the optimal expression with the lowest total cost
- Convert the optimal expression into a query execution plan
What does cost estimation in query optimization refer to?
A numeric estimate of the processing time required, combining storage media access and computation time.
The order of operations does not change the _______ of relational algebra expressions.
[result]
How does changing the order of operations alter the result of an expression.
The order of operations does not change the result of relational algebra expressions.
How should you plan to execute each SQL query?
There can be multiple execution plans for a single SQL query.
How do Query optimizers typically choose an optimal expression?
- They consider various factors, including the estimated cost of operations, the efficiency of accessing data, and overall query execution time.
- Query optimizers do not solely rely on the total number of rows processed.
Which operation is 1 & which is 2?
- select
- project
- What is the select statement committing?
- What is the result?
- The select operation selects employees who earn more than $50,000 in salary.
- What is the project statement committing?
- What is the result?
- What is the select statement committing?
- What is the result?
- What is the product statement committing?
- What is the result?
- The product operation generates the combination of all rows from both tables.
How would Maria Rodriguez be in the union result?
The union operation combines all rows of two compatible tables into a single table
Maria Rodriguez is both an employee and a student, and thus appears twice in result
Duplicate rows are excluded from the result table.
How many rows are in the table defined by the following expression?
- If the values were the same but the column names were different, the intersection would still be valid
- The
INTERSECT
operation focuses on matching values rather than column names.
Result 0
What does the operation A−B represent?
The difference between two sets, retrieving all rows from table A
that are not present in table B
.
retrieving is the result, the leftovers after the substraction
What SQL syntax is used to achieve the set difference A−B?
SELECT A1, A2 FROM A LEFT JOIN B ON (A1 = B1 AND A2 = B2) WHERE B1 IS NULL;
What does a LEFT JOIN do in SQL?
Joins all rows from table A with matching rows from table B, selecting rows from A even if there is no match.
What condition is used to filter results in the set difference query?
WHERE B1 IS NULL
What does the condition WHERE B1 IS NULL
indicate?
It includes only those rows from A
that do not have corresponding entries in B
.
Why is the LEFT JOIN
technique necessary in some SQL databases?
Because they do not support the MINUS
or EXCEPT
operators directly.
Using except
What is an alternative syntax for achieving the set difference if supported?
Would you consider The LEFT JOIN
technique as a simpler option than using the MINUS
or EXCEPT
operators?
-
LEFT JOIN
requires more steps to achieve the same result, as it involves joining tables and then filtering results based on NULL values. -
MINUS
orEXCEPT
directly expresses the intention to find the difference between two sets, making the query clearer and more concise.
What SQL statement is used to write the intersection using aliases A and B?
This statement returns all rows present in both table A and table B.
What can be used instead of the INTERSECT statement if the SQL database does not support it?
INNER JOIN
The INNER JOIN can achieve the same result as the intersection operation.
How would you rewrite an INNER JOIN
to find the intersection of tables A
and B
?
Replace column1 and column2 with the appropriate column names for matching.
This SQL statement does what?
SELECT * FROM A INTERSECT SELECT * FROM B;
-
INTERSECT
compares two queries and returns only the rows that are in both queries. - It’s useful for finding overlapping data between tables or queries
Why would the INNER JOIN
yield similar results to an INTERSECT
statement.
Both approaches yield the same effect in terms of returning intersecting rows.
What does the operation result in?
- No
Student
rows are identical, in all three columns, to anyEmployee
rows. - Therefore the difference operation does not remove any of the three
Employee
rows.
Which particular change within the order of operations; could alter the result of an expression.
- Changing the order of other operations, such as select and aggregate, can alter the result.
- The order of some operations, such as select and project, can be reversed without affecting the result.
How can a execution plan possibly have more than one SQL query.
- Query optimizers generate multiple execution plans, simultaneously estimating the running time for each, after which choosing the fastest plan.
- Most queries can be executed in many different ways.
How does a Query optimizer typically choose optimal expressions?
Most query optimizers use more precise measures; such as bytes read from storage media or CPU processing time.
Why wouldn’t the JOIN
operation be considered a low-level database action.
- The join operation is complex and can be executed in many different ways.
- Query optimizers decompose join operations into other operations, such as product and select, prior to generating a query execution plan.
What is Relational Algebra?
A formal language that serves as the foundation for languages like SQL.
What do queries in Relational Algebra operate on?
Relations.
Name three types of relations used in the context of relational algebra.
- College Relation
- Student Relation
- Application Relation
What fields are contained in the Student Relation?
- Student ID
- Name
- GPA
- High school size
What are the unique attributes in the relations discussed?
- College name
- Student ID
- Application major
What is the purpose of the Select Operator (σ)?
To filter rows based on conditions.
Provide an expression.
Students with GPA>3.7
σ(GPA > 3.7)(Student)
Provide an expression.
Students with GPA>3.7 and HS<1000
σ(GPA > 3.7) AND (HS < 1000) (Student)
in SQL, the logical ‘AND’ operator is represented by the word AND rather than a symbol (^
).
Provide an expression.
Applications to Stanford CS major
σ(cName= ‘Stanford’) AND (major= ‘cs’)
What does the Project Operator (π) do?
Selects certain columns from a relation.
Provide an Expression
ID and decisions of all applications
Provide an Expression
ID and name of students with GPA>3.7
How can Select and Project Operators be combined?
Operators can be composed.
Provide an expression
List of application majors and decisions
How does relational algebra handle duplicates?
It eliminates duplicates in query results. Learn more
Provide an expression
Names and GPAs of students with HS>1000 who applied to CS and were rejected
what is a Cross-Product:
Combines two relations. Video
Provide an expression
Names and GPAs of students with HS>1000 who applied to CS and were rejected
what is a Natural Join:
You dont have to write the condition. Video
What does the Natural Join (∞) operator do?
Performs a cross-product while enforcing equality on all attributes with the same name.
What is the Theta Join (θ)?
Applies a condition to the result of the cross-product of two relations.
List the primary operators of Relational Algebra.
- Select
- Project
- Cross-Product
- Natural Join
- Theta Join
Fill in the blank: The __________ operator is used for selecting columns.
Project
What is the effect of a natural join on duplicate columns?
Removes duplicate columns
What is a natural join equivalent to?
Performing a cross-product followed by a selection enforcing equality on common attributes