3.5 Equijoins, self-joins, and cross-joins Flashcards

1
Q

What is the result of the following equijoin query?

SELECT Class.Name, Student.Name
FROM Class
INNER JOIN Student
ON Student.Code = Class.Code;
A

The INNER JOIN returns rows where Student.Code matches Class.Code.

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

What is the result of the following equijoin query?

SELECT Name, Address
FROM Buyer
LEFT JOIN Property
ON Price < MaxPrice;
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which join type is the following query?

A

Self-join

SELECT A.Name, B.Name
FROM EmployeeManager A
INNER JOIN EmployeeManager B
ON B.ID = A.Manager;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the following cross-join query result in?

SELECT Model, Gigabytes
FROM IPhone
CROSS JOIN Storage;
A

CROSS JOIN generates all combinations of IPhone models and storage capacities.

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

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

Avg (3.5+3.3+2.9+3.0)/4 = 3.1

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

What is the primary use of equijoins?

A
  • To combine rows from tables where specified columns equal each other.
  • Equijoins are typical for matching columns using the = operator.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which SQL command combines two tables using conditions other than the equality operator?

A

Non-equijoin.

Non-equijoins utilize comparison operators != , >, <, >= and <=

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

What does it mean when we refer to a ‘Cartesian product’ in SQL?

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

How do we distinguish same-table columns in a self-join?

A

Aliases act as prefixes to differentiate joined columns of the same table.

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

What is the result when using non-equijoin operators?

Non-equijoins utilize comparison operators != , >, <, >= and <=.

A

Returns rows where the compare columns meet the specified non-equal condition.

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

Why are aliases necessary in self-joins?

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

Identify the join clause(s) usable in non-equijoin queries.

Non-equijoins utilize comparison operators != , >, <, >= and <=

A

All JOIN clauses.

Non-equijoin conditions can apply with INNER, LEFT, RIGHT, or FULL JOINs.

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

What does a cross-join do without comparing columns?

A

Combines all rows from two tables into all possible pairs.

CROSS JOIN results in the Cartesian product of two tables.

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

What is an INNER JOIN?

A

Combines rows from both tables where the join condition is met.

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

What SQL statement is used for an INNER JOIN?

A
SELECT <fields> 
FROM TableA A 
INNER JOIN TableB B 
ON A.key = B.key
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a LEFT JOIN?

A

Returns all rows from the left table and matched rows from the right table.

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

What SQL statement is used for a LEFT JOIN?

A
SELECT <fields> 
FROM TableA A 
LEFT JOIN TableB B 
ON A.key = B.key

Where B.key is NULL

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

What is a RIGHT JOIN?

A

Returns all rows from the right table and matched rows from the left table.

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

What SQL statement is used for a RIGHT JOIN?

A
SELECT <fields> 
FROM TableA A 
RIGHT JOIN TableB B 
ON A.key = B.key
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a LEFT JOIN with NULL condition?

A

A LEFT JOIN that filters for rows where the right table has no match.

21
Q

What SQL statement is used for a LEFT JOIN with NULL condition?

A
SELECT <fields>
FROM TableA A 
LEFT JOIN TableB B 
ON A.key = B.key 
WHERE B.key IS NULL
22
Q

What is a RIGHT JOIN with NULL condition?

A

A RIGHT JOIN that filters for rows where the left table has no match.

23
Q

What SQL statement is used for a RIGHT JOIN with NULL condition?

A
SELECT <fields> 
FROM TableA A 
RIGHT JOIN TableB B 
ON A.key = B.key 
WHERE A.key IS NULL
24
Q

What is a FULL OUTER JOIN with NULL condition?

A

Combines results from both tables, including unmatched rows from both sides.

25
Q

What SQL statement is used for a FULL OUTER JOIN with NULL condition?

A
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.
26
Q

What is a Natural Join?

A

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.

27
Q

What does an Equi Join do?

A

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;

28
Q

What is the syntax for an Inner Join?

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

29
Q

What does an Inner Join return?

A

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.

30
Q

How does a Natural Join work?

A

It removes duplicate columns, keeping only one instance of each common column in the result set.

31
Q

How does an Equi Join return rows and handle column values

A
  1. If there are no matching column values between the tables being joined, an equi join will not return any rows.
  2. It only returns rows where the condition of equality is met.
32
Q

In an Inner Join, what is required for the rows to be included in the result set?

A

Rows must exist in both tables

33
Q

The syntax for a Natural Join is

SELECT * FROM table_name1 \_\_\_\_\_\_\_ table_name2;

A

NATURAL JOIN

34
Q

What is a join in the context of databases?

A

A join is an operation that combines records from two or more tables based on a related column.

35
Q

An INNER JOIN returns only _

A

the rows that have matching values in both tables.

36
Q

What type of join returns all records from the left table and the matched records from the right table?

A

LEFT JOIN

37
Q

A RIGHT JOIN returns all records from the _

A

Returns all records from right table and the matched records from the left table.

38
Q

Which join type includes all records from both tables and fills in NULLs for unmatched records?

A

FULL OUTER JOIN

39
Q

What is the primary difference between INNER JOIN and OUTER JOIN?

A
  1. INNER JOIN returns only matched records
  2. OUTER JOIN returns all records from one or both tables regardless of matches.
40
Q

CROSS JOIN combines every row of the first table with _

A

Every row of the first with every row of the second table.

41
Q

What is a self join?

A

A self join is a join where a table is joined with itself.

42
Q

Which join type would you use to find records in one table that do not exist in another?

A

LEFT JOIN with a NULL check on the right table.

43
Q

What is the main characteristic of a NATURAL JOIN?

A

A NATURAL JOIN automatically matches columns with the same names in both tables.

44
Q

A ______ JOIN is used when you want to combine rows from two tables based on a related column and eliminate duplicate data.

A

INNER

45
Q

What is the result of a LEFT JOIN if there are no matches found in the right table?

A

All records from the left table will be returned with NULLs in the columns from the right table.

46
Q

An OUTER JOIN can be further classified into __ joins.

A

An OUTER JOIN can be further classified into LEFT, RIGHT, and FULL OUTER joins.

47
Q

Which join does not compare values from the joined tables?

A

CROSS JOIN

It does not use a join condition to compare values between tables.

48
Q

What is the SQL syntax for an INNER JOIN?

A

SELECT columns
FROM table1 INNER JOIN table2
ON table1.column = table2.column;