Full Outer Joins: Capturing All Records, Explained

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

← 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 →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

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.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
A FULL OUTER JOIN returns matched pairs plus the unmatched rows from both tables, with NULL filling the columns of the missing side. If a catalog has 8 products, a stock sheet has 4 identifiers, and 3 identifiers are shared, the full join returns 9 rows: 3 matches, 5 catalog-only rows and 1 sheet-only row. SQL Server and SQLite 3.39 or later support it directly; MySQL does not.
How do you do a full outer join in MySQL?
MySQL has no FULL OUTER JOIN, so combine a LEFT JOIN with a RIGHT JOIN that keeps only right-side rows without a match, joined by UNION ALL. The second half adds a test such as WHERE c.product_id IS NULL on the left table's primary key, so matched pairs are not repeated. A plain LEFT JOIN UNION RIGHT JOIN also works when unique keys are selected, but UNION removes genuinely identical rows.
How do you find rows that exist in only one of two tables?
Use a FULL OUTER JOIN and keep rows where either side's key is NULL, as in WHERE c.product_id IS NULL OR s.product_id IS NULL. Matched pairs drop out, leaving rows missing from one side or the other. For sent IDs 21 and 22 and received IDs 22 and 23, the result is 21 and 23. Avoid extra value filters such as s.qty > 0, which also discard rows whose quantity is NULL.
What does COALESCE do in a full outer join?
COALESCE returns its first non-NULL argument, so COALESCE(c.product_id, s.product_id) gives every full join row one key label. A catalog-only row takes the catalog key, and a sheet-only row, whose catalog key is NULL, takes the sheet key. Use it for display labels, but keep both original key columns when hunting for gaps, because the NULL in one key shows which side is missing.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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

Database Programming
3 credits · NCCRS
Enroll →