3.4 Join queries Flashcards
What is a join in relational databases?
Which operator does it use?
- A join is a SQL
SELECT
statement that combines data from two tables into a single result by comparing columns. - Typically uses 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.
- The left table mentioned first
- Then 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)
- Aliases (using the AS keyword)
either of these methods will help the database system understand which column you’re referring to when there are duplicates
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, returningNULL
values where there are no matches. -
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 Name and Department Name on their Code fields?
SELECT FacultyName, DepartmentName FROM Faculty INNER JOIN Department ON Faculty.Code = Department.Code;
Use INNER JOIN for only the matching rows.
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 by using a combination of WHERE
clauses and the UNION
operator to ensure unmatched rows are included.
LEFT JOIN
can be represented without the JOIN keyword
What is the significance of the ON
clause in a join statement?
The ON
clause specifies which columns are used to join the tables, indicating how to match rows between the left and right tables.
Also known as specifying the condition
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.
How can you use more than two tables in a JOIN
query?
When someone says “join query,” they could be referring to any of the 6 specific types of joins
- You can join multiple tables by chaining multiple
JOIN
clauses. - Then specify the conditions for each table’s
JOIN
usingON
.
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.
What is the result set of an INNER JOIN
if there are no matching rows?
If there are no matching rows, the result set of an INNER JOIN
will be empty.
What does the LEFT OUTER JOIN
clause do?
A LEFT OUTER JOIN
returns all records from the left table along with matched records from the right table, showing NULL
values for unmatched rows from the right.
How do you simplify column names in a result set created by a join?
By using aliases with the AS
keyword to rename the output columns, making the results easier to read.
What is the purpose of the UNION
operator in SQL?
- The
UNION
operator is used to combine the result sets of two or moreSELECT
statements. - Also ensures that duplicate rows are removed from the output.
How doJOIN
statements affect the performance of a query?
-
JOIN
statements are essential for combining data from multiple tables in relational databases.- While they can impact performance, especially with large datasets, proper indexing mitigates this.
- This avoids redundancy and enables complex queries.
What happens to unmatched rows in a FULL OUTER JOIN
?
- In a
FULL OUTER JOIN
, all rows from both tables are included in the result set. - When a row has a match in the other table, the corresponding columns from both tables are combined in the output.
- However, when a row doesn’t have a match in the other table, the columns from the other table for that row will be filled with
NULL
values.
What is the distinction between a join and a subquery?
- A join combines data from multiple tables in a single query.
- A subquery is a query nested inside another SQL query, often used to produce a filter criteria or additional data.
Well it’s the result of the following query?
SELECT FacultyName, DepartmentName FROM Faculty FULL JOIN Department ON Faculty.Code = Department.Code;
What is the result of the following query?
SELECT Department.Name AS Group, Employee.Name AS Supervisor FROM Department INNER JOIN Employee ON Manager = ID;
The INNER JOIN
returns only the rows with matching Manager and ID values.
What is the result of the following query?
SELECT Department.Name AS Group, Employee.Name AS Supervisor FROM Department LEFT JOIN Employee ON Manager = ID;
The LEFT JOIN returns all rows from the Department table and matching rows from the Employee table. Unmatched rows will return NULL.
What is the result of the following query?
SELECT Department.Name AS Group, Employee.Name AS Supervisor FROM Department RIGHT JOIN Employee ON Manager = ID;
The RIGHT JOIN
returns all rows from the Employee table and matching rows from the Department table. Unmatched rows will return NULL
.
What is the result of the following query?
SELECT Department.Name AS Group, Employee.Name AS Supervisor FROM Department FULL JOIN Employee ON Manager = ID;
The FULL JOIN returns all rows from both tables, showing NULLs where there are no matches.
What is the result of the following query?
SELECT Department.Name, Employee.Name FROM Department, Employee WHERE Manager = ID UNION SELECT Department.Name, NULL FROM Department WHERE Manager IS NULL;
This query first performs an INNER JOIN and then adds unmatched Department rows using UNION.
What is the result of the following query?
SELECT FacultyName, DepartmentName FROM Faculty LEFT JOIN Department ON Faculty.Code = Department.Code;
The LEFT JOIN returns all faculties and the corresponding departments when available.
What is the result of the following query?
SELECT * FROM Faculty LEFT JOIN Department ON Faculty.Code = Department.Code WHERE DepartmentName IS NULL;
This identifies faculty with no matching department.
What is the result of the following query?
SELECT Faculty.FacultyName, Department.DepartmentName FROM Faculty RIGHT JOIN Department ON Faculty.code = Department.code ORDER BY Department Name;
The RIGHT JOIN returns all departments, sorted by their names, with faculty if available.
What’s the difference between UNION and INNER join?
- Use the UNION approach for a complete view, including unmatched rows.
- Use INNER JOIN for only the matching rows.