3.5 Equijoins, self-joins, and cross-joins Flashcards
What is the result of the following equijoin query?
SELECT Class.Name, Student.Name FROM Class INNER JOIN Student ON Student.Code = Class.Code;
The INNER JOIN
returns rows where Student.Code matches Class.Code.
What is the result of the following equijoin query?
SELECT Name, Address FROM Buyer LEFT JOIN Property ON Price < MaxPrice;
Which join type is the following query?
Self-join
SELECT A.Name, B.Name FROM EmployeeManager A INNER JOIN EmployeeManager B ON B.ID = A.Manager;
What does the following cross-join query result in?
SELECT Model, Gigabytes FROM IPhone CROSS JOIN Storage;
CROSS JOIN generates all combinations of IPhone models and storage capacities.
What is the result of the following query?
SELECT Student.Name FROM Student INNER JOIN Class ON Student.Code = Class.Code WHERE Student.StudentGrade > $ (SELECT AVG(StudentGrade) FROM Student WHERE Student.Code = Class.Code);
Avg (3.5+3.3+2.9+3.0)/4 = 3.1
What is the primary use of equijoins?
- To combine rows from tables where specified columns equal each other.
- Equijoins are typical for matching columns using the
=
operator.
Which SQL command combines two tables using conditions other than the equality operator?
Non-equijoin.
Non-equijoins utilize comparison operators != , >, <, >= and <=
What does it mean when we refer to a ‘Cartesian product’ in SQL?
- The result of a cross-join containing all possible row combinations.
- A Cartesian product pairs each row from the first table with every row from the second.
How do we distinguish same-table columns in a self-join?
Aliases act as prefixes to differentiate joined columns of the same table.
What is the result when using non-equijoin operators?
Non-equijoins utilize comparison operators != , >, <, >= and <=.
Returns rows where the compare columns meet the specified non-equal condition.
Why are aliases necessary in self-joins?
- When you join a table to itself, the columns from each instance of the table could have the same names, which can lead to ambiguity.
- Using aliases allows you to create unique identifiers for each instance of the table, making it clear which set of columns belongs to which table instance.
Identify the join clause(s) usable in non-equijoin queries.
Non-equijoins utilize comparison operators != , >, <, >= and <=
All JOIN clauses.
Non-equijoin conditions can apply with INNER, LEFT, RIGHT, or FULL JOINs.
What does a cross-join do without comparing columns?
Combines all rows from two tables into all possible pairs.
CROSS JOIN results in the Cartesian product of two tables.
What is an INNER JOIN
?
Combines rows from both tables where the join condition is met.
What SQL statement is used for an INNER JOIN
?
SELECT <fields> FROM TableA A INNER JOIN TableB B ON A.key = B.key
What is a LEFT JOIN?
Returns all rows from the left table and matched rows from the right table.
What SQL statement is used for a LEFT JOIN?
SELECT <fields> FROM TableA A LEFT JOIN TableB B ON A.key = B.key
Where B.key is NULL
What is a RIGHT JOIN?
Returns all rows from the right table and matched rows from the left table.
What SQL statement is used for a RIGHT JOIN?
SELECT <fields> FROM TableA A RIGHT JOIN TableB B ON A.key = B.key