An Inner Join as Matching Row Pairs

An inner join returns every pair satisfying its join condition. The output multiplicity depends on how many matching rows exist.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace named items
  2. Check duplicates
  3. Count all matching combinations instead of unique keys
  4. Establish the key before making a count assertion

Trace named items

Two orders with customer ID 4 can each pair with the one customer row having that ID. An order with an unmatched customer yields no pair. Foreign keys can restrict whether such an order is permitted in storage.

Check duplicates

If a query pairs two eligible rows on each side for the same key, that key can contribute four rows. Join cardinality generalizes the count.[1]

Count all matching combinations instead of unique keys

In group east, two orders and three events can match on region alone. The join creates six pairs if every order matches every event. Adding an order ID criterion may narrow the result to the intended associations. See in-memory joins.

Left: (east,L1), (east,L2), (west,L3)
Right: (east,R1), (east,R2), (east,R3)
Region equality → six rows with east, none with west

Establish the key before making a count assertion

Joining on nonunique human labels can multiply rows unexpectedly. Test a duplicate on both sides and an unmatched item. Identify whether output represents a pair, a left entity or an aggregated group.

References

  1. PostgreSQL Global Development Group, Queries ↩