skip to content

After grouping orders by both region and payment method, what identifies each row of the collapsed result, and how many rows are there?

level: juniorimportance: must knowfreq 62%

answer

  1. identity, not just one value
  2. both keys must agree
  3. pairs that actually occurred
  4. product of distinct counts is a ceiling

basics

~20 s

Each result row is identified by a pair, one value from each grouping key, and the result holds exactly one row per pair that actually occurs in the data, not one row per pair the two columns could form.

solid answer

~40 s

Grouping on two keys makes the group identity a pair rather than a single value: two records belong together only when they agree on both. A collapse to one row per group therefore returns one row for every pair of values that actually appears together in the data. That count is bounded above by the product of the two columns' distinct-value counts, but in real data it usually sits well below the product, because most pairs never co-occur. Whether the pair arrives as one row label made of two parts or as two ordinary columns side by side depends on the tool's data model; the identities and the aggregated numbers are the same either way.

go deeper

for a junior

Recall that two keys make the group identity a pair, and that only records agreeing on both belong together. One row comes back per pair that occurs in the data.

for a middle

Explain why the row count sits below the product of the two columns' distinct-value counts, and why adding a key changes the result's shape and labels rather than the arithmetic inside each group.

for a senior

Predict the row count before running and use it as a check. A result far larger than the ceiling you computed means a key column is dirtier than it looks, and unnormalised values multiply through the pair count fast.

for a principal

Agree the grain of shared reports up front. Two dashboards keyed on different numbers of columns will disagree for a reason nobody can see in either query, and the fix is a written convention rather than a reconciliation.

## Group identity stops being a value and becomes a pair The grouped operation is one pass that gives every row a key, runs a computation once per key, and reassembles the answers into a result. It treats the **grouping key** - the column, columns or derived expression whose value decides which rows belong together - as a single thing, however many columns went into it. Name one column and a group's identity is one value. Name two and the identity is a **pair**, and two records land in the same group only when they agree on **both**. That is the whole of the conceptual change. The split still records which rows belong to which key, the per-key computation still runs once per key, and collapsing to one row per group still returns exactly as many rows as there are groups. What moves is how fine the groups are, what labels the result carries, and therefore how many rows come back. ## The row count has a ceiling and a reality Take a table of orders whose region column holds six distinct values and whose payment method column holds four. Grouped on the pair and collapsed, the result holds one row for each pair that **actually occurs together in the data**: | quantity | example value | where it comes from | |---|---|---| | distinct values of the first key | 6 | the data in that column | | distinct values of the second key | 4 | the data in that column | | pairs the two columns could form | 24 | the product of the two counts | | pairs some record actually carries | 11 | counted from the data, never from the columns | | rows in the collapsed result | 11 | one per pair that occurred | The product is a **ceiling**, not a prediction. Real key columns are rarely independent of one another: a payment method may only be offered in some regions, a product line may only exist in some markets. The gap between 24 and 11 is the ordinary case, and it widens fast as keys are added - three keys of 20, 50 and 12 distinct values have a ceiling of 12,000 and may well produce a few hundred rows. ## Predicting the result before you run it This is the habit an interviewer is probing, because it is the cheapest correctness check available: 1. **Name the identity out loud.** Say what one output row stands for: "one region-and-payment-method combination", not "one region". 2. **Compute the ceiling.** Multiply the distinct-value counts of the keys. A result above that number is impossible, and means a key is not the column you think it is. 3. **Expect to land well under it.** A count that lands exactly on the ceiling means the data is dense. That is possible, and it is worth confirming rather than assuming. 4. **Compare against the single-key run.** Grouping on the first key alone gives 6 rows; adding a second key can only refine those 6 into more rows, never into fewer. A count far above the ceiling you computed is the classic signal that a key column is dirtier than you believed. Trailing spaces, mixed casing and two spellings of one value each count as a distinct value, and each of those multiplies through the pair count. ## What the numbers do, and what they do not do - Each output number is still **the same reduction over the records sharing that identity**. Adding a key does not change the arithmetic of the reduction; it changes which records are in the pot. - For a reduction that folds through a group - a sum, a count, a minimum - the total across the whole result is unchanged by adding a key, because every record still belongs to exactly one group. Six regional sums and eleven region-and-method sums add up to the same grand figure. - For a reduction that does not fold that way, such as a mean, the finer result **cannot** be collapsed back by averaging its rows. A mean of means equals the overall mean only when the groups are the same size, which is exactly what grouping on a second key stops being true. - Nothing about a second key makes a record count twice. A record carries exactly one pair of key values and contributes to exactly one group. ## Where the pair ends up The pair has to appear in the result somehow, and here the family genuinely differs. Designs that carry a row-label slot compose the two values into one label made of two parts, one part per key, and put the aggregated columns beside it. Designs with no row-label concept return the two keys as two more ordinary output columns. Both carry the same identity and the same numbers; only the second form can be addressed by column name in a later step, which is worth establishing about your tool before you write that step.

  • The two key columns hold 20 and 50 distinct values. What can you say about the collapsed result's row count?
    At most 1,000, the product of the two distinct-value counts, and in practice usually far fewer, because only pairs some record actually carries produce a row. The product is a ceiling rather than a prediction; the real count is the number of distinct pairs present in the data, and a count above 1,000 means a key column holds values you did not expect.
  • Does adding a second grouping key change any aggregated value?
    It changes which records are folded together, so the individual numbers differ, but the arithmetic does not change: each number is still the same reduction over the records sharing that identity. For a reduction that folds, such as a sum or a count, the total across the whole result is the same as before. For a mean it is not recoverable by averaging the finer rows, because the groups differ in size.
  • The collapsed result has more rows than the product of the two columns' distinct-value counts. What happened?
    That is arithmetically impossible for the columns as you counted them, so one of the two counts is wrong. The usual cause is that the key column holds more distinct values than it appears to: whitespace, casing or two spellings of the same thing. Count the distinct values again on the exact column the grouped operation used, not on a cleaned-up version of it.

saying these in an interview costs you the question

  • Says the result has one row per distinct value of the first key
  • Expects every pair the two columns could form to appear as a row
  • Thinks a second grouping key changes the arithmetic of the reduction
  • Cannot say what makes two records belong to the same group
  • Assumes one layout for the keys without knowing which model the tool uses
  • Believes a record can contribute to more than one group