Join Tables Flashcards

1
Q

List the 6 steps that are involved in writing a query that requires joining tables:

A
  1. Identify the entities and relationships
  2. Define the cardinality of the relationships
  3. Identify the logical expressions for those relationships
  4. Determine whether to include entities for which the logical expressions are never satisfied
  5. Write the FROM clause
  6. Write the SELECT clause

(6.5)

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

Where are table aliases defined?

A

In the FROM clause.

6.5

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

Are table aliases required to be unique?

A

Yes. Each table alias in the FROM clause is unique.

6.5

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

Why is it important to identify which entity relationship will be used by the query?

A

Because multiple relationships between the entities are possible.

(6.6)

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

What does the cardinality of a relationship describe?

A

The numeric relationship between two entities.

6.6

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

The cardinality of a relationship can be summarized with a phrase of the form “M to N” where M and N are each one of four phrases. What are those phrases?

A
  • Zero or One
  • Zero or More
  • One (One exactly)
  • One or More

(6.6 - 6.7)

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

Should you avoid many-to-many relationships?

A

Yes

6.8

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

“One to Zero or More” can be read as…

A

1-to-many

6.8

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

“Zero or More to One exactly” can be read as…

A

many-to-1

6.8

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

“Zero or more to Zero or More” can be read as…

A

many-to-many

6.8

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

What is each type of entity represented in a relational database known as?

A

A table.

6.9

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

What does the join type of a join dictate?

A

Whether or not rows in one table that have no valid matches in the other table will be in the query result.

(6.10)

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

What type of join only includes rows that represent a match between the two tables?

A

The INNER JOIN (or just JOIN)

6.10

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

What type of join should you use if you want your query result to only include valid matches?

A

INNER JOIN

6.10

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

Why is an INNER JOIN an appropriate choice for relationship cardinalities that don’t include “Zero or…” on either side?

A

If there aren’t “Zero or…” on either side than the INNER JOIN would return everything.

(6.10)

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

What rows does a LEFT OUTER JOIN keep?

A

Rows in the left table even if there is not match in the right table.

(6.11)

17
Q

In terms of relationship cardinality, when is the LEFT OUTER JOIN an appropriate choice?

A
  1. When the right side is “Zero or…”
  2. When the query result should include rows in the left table even if there is no match in the right table

(6.11)

18
Q

Why would you swap the order of the tables when using a LEFT OUTER JOIN?

A

To keep all rows in the right table rather than the left.

6.11

19
Q

How do you know which table is considered the left table?

A

The table listed before the join type is considered the left table.

(6.12)

20
Q

When using a LEFT OUTER JOIN, why would you put a logical expression in the JOIN condition instead of the WHERE clause?

A

When the logical expression should only affect the right table.

(6.12)

21
Q

What operator would you use when adding a logical expression in a JOIN clause?

A

AND

Ex: AND PAT_ENC_DX.PRIMARY_DX_YN = ‘Y’

(6.12-6.13)

22
Q

Which clause always maintains or reduces the number of rows in the result?

A

The WHERE clause.

6.13

23
Q

What’s the difference between putting a condition in the WHERE clause verses a LEFT OUTER JOIN?

A

Conditions in the WHERE clause remove rows from the overall query result whereas a LEFT OUTER JOIN condition just ignores mismatches in the right table.

(6.12)

24
Q

How many joins does the FROM clause evaluate at a time?

A

One at a time in the order listed.

Note: If you perform a left outer join, then the columns that came from the right table may be NULL for some rows. If you then perform an inner join, those rows may be discarded if NULL s are not properly handled.

(6.18)

25
Q

What allows you to assign new and temporary names to tables?

A

Table aliases

6.23

26
Q

Where are table aliases defined?

A

In the FROM clause.

6.23

27
Q

Why can table aliases be used in all the clauses?

A

Because the FROM clause is evaluated first.

6.23

28
Q

Is it required to enclosed table aliases in either double quotes or square brackets?

A

The table alias must be enclosed in double quotes or square brackets if the table alias has a space in it.

(6.23)

29
Q

What keyword can you use to make the table alias explicit?

A

You can use the AS keyword in a table alias to make it explicit, but it is not required.

(6.23)

30
Q

Why would a table need to be included in the FROM clause two times?

A

If a single row of the query result needs to include information from two distinct rows.

(6.24)

31
Q

Do the table aliases have to be unique if the FROM clause references a table more than once?

A

Yes. If a table is included in the FROM clause more than one, then aliases are required to tell each copy apart.

(6.24)

32
Q

Why would a table ever be joined to itself?

A

If both entities are found in the same table.

This is also an example of when table aliases are required.

(6.25)

33
Q

What are entities represented by?

A

Tables.

6.27

34
Q

What are relationships represented by?

A

Foreign keys

6.28

35
Q

Are all relationships represented by foreign keys?

A

No. The idea that relationships are foreign keys is a rule of thumb. It does not always work.

(6.29)