21
Bob Litsinger [email protected] Tables working together Joins and other joint table operations

Understanding Joins And Joint Table Operations

Embed Size (px)

DESCRIPTION

Slide Presentation: Understanding SQL Joins and Other Joint Table Operations

Citation preview

Page 1: Understanding Joins And Joint Table Operations

Bob [email protected]

Tables working togetherJoins and other joint table operations

Page 2: Understanding Joins And Joint Table Operations

Bob [email protected]

ContentsUnderstanding Join and Joint Table Operations

Setting the stage (preliminary discussion and review of dataset) 3

Joins 7 Other Joint Table Operations 14

Inner Joins 7 Union 14

Types of Outer Joins 8 Except 17

Left Outer Join 9 Intercept 18

Right Outer Join 10 Merge 19

Full Outer Join 11 Conclusion 21

Cross Join 12

2

Use of multiple primary tables is covered in this presentation. The use of tables as sub-queries is saved for another discussion.

Page 3: Understanding Joins And Joint Table Operations

SETTING THE STAGEfor a discussion about joins.

3

A family and the relationships between them will be used to illustrate the different kinds of joins.

Page 4: Understanding Joins And Joint Table Operations

Bob [email protected]

Building Relationships

To understand joins we’ll consider how some families are related.

JackSarahVictoriaMichael

MichaelMaryTerryLori

MarkVictoriaSally

4

Page 5: Understanding Joins And Joint Table Operations

Bob [email protected]

Bringing them together From time to time families like to get together.

They might get together in various ways:

Michael might visit is parents

Michael’s family might visit his parents

Everyone might get together

Using joins in a database is about bringing together things in much the same way.

5

Page 6: Understanding Joins And Joint Table Operations

Bob [email protected]

Families in the database So we’ll use the families as an example.

6

Page 7: Understanding Joins And Joint Table Operations

Inner Joins An INNER JOIN between two

tables returns information from rows that match on some characteristic.

An INNER JOIN is the default join type so the word INNER can be omitted and the query will return .an INNER JOIN

Michael is the only person in Michael’s Family that is also found in Jack’s Family, so if we join on names only one row should be returned.

Both queries return a single line result set:

7

Page 8: Understanding Joins And Joint Table Operations

Bob [email protected]

Types of Outer Joins There are three types of OUTER JOINS

LEFT OUTER JOIN

RIGHT OUTER JOIN

FULL OUTER JOIN

The first table mentioned in the FROM clause is on the left, and every other table mention after that is further right.

Think of LEFT, RIGHT and FULL as inclusion requests.

Basically, each says include all of these even if there is no match.

If there are no matches in the other table, the values for those show as null.

The easiest way to understand this is to look at some examples.

8

Page 9: Understanding Joins And Joint Table Operations

LEFT OUTER JOINVictoria’s family is on vacation, so we’ll leave them out for now.

Jack’s Family is mentioned first, so it is the “left table.”

Notice that we excluded Victoria in the WHERE clause.

But now we get Jack and Sarah because the LEFT JOIN includes all the qualified rows from the left table.

There are not matching values for Jack and Sarah in MichaelsFamily, so these are left NULL.

This is Michael visiting his Mom & Dad!9

Page 10: Understanding Joins And Joint Table Operations

RIGHT OUTER JOINWe won’t need to exclude Victoria if Michael’s because she is not in Michael’s family.

Michael’s Family is mentioned second, so it is the “right table.”

The only match in JacksFamily is Michal, so he is the only one with values from Jack’s Family.

As in the LEFT JOIN, where the values don’t match the fields from JacksFamily are left NULL.

Michael stays home and they call his Mom & Dad.

Bob [email protected]

10

Page 11: Understanding Joins And Joint Table Operations

FULL OUTER JOINVictoria’s Family is still away. Because this as a full join,

excluding Victoria requires she be removed before joining the tables. This is done with an opening common table expression.

Everyone else from the two family’s is listed.

There are NULLs on one side or the other except where there is a match in both tables.

Michael’s family visits his Mom & Dad.

Bob [email protected]

11

Page 12: Understanding Joins And Joint Table Operations

Bob [email protected]

Cross Join – Introduction There is one additional join called a CROSS JOIN. This join has no equality matching, but instead joins

everyone row in one table to every row in a second table. The result set contains (the number of rows Table 1) X (the

number of rows in Table 2). CROSS JOINS can be useful, but a where condition to limit

returns is your friend.

CAUTION: A cross join between 2 tables having 1M rows each returns 1,000,000,000,000 rows (if you server does crash before it finishes)!!!!!

Two syntax statements work:

The second syntax without a where condition is a common error.

12

Page 13: Understanding Joins And Joint Table Operations

CROSS JOIN Even if Victoria is away, everyone in

both families is related. A cross join does not seek a match and

joins every row to every row in the other table.

Everyone is related to everyone else in this cross join between Michael’s Family and his Parent’s Family.!

Bob [email protected]

13

Page 14: Understanding Joins And Joint Table Operations

Union & Union All

Bob [email protected]

There are two variations on this, Union and Union All

The results for these are in a different orderUNION mixes the tables, sorting on the primary ID, then in table order.

UNION ALL keeps the table set together, and sorts by the primary ID within the table.

Notice that Michel and Victoria are listed twice, but that’s because of unique IDs and Family Roles.

Watch what happens if we only use the matched fields.14

Page 15: Understanding Joins And Joint Table Operations

Unions on just matched fields

Bob [email protected]

With just fields that match, UNION drops duplicates!

But, UNION ALL still keeps the duplicates.

Now UNION only lists Michael and Victoria only once, but UNION ALL still lists them twice.

15

Page 16: Understanding Joins And Joint Table Operations

Bob [email protected]

Important Union Requirement

The number of fields and data types must match or the union will fail.

16

Page 17: Understanding Joins And Joint Table Operations

ExceptTo show except, a UNION ALL is used to create a table of the results.

An EXCEPT will exclude identical rows from the 2nd table from a result.

Bob [email protected]

17

Page 18: Understanding Joins And Joint Table Operations

IntersectIntersect will identify records that are same in two tables.

Victoria, of course, is Jack’s daughter and listed in both.

Note the same rule about fields and data types applies, so:

Finds no matches because the ID and FamilyRole do

not match!

Bob [email protected]

18

Page 19: Understanding Joins And Joint Table Operations

Merge – Page 1 of 2 MERGE can solve the duplication

problem Michael is putting together a list of

people for the picnic. He started with is parent’s family. Unfortunately, he got his sister’s

birthday wrong! He added his family using MERGE.

Bob [email protected]

Target TableTable providing new family members

Check for people already listed

Fix info if incorrect

Add anyone new

Michael’s Family Role is updated. Of course, Vicki’s birthday is still wrong,

and she is listed as a child.19

Page 20: Understanding Joins And Joint Table Operations

Merge – Page 2 of 2

Bob [email protected]

Target TableTable providing new family members

Check for people already listed

Fix info if incorrect

Add anyone new

The second merge sees and fixes Vicki’s role and Birthday.

Notice that a MERGE script MUST end with a semicolon!

This is one of a few places where a semicolon is required!

20

Page 21: Understanding Joins And Joint Table Operations

Bob [email protected]

Conclusion I hope you’ve found the presentation to be helpful.

Questions and comments are welcome. Please direct them to me at [email protected].

Check my LinkedIn page for additional presentations on query basics or more advanced topics.

21