UPI Study › Courses › Database Programming › Free lessons › Lesson 8.3 Free with a free account
Full Outer Joins: Capturing All Records
Database Programming · Chapter 8: Joining Tables and Combining DataSection 8.3 · lesson 44 of 768 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Cross Joins: Generating Every Combination →← Left and Right Joins: When and How They DifferNext lesson: Cross Joins: Generating Every Combination →
A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →
In this lesson
Keep matched and unmatched rows from both tables in one result.
- Explain how a FULL OUTER JOIN combines matched pairs with left-only and right-only rows
- Compare inner, left, right and full join row counts on the same inputs
- Emulate a full outer join in MySQL with LEFT JOIN, RIGHT JOIN and UNION or UNION ALL
- Build a mismatch report by keeping rows where either key is NULL
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (929 words). Read along, or search it.
Opening Problem
Catalog and Stock Count Disagree
Amina Diallo manages the database at Lantern Books, our fictional bookstore. The catalog and the latest stock-count sheet disagree about which products exist. An inner join would show their agreements. Amina needs the disagreements too: catalog items never counted, and counted identifiers nobody recognizes. How can one report preserve evidence from both lists? A full outer join assembles matched pairs and the unmatched rows from either side.
Sample Data
A Stock Sheet With an Unknown Item
Restore the canonical bookstore, then add a disposable stock-count list. Three identifiers refer to catalog products; one deliberately refers to an unknown item. The sheet records Database Basics, Biology Guide, and Notebook. Its unknown identifier is ninety-nine, and its quantity is two. The count sheet has its own primary key, but no catalog foreign key. That choice lets a reconciliation retain an unexpected identifier for investigation. Run the setup. These are invented observations, and a missing count is not the same as a count of zero. Our SQLite version can execute full outer join directly. SQL Server also has native syntax; MySQL requires a combination of supported operations.
Full Outer Join
Matches Plus Unmatched Rows From Both Sides
Read the two key columns separately. A matched pair has both identifiers, while an unmatched row has nulls for the input that supplied no partner. Full outer join preserves unmatched rows on both sides. Outer is optional, so full join expresses the same operation here and in SQL Server. The equality condition does not match null to null. In this example, both declared product identifiers are non-null primary keys. Run the complete reconciliation, sorted by the first available identifier. Selecting both keys makes each row’s origin inspectable. Nine rows appear: three matches, five catalog-only rows, and one count-only row. The unknown item retains its count identifier even though catalog identifier is null. Keeping all rows does not mean matching every pair. Only the three shared identifiers form pairs; the remaining identifiers each contribute an unmatched row.
Join Comparison
Inner, Left, Right and Full Counts
Compare the four join choices against the same inputs. The catalog has eight rows, and the count sheet has four, with three shared identifiers. Inner keeps three matched pairs. Left adds five catalog-only rows; right instead adds the single count-only row. Full keeps all three groups. Repeated matching keys can produce several combinations within the matched region. Run the count comparison. Its using clauses are supported by SQLite; SQL Server versions would spell the same equality with on. The counts are three, eight, four, and nine. These values belong to this unique-key example, rather than a universal formula for every full join.
MySQL Workaround
LEFT JOIN, UNION and RIGHT JOIN
MySQL can assemble this particular result with left join, union, and right join. Both halves must describe exactly the same matching relationship. Their select lists need equal column counts, corresponding meanings, and compatible types. Output names come from the first select, where item identifier receives its alias. Coalesce chooses the first non-null key. It labels a catalog-only row from the catalog key, and a count-only row from the count key. Execute the recipe in SQLite to inspect its real output. It uses operations also available in MySQL, without claiming to run a MySQL server. The same nine identifiers appear. Because this projection retains unique item keys, union safely removes the matched rows repeated by both halves. Union also removes other identical projected rows. If genuine duplicates matter, this recipe can lose information; dropping keys from the projection makes that danger easier to miss.
Safer Pattern
UNION ALL With Right-Only Rows
The general recipe uses union all, then limits the second half to right-only rows. That half contributes no matched pair already supplied by the first. Notice the null test on the catalog primary key. It recognizes the absence of a real catalog partner, rather than testing an ordinary nullable description. Union all preserves multiplicity. Matched combinations appear once from the first half, while genuine repeated result rows are not globally deduplicated. Run this safer pattern. It works with supported left joins, making it useful when a system lacks native full join. Again there are nine rows. The order differs because this file sorts its separate key columns; the matching and preservation rules remain the same.
Reconciliation
Keeping Rows Where Either Key Is Null
Turn reconciliation into an action list by retaining rows where either primary key is null. The matches leave, and the gaps on both sides remain. A catalog-only row needs counting. A count-only row needs investigation; automatically adding it to the catalog would assume the sheet is correct. A filter demanding a positive counted quantity would discard catalog-only rows. The missing quantity is null, so it does not pass that value comparison. Run the mismatch query. Always examine a filter’s effect on padded rows before adding it to a full-join report. Six identifiers remain. The same approach compares payments with invoices, or records exported from two systems, whenever omissions on either side matter.
Quick Check
Sent and Received Shipment Gaps
A new dispatch audit compares sent identifiers twenty-one and twenty-two, with received identifiers twenty-two and twenty-three. The displayed program keeps only unmatched rows. How many discrepancies will it report? Pause, and decide.
Answer
Two Discrepancies, One in Each Direction
Two unmatched shipments. Twenty-one exists only in sent, while twenty-three exists only in received. The program returns two. Twenty-two has a partner, so it fails the mismatch filter and does not belong in this work list. One would account for only one direction of missing evidence. Three would count the agreement too, answering a different reconciliation question.
Key Takeaways
Preserve Evidence From Both Lists
Amina’s reconciliation preserves evidence from both lists. Full joins include matched combinations, left-only rows, and right-only rows. Merge non-null keys with coalesce when one label is useful. Keep null tests when the purpose is to find gaps. Choose a dialect-supported implementation, and protect duplicate multiplicity. A successful query still needs an audit of its projection and filters.
About Database Programming at UPI Study
Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Full Outer Joins: Capturing All Records
What does a FULL OUTER JOIN return?
How do you do a full outer join in MySQL?
How do you find rows that exist in only one of two tables?
What does COALESCE do in a full outer join?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Database Programming.
76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →