Course contents 07 / 17

07 / 17 · Combining Rows

Outer joins

Preserve catalogue entries without sales, count true matches and compare filters in ON and WHERE.

Prerequisites

Inner joins

On this page 4

Mint is in the catalogue and has no sales. A report that starts with the inner join gives rows for tea, coffee and cocoa. Mint disappears before we count sales. To report the lack of activity, we need to preserve the catalogue row through the join.

What happens to an unmatched row?#

Use the catalogue and the six order_lines from the same example. Put the catalogue on the left because each product must appear in the report. A left outer join emits all matching pairs, then emits one extra row for each left row with no match. In that extra row every right-hand column is NULL. The word OUTER is optional in LEFT OUTER JOIN. (PostgreSQL 18, §7.2.1; DuckDB 1.4, Outer joins)

SELECT c.product, l.order_id, l.line_no, l.quantity
FROM catalogue AS c
LEFT JOIN order_lines AS l ON l.product = c.product
ORDER BY c.product, l.order_id, l.line_no;
productorder_idline_noquantity
cocoa10413
coffee10121
coffee10312
mintNULLNULLNULL
tea10112
tea10211
tea10321

Mint contributes one row even though it has no order line. The NULL values came from the join. The other products produce as many rows as they have matches. Preserving every catalogue row guarantees at least one result row per product, with additional rows for additional matches.

Count the matches#

The report needs one result per product. Group the rows and count an expression that is non-null on every genuine match. In the sample, l.order_id is present on every real order line and absent on the row added for mint. (PostgreSQL 18, §9.21)

SELECT c.product,
       COUNT(*) AS joined_rows,
       COUNT(l.order_id) AS sales_lines,
       COALESCE(SUM(l.quantity), 0) AS units
FROM catalogue AS c
LEFT JOIN order_lines AS l ON l.product = c.product
GROUP BY c.product
ORDER BY c.product;
productjoined_rowssales_linesunits
cocoa113
coffee223
mint100
tea334

COUNT(*) counts the added row for mint. COUNT(l.order_id) skips it. SUM(l.quantity) would return NULL for mint, so COALESCE makes the total zero. This interpretation is appropriate here because every real quantity is known. If genuine matches could have missing quantities, the same expression would also show zero for an all-missing group. The count of matches would then still carry different information from the sum.

Filter the pairs or the joined result#

Suppose the report should keep every catalogue item and count only lines with three units or more. Write that requirement as part of the match. For a tea row with two units, the code comparison succeeds and the quantity comparison fails. That row supplies no pair. None of the other tea lines qualifies, so the left join adds one row for tea.

SELECT c.product, COUNT(l.order_id) AS large_lines
FROM catalogue AS c
LEFT JOIN order_lines AS l
    ON l.product = c.product AND l.quantity >= 3
GROUP BY c.product
ORDER BY c.product;

The result is cocoa with 1, followed by coffee, mint and tea with 0. Move the quantity condition to WHERE and the result changes.

SELECT c.product, COUNT(l.order_id) AS large_lines
FROM catalogue AS c
LEFT JOIN order_lines AS l ON l.product = c.product
WHERE l.quantity >= 3
GROUP BY c.product
ORDER BY c.product;

The join now finds matches for tea and coffee, so it adds no extra rows for them. The subsequent WHERE discards every one of their rows because each quantity is below 3. It discards mint’s row because NULL >= 3 is unknown. Only (cocoa, 1) remains. Adding OR l.order_id IS NULL to that WHERE would keep mint, but tea and coffee would still disappear. Their rows matched on code, and those matches contain known order identifiers. (PostgreSQL 18, §7.2.1; SQLite, §2.3)

Figure

Filter placement changes which products appear

Filter placement changes which products appearIn the first result cocoa has one line and coffee, mint and tea have zero. In the second result coffee, mint and tea are absent.productONWHEREcocoa11coffee0No rowmint0No rowtea0No row
Putting quantity ≥ 3 in ON preserves four products. Putting it in WHERE leaves one row for cocoa.

The distinction is between the rule that creates pairs and the rule that selects joined rows. Apply a condition in ON when it decides which rows count as matches for a left row we still want to preserve. Use WHERE when failing the condition should remove the entire joined row.

Which side must be preserved?#

A right outer join preserves the right input. It is equivalent to a left join with the inputs swapped and the output columns arranged as needed. A full outer join adds an unmatched row from either side. For a reconciliation, suppose catalogue A has tea and coffee, while catalogue B has tea and cocoa. Joining by code gives a tea pair. A full join also gives coffee with missing B fields and cocoa with missing A fields. It still returns every matching pair if a code occurs more than once. (PostgreSQL 18, §7.2.1; DuckDB 1.4, Outer joins)

Next#

The result can tell us that mint has zero sales. If the question only asks which catalogue products have any sale, we can check for a matching row without producing the pairs first. Subqueries and existence use that property to express both presence and absence.

References#

  1. PostgreSQL Global Development Group. PostgreSQL 18 documentation, 2025. §7.2.1 defines outer joins and contrasts ON with WHERE. §9.21 specifies the forms of COUNT and SUM.
  2. SQLite Project. SELECT, §§2.1 and 2.3. Describes when outer joins add rows and when the resulting rows are filtered.
  3. DuckDB Foundation. FROM and JOIN Clauses, Outer Joins. DuckDB 1.4 documentation on preserved inputs and missing values in unmatched rows.