database management - using full joins in sql

14
FULL JOIN Brought to you by… The Four Marketeers

Upload: marisamendezbrady

Post on 12-May-2015

250 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Database Management - Using Full Joins in SQL

FULL JOINBrought to you by…

The Four Marketeers

Page 2: Database Management - Using Full Joins in SQL

Why use full join?Glad you asked.

The Full Join function combines records from two tables into one display, automatically inserting “null” values for missing matches on either side

Full Join can be used to combine all columns from two tables into one comprehensive table, OR you can choose specific columns from each table to combine information

Page 3: Database Management - Using Full Joins in SQL

How do you Full Join?First, you select the column from the table you would like to join with another TABLE (use the * function if you want all of the columns to display)

Once you do a SELECT statement clarifying this, you FULL JOIN with the target table

Then, you select which common column, or field, you would like to initiate the full join with. Ta-Da! Your full join is complete.

Let’s look at some examples!

Page 4: Database Management - Using Full Joins in SQL

Syntax

SELECT * FROM table1 -> FULL JOIN table2 -> USING (common column);

Page 5: Database Management - Using Full Joins in SQL

This join is between the

tables “books” and “borrowers”

using the common column

“book_id”

Page 6: Database Management - Using Full Joins in SQL

Joined Table DisplayIn this example, using the common column “book_id”, we created a FULL JOIN between books and borrower tables. All the columns in both tables now display together.

Page 7: Database Management - Using Full Joins in SQL

This join is between “patrons” and “borrowers”

using the common column “patron_id”

Page 8: Database Management - Using Full Joins in SQL

A Closer Look

Page 9: Database Management - Using Full Joins in SQL

NULL Values

Where there is no overlapping information, a NULL will appear in the row; thus, you can easily identify where there is nothing populated in a field

Page 10: Database Management - Using Full Joins in SQL

When is using Full Joins appropriate?

Examples using our in class library:● To see if a title has circulated recently● To see what patrons have borrowed ● To see if any items have not been returned

The possibilities are endless...

Page 11: Database Management - Using Full Joins in SQL

Other Ways to use FULL JOIN● In some situations you might want to merge information

contained in one table with information in another table. This would display the same information, but differently.

● For instance, to display the descriptive information for an ID in a junction table, you can create a FULL JOIN between the column containing the text value of an ID in a table that otherwise wouldn’t contain it.

This is hard to explain with words. Lets look at the next slide and revisit this

information on the other end.

Page 12: Database Management - Using Full Joins in SQL
Page 13: Database Management - Using Full Joins in SQL

So….● If you want to see the titles of books in

the borrower table, rather than the book ID, you can do a FULL JOIN to display this information.

● This would work for any field where an ID is displayed. You can basically use the FULL JOIN to connect any information contained in one column in one table, to information contained in a separate table.

● To summarize, a FULL JOIN combines all of the effects of applying left and right joins

Page 14: Database Management - Using Full Joins in SQL

Later Nerds