This is the capstone of Module 13. Every operator and pattern from Topics 01–06 gets applied together against one continuous business scenario, the way they actually show up in a single real project — not as isolated syntax demonstrations.
The scenario: GlobalMart, a mid-size retailer, has just acquired UrbanCart, a regional e-commerce competitor. The data engineering team has one quarter to deliver three things leadership is asking for directly:
Every one of these is a set-operator problem, and this file solves all three end to end.
Textbook examples teach one operator at a time. Production work never arrives that way — a single project typically needs integration reporting, reconciliation, and deduplication together, often against the same tables, often under a deadline. This case study is deliberately built to require moving between UNION ALL, INTERSECT, EXCEPT, and their EXISTS/NOT EXISTS equivalents in one coherent piece of work, so the decision-making — not just the syntax — gets exercised.
Mergers and acquisitions are one of the most common real-world triggers for heavy set-operator use: two companies, each with their own customers, sales history, and product catalogs, need one combined operational picture fast, and leadership needs proof — not a verbal assurance — that no customer or transaction was lost in the process.
EXCEPT pattern from Topic 05.UNION ALL and a discriminator column while the two billing systems run in parallel for a transition period, following the pattern from Topic 04.UNION/INTERSECT before a combined onboarding email campaign, following Topic 02’s and Topic 03’s patterns. UrbanCart CRM ──┐ ┌── GlobalMart CRM
│ │
▼ ▼
┌───────────────────────────────────────┐
│ customer_id EXCEPT (both directions) │ → migration gaps
└───────────────────────────────────────┘
│ │
▼ ▼
sales_urbancart sales_globalmart
│ │
└──────── UNION ALL ─────┘ → unified_sales (Topic 04 pattern)
loyalty_urbancart UNION loyalty_globalmart → deduplicated campaign list
No new syntax is introduced in this file — every statement below is a direct application of UNION, UNION ALL, INTERSECT, EXCEPT, and EXISTS/NOT EXISTS as taught in Topics 01–06.
The case study is deliberately sequenced to mirror a real project timeline:
This order is itself an engineering decision: reversing steps 2 and 3 risks building a marketing list on top of an unverified, possibly incomplete migration.
-- Step 1 preview: unified sales, tagged by originating company
SELECT order_id, order_total, order_date, 'GlobalMart' AS source_company
FROM sales_globalmart
UNION ALL
SELECT order_id, order_total, order_date, 'UrbanCart' AS source_company
FROM sales_urbancart;
-- Step 2 preview: migration gap check, one direction
SELECT customer_id FROM urbancart_customers
EXCEPT
SELECT customer_id FROM globalmart_customers;
UNION ALL and a source_company discriminator — see Topic 04.EXCEPT migration audit on customer_id — see Topics 03 and 05.EXCEPT result to a manual investigation queue before proceeding.UNION — see Topic 02.EXISTS/NOT EXISTS and compare — see Topic 06 — before scheduling any of this as a recurring job.sales_globalmart/sales_urbancart, urbancart_customers/globalmart_customers, loyalty_globalmart/loyalty_urbancart) rather than the running employees/departments schema from Topics 01–06, because a real merger scenario involves genuinely separate systems, not variations of one company’s internal data.source_company discriminator introduced in Step 1 is what allows leadership to ask “how is UrbanCart’s book of business performing since the acquisition?” without a schema redesign.At small scale, every form below performs indistinguishably. At the actual scale a merger reconciliation runs at — often hundreds of thousands to millions of customer records — the EXISTS/NOT EXISTS rewrites from Topic 06 are very likely to outperform the native EXCEPT/INTERSECT forms, provided customer_id is indexed on both sides. This file includes both forms for the migration audit specifically so the choice can be benchmarked on real data rather than assumed.
| Feature used | MySQL | PostgreSQL | SQL Server | Oracle |
|---|---|---|---|---|
UNION ALL integration (Step 1) |
✅ | ✅ | ✅ | ✅ |
EXCEPT migration audit (Step 2) |
8.0.31+ | ✅ | ✅ | use MINUS |
NOT EXISTS rewrite (Step 2 alt.) |
✅ | ✅ | ✅ | ✅ |
UNION deduplication (Step 3) |
✅ | ✅ | ✅ | ✅ |
EXCEPT audit against the target system returns zero rows.UNION) before the migration audit passes, risking a campaign sent against an incomplete or duplicated customer base.UNION ALL and Step 3’s UNION are solving different problems (integration vs. deduplication) and are not interchangeable.customer_id values, no shared key) will not be caught by an EXCEPT/INTERSECT audit on customer_id — that is a fuzzy-matching problem (name, email, address similarity), outside the scope of exact-match set operators, and worth flagging explicitly to stakeholders as a known limitation.UNION ALL correct for the sales integration but UNION correct for the loyalty list?EXCEPT/INTERSECT audit have when the same real-world customer has two different, unrelated ID values in each system? How would you address it?A real merger data-consolidation project needs every pattern from this module, applied in a deliberate sequence: integrate for an immediate combined view, reconcile to prove the migration is trustworthy, then deduplicate to produce a clean downstream asset. The SQL file accompanying this topic implements all three steps against a realistic two-company schema, including a performance-rewrite comparison for the reconciliation step.
product_category breakdown, assuming both sales_globalmart and sales_urbancart gain a product_category column.NOT EXISTS equivalent of Step 2’s migration audit for the reverse direction (GlobalMart customers missing from UrbanCart) and explain when that direction would legitimately return non-empty results.