skip to content

Putting Two Tables Together

Matching two tables on a key or on their labels, what each shape of match keeps, and which of two identical rows survives. The count that comes back is the number people forget to look at.

on this pageshow

questions

25

Two columns of 1,000 values each, taken from different tables, are added together — what decides which value pairs with which?

level: juniorimportance: must knowfreq 58%

answer

  1. the line does not choose
  2. property of the tool, not the code
  3. labels first, position only without them
  4. an unmatched label leaves a hole

basics

~10 s

The tool decides, not the expression. Designs that carry a per-row identifier pair the two operands by that identifier first; designs that carry none pair strictly by position and refuse operands of different lengths.

solid answer

~50 s

Nothing in the line you wrote chooses the pairing rule — the tool holding the data does. Some designs attach **row labels** (a per-row identifier carried beside the columns) and pair the two operands by label before computing anything, so the value at position 3 on one side may be added to the value at position 900 on the other, and a label present on only one side comes back as a cell holding the absent-value marker, the tool's representation of a value that is not there. Other designs — a bare rectangle of numbers addressed by position only — have no labels at all, pair strictly by position, and refuse two operands of different lengths. The same expression therefore means two different things. Before trusting the result, say what makes a row on one side correspond to a row on the other, and make the operation pair on that.

go deeper

for a junior

Recall that two columns can be paired in two different ways — by a per-row identifier or by position — and that which one happens depends on what the data is held in, not on how you wrote the line.

for a middle

Explain what a per-row identifier does to an arithmetic operation: the pairing happens before the arithmetic, a label on one side only leaves a hole, and identifiers that look like counting numbers stop matching positions the moment one side is filtered.

for a senior

Show that you would state the intended correspondence before writing the expression, force it explicitly, and check the output row count against a number you predicted rather than reading the first few rows on screen.

for a principal

Weigh a codebase where every combination inherits an implicit pairing default against one where the correspondence is declared at every seam, and be able to say which of the two failures you would rather your team debug under time pressure.

## The question underneath the expression Two columns are combined with an ordinary arithmetic expression. Before a single addition happens, the tool has to decide **which value on one side is paired with which value on the other**. That decision is made by the library, not by the line you wrote, and the two rules in common use produce different results from identical source text. Four terms, defined once: - **Row labels** — the per-row identifier a tool carries beside the columns. Present in some designs, absent entirely in others. - **Labelled table** — a rectangle of named columns that, in some tools, also carries one of those identifiers for each row. - **Bare numeric array** — a rectangle of numbers addressed by position only, carrying no labels at all. - **Label pairing** — the tool silently lining two operands up by their row labels before it computes anything. Its opposite is **positional pairing**: the first value with the first, the second with the second. ## The two rules, side by side | What the operands carry | How they are paired | What a mismatch does | |---|---|---| | Row labels on both sides | By label, before any arithmetic runs | A label on one side only yields a cell holding the **absent-value marker** — the tool's representation of a value that is not there | | No label concept at all | Strictly by position | Two operands of different lengths are refused at the call | | Labels that happen to be consecutive counting numbers | Still by label — the numbers are identities, not positions | Two sides counted from different starting points overlap only partly | The third row is the one that fools people. A label set reading 0, 1, 2, 3 looks exactly like a set of positions, and for as long as both sides were built the same way it behaves like one. Filter one side, and the rows that survive keep the identifiers they already had rather than closing up; from then on the two sides agree by label only where the filter happened to keep a row. ## Why this is a property of the tool - The expression is identical in both worlds. Nothing in it says *pair by label* or *pair by position*. - Whether the operands carry labels at all is a design decision taken by whoever wrote the library, and the designs in wide use genuinely differ on it. - In one process you can hold both kinds at once: a labelled table beside a bare rectangle of numbers. The same arithmetic between two of the first behaves differently from the same arithmetic between two of the second. - Nothing raises. Label pairing that finds few common labels returns a full-length result of absent values; positional pairing over equal-length operands that do not actually correspond returns plausible wrong numbers. ## The three failure shapes 1. **Silent reordering.** Both sides carry the same labels in different orders. Positional pairing would be wrong here and label pairing is right — which is why someone who assumed position is surprised by a *correct* answer and learns the wrong lesson from it. 2. **Holes.** The two label sets overlap only partly, so rows whose label exists on one side only come back carrying the absent-value marker. 3. **Growth.** A label occurring more than once on both sides is paired with each of its partners in turn, so the result is longer than either input. ## Making the correspondence something you wrote down The durable habit is to stop relying on the default and state what the correspondence is: 1. Say out loud what makes row 400 on one side the same thing as some row on the other. If the honest answer is *they came out of the same ordered step over the same rows*, the correspondence is position. If it is *they are the same account*, the correspondence is a value. 2. When it is position, **drop to positions** deliberately — discard the labels so the operation pairs strictly by position — and assert that the two lengths are equal in the same breath. 3. When it is a value, put that value in a column on both sides and match the two tables on it. The correspondence is then written in the code and survives anyone filtering or reordering either side later. 4. Either way, compare the output row count against a number you predicted before you look at any value. ## What varies, and how to say it There is no single true sentence about pairing for this whole family of tools. Some attach an identifier to every row and treat it as the identity of that row, so any operation between two operands pairs by label first and the unmatched labels become holes. Others have no label concept, pair strictly by position, and treat a length mismatch as an error. A candidate who says flatly *it pairs by label* is describing one design as though it were the class. The answer that travels across ecosystems is: it depends what the operands carry, here is how each behaves, and here is how I would make it explicit rather than inherit a default.

  • If both operands carry the same labels in the same order, does label pairing give the same answer as positional pairing?
    Usually yes: identical label sets in identical order pair the same way under either rule. Two caveats matter. The equality is a property of the data at run time rather than of the code, so a filter or a reorder upstream breaks it without touching your line. And if a label occurs more than once on both sides, label pairing multiplies the rows even when the two sides look identical.
  • How do you make the correspondence explicit instead of relying on the default?
    Two routes. If position genuinely is the correspondence, discard the labels so the operation pairs strictly by position, and assert the two lengths are equal in the same step. If the correspondence is a value both sides carry, put it in a column on each side and match the two tables on it — that keeps working after either side is filtered or reordered.

