3.4 Join queries Flashcards

1
Q

What is a join in relational databases?

Which operator does it use?

A
  1. A join is a SQL SELECT statement that combines data from two tables into a single result by comparing columns.
  2. Typically uses 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.

  1. The left table mentioned first
  2. Then 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
  1. Prefixes: using the table name followed by a period)
  2. 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

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
  1. A FULL JOIN selects all rows from both tables, returning NULL values where there are no matches.
  2. 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
  1. A LEFT JOIN returns all rows from the left table and the matching rows from the right table.
  2. 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 Name and Department Name on their Code fields?

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

Use INNER JOIN for only the matching rows.

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

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

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

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

A
  1. You can join multiple tables by chaining multiple JOIN clauses.
  2. Then specify 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
  1. The UNION operator is used to combine the result sets of two or more SELECT statements.
  2. Also ensures that duplicate rows are removed from the output.
20
Q

How doJOIN statements affect the performance of a query?

A
  • 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.
21
Q

What happens to unmatched rows in a FULL OUTER JOIN?

A
  1. In a FULL OUTER JOIN, all rows from both tables are included in the result set.
  2. When a row has a match in the other table, the corresponding columns from both tables are combined in the output.
  3. 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.
22
Q

What is the distinction between a join and a subquery?

A
  1. A join combines data from multiple tables in a single query.
  2. A subquery is a query nested inside another SQL query, often used to produce a 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;
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?

SELECT Faculty.FacultyName, Department.DepartmentName 
FROM Faculty RIGHT JOIN Department 
ON Faculty.code = Department.code 
ORDER BY Department Name;
A

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

32
Q

What’s the difference between UNION and INNER join?

A
  • Use the UNION approach for a complete view, including unmatched rows.
  • Use INNER JOIN for only the matching rows.