skip to content

Adding Rows Against Adding Columns

Adding records and adding attributes are different operations with different failures. A forgiving append turns one mistyped column name into two half-empty columns, in silence.

on this pageshow

questions

4

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%

answer

  1. two operations, not one setting
  2. longer against wider
  3. records add rows, attributes add columns
  4. one needs columns to agree, one needs rows to
  5. predict both counts before running

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.

solid answer

~50 s

They are two operations, not one operation with a direction. **Stacking pieces** puts two pieces that mean the same thing one under the other: the result is longer, the columns are supposed to be the same set, and the expected row count is the sum of the pieces. **Gluing side by side** puts two tables next to each other so each output row is one row of each: the result is wider, and what has to agree is the correspondence between the rows. The failures are different too. A stack goes wrong when a column is present or spelled differently on one piece. A glue goes wrong when the two sides are not in the same order, so values land beside the wrong records. Both can return a plausible table with no error at all.

go deeper

for a junior

Know the two shapes and say which one you mean: stacking makes the table longer by adding records, gluing side by side makes it wider by adding attributes. Then say the expected row count out loud before you run it.

for a middle

Explain what each operation assumes. A stack needs the two pieces to agree on column names; a glue needs the rows to correspond. Neither looks at a record identifier, which is why matching on a key column is the safer third option.

for a senior

Show that you know the behaviours vary across tools - union and fill, refuse, or reconcile by position for a stack; positional or label-based pairing for a glue - and that you check the combined counts at the seam rather than downstream.

for a principal

The angle worth arguing is where the correspondence lives. A pipeline that glues blocks side by side encodes its row correspondence in the order of earlier steps; one that matches on declared keys encodes it in the data, and survives a refactor of those steps.

## Two operations, not one setting A **labelled table** here means a rectangle of named columns that, in some tools, also carries an identifier for each row. There are two entirely different ways to combine one with another, and confusing them is the most common thing an interviewer is testing at this level. **Stacking pieces** means putting two pieces that mean the same thing one under the other, so the result is longer. You are **adding records**. Twelve monthly pieces of one report are twelve batches of the same kind of fact, and what makes them stackable is that they share a column vocabulary. **Gluing side by side** means putting two tables next to each other so that each output row is one row of each, and the result is wider. You are **adding attributes**. A block of computed scores placed beside the customers they were computed for adds columns, not rows. Many tools expose both through a single call with a setting that says whether records or attributes are being added, and that packaging is exactly why people think of them as one operation with a knob on it. They are not. They have different preconditions, different expected output counts, and different silent failures. ## What each one assumes 1. **A stack assumes a shared column vocabulary.** It does not assume anything about the rows: two pieces of different lengths stack happily, and that is the point. What it needs is that a column meaning the same thing is called the same thing on both pieces. 2. **A glue assumes a row correspondence.** It does not assume anything about the columns: they can be completely different, and that is the point. What it needs is that row three of one side really is the same record as row three of the other, or that whatever the tool pairs on is the record's identity. 3. **Neither operation compares records.** Nothing in either call looks at a customer number and checks it against a customer number. That is a third operation - matching two tables on a key - and it is the thing you actually want whenever you are unsure. ## The arithmetic to state before you run it Say the expected numbers out loud first; a candidate who does this catches almost everything in this category. | | Stacking pieces (adding records) | Gluing side by side (adding attributes) | |---|---|---| | Result is | longer | wider | | What must agree | the column set | the row correspondence | | Expected row count | the sum of the pieces' row counts | the shared row count, where the pairing is positional | | Expected column count | the shared set, or the union of the two sets | the sum of the two column counts | | Silent failure | a column named differently on one piece becomes two partly filled columns | values land beside the wrong records | | Cheapest check | compare the two pieces' column names as sets | spot-check an identifying value on both sides of a few rows | ## Where the designs genuinely differ This is a family of tools across several ecosystems, and they do not agree here, so answer for the class rather than for the one you use: - **A stack whose pieces disagree on columns** may take the union of the two column sets and fill the gaps with the tool's representation of a value that is not there, may refuse the call until the sets match, or may reconcile the pieces by position rather than by name. - **A side-by-side glue** may be a positional pairing that only checks that the two lengths agree, or, in tools that carry an identifier for each row, may line the two row-label sets up first and insert holes wherever a label appears on only one side - which means the result can have more rows than either input did. - **What a stack does to row identity** varies too: where rows have positions only, the result is simply longer; where each row carries a label, both pieces bring their labels along and the same label can now appear twice. So "it will just work" is not an answer for either operation. "Here is what mine does, and here is what I checked" is. ## What to do instead, when you can If both sides carry a column that identifies the record - an order number, a customer number, a date plus a store - then matching the two tables on that column is strictly better than gluing them side by side, because the pairing is stated in the code rather than inherited from whatever the previous step happened to leave behind. Gluing side by side is for the case where you genuinely built the second block from the first, in one pass, and nothing has re-ordered or filtered either since. For a stack, the equivalent discipline is cheap: compare the two pieces' column names as sets before combining, and compare the combined row count against the sum you predicted. Both take one line and both catch the failure at the seam instead of three steps downstream, where it looks like a data problem rather than a combining problem.

  • Two pieces of 400 and 600 rows are stacked. What should the result's row count and column count be?
    1,000 rows. The column count is the shared set if the pieces carry the same columns; if they do not, it is either the union of the two sets, with the extra columns filled only for the rows that had them, or the call is refused, depending on the tool. Predicting both numbers before running is what makes a wrong one visible.
  • Why is matching on a key column usually a better answer than gluing two tables side by side?
    Because the pairing becomes something you wrote down rather than something you inherited. Matching two tables on a key compares an identifying value on each side, so a row that has no partner is visible as an unpartnered row rather than as a plausible wrong value. A side-by-side glue pairs on position or on row labels and can be defeated by any earlier sort or filter.

Adding records is filing more forms in the same cabinet - every form has the same boxes on it. Adding attributes is adding a new box to every form already filed, which only works if you can tell which form is which.

saying these in an interview costs you the question

  • Treats adding records and adding attributes as one operation with a direction.
  • Assumes a stack verifies that both pieces carry the same columns.
  • Assumes a side-by-side glue can never change the row count.
  • Expects a spelling mismatch between two pieces to raise an error.
  • Never predicts the output row count before combining anything.
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

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