3.4 Join queries Flashcards

1
Q

What is a join in relational databases?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When writing a join query, how are the left and right tables defined?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In a join where duplicates exist in column names, what tools can be used to resolve this issue?

A

Prefixes (using the table name followed by a period) and aliases (using the AS keyword) can distinguish columns with the same name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the INNER JOIN clause?

A

The INNER JOIN clause selects only those rows that have matching values in both tables being joined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does a FULL JOIN differ from an INNER JOIN?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does a LEFT JOIN do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you combine results from LEFT and RIGHT joins in SQL?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the consequences of using duplicate column names in your SQL results?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you write a query to join the tables Faculty and Department on their Code fields?

A
SELECT FacultyName, DepartmentName 
FROM Faculty INNER JOIN Department 
ON Faculty.Code = Department.Code;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What SQL statement can be used to perform an alternative to INNER JOIN while ensuring unmatched rows are included?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the significance of the ON clause in a join statement?

A

The ON clause specifies the columns used to join the tables, indicating how to match rows between the left and right tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What result does a RIGHT JOIN yield when there’s no match found?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you use more than two tables in a join query? If so, how?

A

Yes, you can join multiple tables by chaining multiple JOIN clauses, specifying the conditions for each table’s JOIN using ON.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you handle situations where you want to join two tables that don’t have related columns?

A

When there are no related columns, a CROSS JOIN can be used, which produces a Cartesian product of the two tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In what scenario would you use a self-join?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the result set of an INNER JOIN if there are no matching rows?

A

If there are no matching rows, the result set of an INNER JOIN will be empty.

17
Q

What does the LEFT OUTER JOIN clause do?

A

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.

18
Q

How do you simplify column names in a result set created by a join?

A

By using aliases with the AS keyword to rename the output columns, making the results easier to read.

19
Q

What is the purpose of the UNION operator in SQL?

A

The UNION operator is used to combine the result sets of two or more SELECT statements, ensuring that duplicate rows are removed from the output.

20
Q

Can JOIN statements affect the performance of a query? How?

A

Yes, JOIN statements can affect performance, especially with large datasets, as they may require substantial processing to match rows, especially if indexes are not properly used.

21
Q

What happens to unmatched rows in a FULL OUTER JOIN?

A

In a FULL OUTER JOIN, unmatched rows from both tables are included, with NULLs filled in for the columns of the table that doesn’t have a matching row.

22
Q

What is the distinction between a join and a subquery?

A

A join combines data from multiple tables in a single query, while a subquery is a query nested inside another SQL query, often used to produce filter criteria or additional data.

23
Q

Well it’s the result of the following query?

SELECT FacultyName, DepartmentName 
FROM Faculty
FULL JOIN Department 
ON Faculty.Code = Department.Code;
A
24
Q

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;
A

The INNER JOIN returns only the rows with matching Manager and ID values.

25
Q

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;
A

The LEFT JOIN returns all rows from the Department table and matching rows from the Employee table. Unmatched rows will return NULL.

26
Q

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;
A

The RIGHT JOIN returns all rows from the Employee table and matching rows from the Department table. Unmatched rows will return NULL.

27
Q

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;
A

The FULL JOIN returns all rows from both tables, showing NULLs where there are no matches.

28
Q

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;
A

This query first performs an INNER JOIN and then adds unmatched Department rows using UNION.

29
Q

What is the result of the following query?

SELECT FacultyName, DepartmentName
FROM Faculty
LEFT JOIN Department
ON Faculty.Code = Department.Code;
A

The LEFT JOIN returns all faculties and the corresponding departments when available.

30
Q

What is the result of the following query?

SELECT *
FROM Faculty
LEFT JOIN Department
ON Faculty.Code = Department.Code
WHERE DepartmentName IS NULL;
A

This identifies faculty with no matching department.

31
Q

What is the result of the following query?

A

The RIGHT JOIN returns all departments, sorted by their names, with faculty if available.