SQL-Engineering-Handbook

07 — Real-World Case Study: Retail Merger Data Consolidation

Introduction

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.

Concept Overview

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:

  1. A single, unified sales report across both companies’ regions
  2. Proof that the customer database migration from UrbanCart’s legacy CRM into GlobalMart’s CRM was complete and lossless
  3. A cleaned, deduplicated combined loyalty list for the first joint marketing campaign

Every one of these is a set-operator problem, and this file solves all three end to end.

Why This Exists

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.

Business Context

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.

Real Company Examples

Production Use Cases

Visual Explanation

Capstone merger consolidation combining EXCEPT and UNION ALL

 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

Syntax

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.

Detailed Explanation

The case study is deliberately sequenced to mirror a real project timeline:

  1. Integrate first (Topic 04 pattern) — leadership wants a combined sales view immediately, before any cleanup work is done, because a rough combined picture today is more valuable than a perfect one next month.
  2. Reconcile second (Topics 03 and 05 patterns) — once the combined view exists, the migration of UrbanCart’s customers into GlobalMart’s CRM must be formally verified, in both directions, before UrbanCart’s legacy system is decommissioned.
  3. Deduplicate last (Topic 02 pattern) — the joint loyalty list is only built once the reconciliation confirms which customers are genuinely distinct versus already-migrated duplicates.

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.

Business Examples

-- 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;

Production Workflow

  1. Confirm schema compatibility between UrbanCart and GlobalMart tables (column count, types) before writing any combining query — see Topic 01.
  2. Build the unified sales report with UNION ALL and a source_company discriminator — see Topic 04.
  3. Run the bidirectional EXCEPT migration audit on customer_id — see Topics 03 and 05.
  4. Route any non-empty EXCEPT result to a manual investigation queue before proceeding.
  5. Once the audit passes, build the deduplicated joint loyalty list with UNION — see Topic 02.
  6. Re-express the costliest queries as EXISTS/NOT EXISTS and compare — see Topic 06 — before scheduling any of this as a recurring job.

Engineering Considerations

Performance Notes

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.

Database Compatibility

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)

Best Practices

Common Mistakes

Edge Cases

Interview Questions

  1. (Foundational) In this case study, why is UNION ALL correct for the sales integration but UNION correct for the loyalty list?
  2. (Intermediate) Why does the workflow reconcile customers before building the joint loyalty list, rather than after?
  3. (Advanced) What limitation does an exact-match EXCEPT/INTERSECT audit have when the same real-world customer has two different, unrelated ID values in each system? How would you address it?
  4. (Staff-level) Design the recurring automated check that should run for as long as UrbanCart’s legacy CRM remains writable during the transition period, and describe what should happen when it fails.

Summary

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.

Practice Problems

  1. Extend Step 1’s unified sales query to include a product_category breakdown, assuming both sales_globalmart and sales_urbancart gain a product_category column.
  2. Write the 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.
  3. Propose, in a comment, a fuzzy-matching approach (outside pure set operators) for the same-person/different-ID edge case described above.

Further Reading


← Performance and Optimization · Back to README