3.4 Join queries Flashcards
What is a join in relational databases?
A join is a SQL SELECT statement that combines data from two tables into a single result by comparing columns, typically using the ‘=’ operator for columns with comparable data types.
When writing a join query, how are the left and right tables defined?
The left and right tables are specified in the FROM clause, with the left table mentioned first and the join clause indicating the right table.
In a join where duplicates exist in column names, what tools can be used to resolve this issue?
Prefixes (using the table name followed by a period) and aliases (using the AS keyword) can distinguish columns with the same name.
What is the purpose of the INNER JOIN clause?
The INNER JOIN clause selects only those rows that have matching values in both tables being joined.
How does a FULL JOIN differ from an INNER JOIN?
A FULL JOIN selects all rows from both tables, returning NULL values where there are no matches, while INNER JOIN only returns rows with matching values.
What does a LEFT JOIN do?
A LEFT JOIN returns all rows from the left table and the matching rows from the right table; if there’s no match, NULLs appear in columns from the right table.
How can you combine results from LEFT and RIGHT joins in SQL?
You can use the UNION operator to combine the results from a LEFT JOIN and a RIGHT JOIN to include all relevant rows from both tables.
What are the consequences of using duplicate column names in your SQL results?
Duplicate column names can lead to ambiguity in the result set; hence, proper prefixes or aliases are necessary to clarify which table the data is coming from.
How would you write a query to join the tables Faculty and Department on their Code fields?
SELECT FacultyName, DepartmentName FROM Faculty INNER JOIN Department ON Faculty.Code = Department.Code;
What SQL statement can be used to perform an alternative to INNER JOIN while ensuring unmatched rows are included?
An equivalent LEFT JOIN can be represented without the JOIN keyword by using a combination of WHERE clauses and the UNION operator to ensure unmatched rows are included.
What is the significance of the ON clause in a join statement?
The ON clause specifies the columns used to join the tables, indicating how to match rows between the left and right tables.
What result does a RIGHT JOIN yield when there’s no match found?
A RIGHT JOIN will include all rows from the right table and NULLs for the columns of the left table where no match is found.
Can you use more than two tables in a join query? If so, how?
Yes, you can join multiple tables by chaining multiple JOIN clauses, specifying the conditions for each table’s JOIN using ON.
How do you handle situations where you want to join two tables that don’t have related columns?
When there are no related columns, a CROSS JOIN can be used, which produces a Cartesian product of the two tables.
In what scenario would you use a self-join?
A self-join is used to join a table with itself to compare rows within the same table, often used for hierarchical or recursive data relationships.