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
What is a LEFT JOIN with NULL
condition?
A LEFT JOIN that filters for rows where the right table has no match.
What SQL statement is used for a LEFT JOIN with NULL condition?
SELECT <fields> FROM TableA A LEFT JOIN TableB B ON A.key = B.key WHERE B.key IS NULL
What is a RIGHT JOIN with NULL condition?
A RIGHT JOIN that filters for rows where the left table has no match.
What SQL statement is used for a RIGHT JOIN with NULL condition?
SELECT <fields> FROM TableA A RIGHT JOIN TableB B ON A.key = B.key WHERE A.key IS NULL
What is a FULL OUTER JOIN with NULL condition?
Combines results from both tables, including unmatched rows from both sides.
What SQL statement is used for a FULL OUTER JOIN with NULL condition?
SELECT <fields> FROM TableA A FULL OUTER JOIN TableB B ON A.key = B.key WHERE A.key IS NULL OR B.key IS NULL.
What is a Natural Join?
A join that combines tables based on the same column names and their data types
It returns all attributes of both tables along with unique duplicate columns in the result set.
What does an Equi Join do?
It joins tables based on the equality or matching column values in the associated tables
Syntax: SELECT table_name1, table_name2 FROM table_name1, table_name2;
What is the syntax for an Inner Join?
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
It joins tables based on the column name specified in the ON clause explicitly.
What does an Inner Join return?
Only those rows that exist in both tables
It returns all the attributes of both tables along with duplicate columns that match the ON clause condition.
How does a Natural Join work?
It removes duplicate columns, keeping only one instance of each common column in the result set.
How does an Equi Join
return rows and handle column values
- If there are no matching column values between the tables being joined, an equi join will not return any rows.
- It only returns rows where the condition of equality is met.
In an Inner Join, what is required for the rows to be included in the result set?
Rows must exist in both tables
The syntax for a Natural Join is
SELECT *
FROM table_name1 \_\_\_\_\_\_\_ table_name2;
NATURAL JOIN
What is a join in the context of databases?
A join is an operation that combines records from two or more tables based on a related column.
An INNER JOIN returns only _
the rows that have matching values in both tables.
What type of join returns all records from the left table and the matched records from the right table?
LEFT JOIN
A RIGHT JOIN returns all records from the _
Returns all records from right table and the matched records from the left table.
Which join type includes all records from both tables and fills in NULLs for unmatched records?
FULL OUTER JOIN
What is the primary difference between INNER JOIN and OUTER JOIN?
- INNER JOIN returns only matched records
- OUTER JOIN returns all records from one or both tables regardless of matches.
CROSS JOIN combines every row of the first table with _
Every row of the first with every row of the second table.
What is a self join?
A self join is a join where a table is joined with itself.
Which join type would you use to find records in one table that do not exist in another?
LEFT JOIN with a NULL check on the right table.
What is the main characteristic of a NATURAL JOIN?
A NATURAL JOIN automatically matches columns with the same names in both tables.
A INNER JOIN
is used when you want ?
A INNER JOIN
is used when you want to combine rows from two tables based on a related column and also eliminates duplicate data.
What is the result of a LEFT JOIN
if there are no matches found in the right table?
All records from the left table will be returned with NULL
s in the columns from the right table.
An OUTER JOIN
can be further classified into __ joins.
An OUTER JOIN can be further classified into LEFT
, RIGHT
, and FULL OUTER
joins.
Which join does not compare values from the joined tables?
CROSS JOIN
It does not use a join condition to compare values between tables.
What is the SQL syntax for an INNER JOIN?
SELECT columns
FROM table1 INNER JOIN table2
ON table1.column = table2.column;