Two decks of numbered cards. Pairing by label is dealing so that card 7 always meets card 7 wherever it sits in each deck, and a number present in only one deck comes back with no partner. Pairing by position is dealing off both tops at once, which is right only if someone stacked the two decks in the same order and can say who.

saying these in an interview costs you the question

  • Says element-wise arithmetic always pairs the two operands by position.
  • Says every tabular design pairs on row labels before computing.
  • Assumes equal lengths guarantee that the two sides correspond.
  • Expects a wrong pairing to raise rather than return plausible values.
  • Treats row labels as display decoration with no effect on results.
open as a page

A match of two tables on their key columns returns zero rows, though the same codes print on both sides. Why?

level: juniorimportance: must knowfreq 70%

basics

~20 s

Equality compares stored values, not the rendering. Two codes that print alike can differ by letter case, leading or trailing spaces, an invisible character, a lost leading zero, or one side holding digits as text and the other as numbers.

open as a page

Matching 1,000 orders to a customer list on customer id returns 940 rows: which match shape was used, and what do the others keep?

level: juniorimportance: must knowfreq 80%

basics

~20 s

Losing 60 rows means the match kept only rows partnered on both sides. The three alternatives keep the orders table whole, the customer list whole, or both whole, and unpartnered rows then survive with the other side's columns holding the absent-value marker.

open as a page

Twelve monthly pieces are stacked into one longer table - how does that differ from gluing two tables side by side?

level: juniorimportance: must knowfreq 66%

basics

~20 s

Stacking adds records, so the result is longer and the hazard is two pieces whose column sets do not agree. Gluing side by side adds attributes, so the result is wider and the hazard is rows paired in an order nobody checked.

open as a page

A match of 1,000 order rows against a customer reference table returns 1,340 rows - how is that possible?

level: juniorimportance: must knowfreq 76%

basics

~20 s

A key match returns, for each key value, the first side's occurrence count multiplied by the second side's. If a customer identifier appears more than once in the reference table, the orders carrying it are copied and the total grows.

open as a page

Two rows are identical in every column; two others are identical only on the columns that identify one entity — why are those different defects?

level: juniorimportance: must knowfreq 68%

basics

~20 s

Rows identical in every column carry nothing you would lose by collapsing them. Rows identical only on the identifying columns disagree somewhere else, so collapsing one discards a fact and forces a decision about which row is right.

open as a page

A key match returns far fewer rows than expected — what do you compute on the two key sets to find the cause?

level: middleimportance: must knowfreq 62%

basics

~20 s

Compare the key value sets, not the rows: how many distinct values each side has, how many are shared, and how many each side holds that the other does not. Then inspect a sample of the unshared ones.

open as a page

In a match keeping the orders table whole, a row shows the absent-value marker in every reference-side column: what two causes explain it?

level: middleimportance: must knowfreq 62%

basics

~20 s

Either the order's key found no partner and the match manufactured those cells, or it did find a partner whose own values were already absent. The cells look identical, so distinguish them with a provenance marker or by testing the key against the other table's key values.

open as a page

You stacked two pieces and got two half-empty columns where you expected one - what happened, and why no error?

level: middleimportance: must knowfreq 58%

basics

~20 s

The column is named differently on the two pieces, so a stack that reconciles by column name treated them as two columns and took the union of the two column sets, filling each gap with the absent-value marker. Nothing was wrong from the tool's point of view, so nothing was raised.

open as a page

Before running a match of two tables on a key, how do you predict its output row count?

level: middleimportance: must knowfreq 61%

basics

~20 s

Sum, over every key value present on both sides, the product of its occurrence count on each side. In practice you first ask the cheap question: is either side unique on the key? If one is, the output cannot exceed the other side's rows.

open as a page

A de-duplication keeps one row from each set of repeats; on what ordering is that survivor defined?

level: middleimportance: must knowfreq 62%

basics

~20 s

On whatever order the rows happen to be sitting in when the step runs, which is a by-product of the previous operation rather than anything you stated. Put the rows in a deliberate order on a column that encodes precedence, or the survivor is not reproducible.

open as a page

In a tool that pairs operands by row label, one side carries label A twice and the other three times — what comes back?

level: middleimportance: should knowfreq 49%

basics

~20 s

Six rows come back for that label. Every occurrence on one side is paired with every occurrence on the other, so the output row count for a label is the product of its two occurrence counts, not the larger of them.

open as a page

Two key columns hold the same codes, one as text and one as numbers — will the match raise an error?

level: middleimportance: should knowfreq 55%

basics

~20 s

Do not count on it. Some designs refuse a cross-representation key comparison at the call, others compare, find nothing equal and hand back an empty or badly thinned result, and some convert one side - which can change the code itself.

open as a page

You want only the orders whose customer id appears in a flagged-accounts list, with no columns added: which operation, and why not an ordinary match?

level: middleimportance: should knowfreq 50%

basics

~20 s

Use the keep-if-partnered filter: a match used purely as a membership test, where rows are kept or dropped by whether a partner exists and no columns are added. An ordinary widening match adds the other table's columns and can change the row count.

open as a page

Before dropping repeated rows from a delivery, why would you mark or count them first?

level: middleimportance: should knowfreq 44%

basics

~20 s

Dropping is destructive and unmeasured: it produces a clean table and no evidence. Counting tells you whether this is a handful of stray copies or a uniformly doubled delivery, and marking keeps every row so a later step, or a person, can still decide.

open as a page

When is discarding the row labels to force positional pairing the right fix, and when does it hide a mismatch?

level: seniorimportance: should knowfreq 37%

basics

~20 s

Discarding labels is right only when position genuinely is the correspondence and you can name the step that guarantees it. Otherwise it converts a visible failure — holes, or a result longer than either input — into wrong values paired row by row.

open as a page

An element-wise subtraction between two tables of 50,000 rows each returns almost entirely absent values, though both hold the same records — why?

level: seniorimportance: should knowfreq 45%

basics

~20 s

The two sides were paired by row label and their label sets barely overlap. Equal row counts prove nothing: an upstream filter or rebuild left each side carrying identifiers the other does not have, so almost nothing found a partner.

open as a page

A nightly revenue report has quietly matched only 94% of transactions to accounts for months — why is that worse than a match that returned nothing at all?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Because it ships. An empty result stops everything and is fixed the same day; a 94% match returns a plausible table whose loss is systematic rather than random, so the totals are consistently wrong, consistently believable and consistently signed off.

open as a page

Both tables in a match carry a column named region, and the output carries two of them: what happened, and what should you have done first?

level: seniorimportance: should knowfreq 44%

basics

~20 s

A non-key column with the same name on both sides collided, and the tool kept both copies under decorated names rather than choosing between them. Decide before the match which side is authoritative, and drop or rename the other there — never ship a table carrying both.

open as a page

A column of scores is glued beside a 1,000-row customer table and the scores land on the wrong customers - why?

level: seniorimportance: should knowfreq 47%

basics

~20 s

Because gluing side by side never compares a customer to a customer. It pairs rows by position, or by the row labels the tool carries, and both were fixed by whatever earlier step produced each side. Once one side was sorted or filtered independently, the pairing was wrong and nothing could say so.

open as a page

Two pieces carry the same five column names in a different order and are stacked - what can land where, and how would you notice?

level: seniorimportance: should knowfreq 38%

basics

~20 s

It depends entirely on the reconciliation rule. Reconciled by name, column order is irrelevant and the result is correct. Reconciled by position, the second piece's values land under the wrong headings with no holes and no error. Establish which rule the operation follows before trusting it.

open as a page

A revenue total jumped after a step matched two tables, yet the distinct customer count is unchanged - which checks belong either side of that match?

level: seniorimportance: should knowfreq 57%

basics

~20 s

A sum that grew while a distinct count held steady means rows were copied, not added. Bracket the match: before it, assert the side that should be unique really is unique on the key; after it, compare the output row count and an unchanged total against what you predicted.

open as a page

A nightly de-duplication comparing every column reports nothing removed, yet each customer still appears twice — why?

level: seniorimportance: should knowfreq 51%

basics

~20 s

Nothing was identical on every column, so nothing was removed. The two rows for each customer differ on a column that has nothing to do with the customer — an ingestion timestamp, a batch identifier, a loader's sequence number — and comparing on all columns makes exactly those repeats invisible.

open as a page

When should the expected one-to-many relationship at a nightly match be a declared property that fails the run rather than a number someone reads?

level: principalimportance: should knowfreq 43%

basics

~20 s

Declare it when a wrong number is more expensive than a stopped run, and when the pairing the step depends on is stable enough that a violation really is a defect. Where it is expected to change, measure and publish the number instead of failing.

open as a page

Across a pipeline of a dozen matches, what standing rule would you set for the default match shape and rows with no partner?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

There is no single right rule, only three defensible defaults with different costs: drop unpartnered rows, keep the driving table whole and mark them, or stop the run. Whichever you pick, the disposition should be decided at each seam and written down, not inferred later from a number.

open as a page