Actuarial Notes

Table Join

Study guide: DISC-DA

A Table Join combines two tables into one by pairing each row of the first with the rows of the second whose join key matches, under a join condition written in SQL's ON clause. It is the step that brings policy, claim, vehicle and driver data stored in separate tables of a relational database into one data set.

The join type decides what happens to rows that find no match. With R the left table and S the right table:

Join type Rows kept
Inner join Matched pairs only
Left join Every row of R, with nulls in S's fields where there is no match
Right join Every row of S, with nulls in R's fields where there is no match
Full outer join Every row of both, filling the missing side with null

A right join is a left join with the tables swapped. A cross join has no condition and pairs every row with every row, giving \lvert R \rvert \times \lvert S \rvert rows, which is rarely wanted and is what a forgotten condition produces.

If n_R(k) and n_S(k) count the rows of R and S that carry key value k, the number of rows of the inner join is

I = \sum_{k} n_R(k)\, n_S(k)

Reading a left join as keeping the right table In A LEFT JOIN B, every row of A survives and B's fields may be null. Identify the left table from the order of the FROM clause before counting.

Row counts of outer joins

Each unmatched row appears exactly once in an outer join that keeps its table, with nulls in the other table's fields. The number of rows of a left join is

\lvert R \text{ LEFT JOIN } S \rvert = I + u_R

  • u_R is the number of rows of R with no match in S.

The number of rows of a right join is

\lvert R \text{ RIGHT JOIN } S \rvert = I + u_S

  • u_S is the number of rows of S with no match in R.

The number of rows of a full outer join is

\lvert R \text{ FULL JOIN } S \rvert = I + u_R + u_S

A condition on a right-table field in the WHERE clause, such as WHERE c.paid > 0, removes the rows where that field is null, which turns a left join into an inner join. Put such a condition in the ON clause to keep the unmatched rows.

With the policy table on the left, how many rows does each join type return? A policy table has 50,000 rows, one per policy. A claim table has 4,200 rows, one per claim. Of the claims, 60 cite policy numbers that are not in the policy table, and the other 4,140 belong to 3,550 different policies.

Answer

  • The policy number is unique in the policy table, so each matched claim pairs with exactly one policy and I = 4{,}140.
  • The policies with no claim are the unmatched left rows, u_R = 50{,}000 - 3{,}550 = 46{,}450.
  • The 60 orphan claims are the unmatched right rows, u_S = 60.
  • The inner join returns 4,140 rows and the left join 4{,}140 + 46{,}450 = 50{,}590.
  • The right join returns 4{,}140 + 60 = 4{,}200 rows, exactly the claims, which confirms that no claim was duplicated.
  • The full outer join returns 4{,}140 + 46{,}450 + 60 = 50{,}650 rows.
  • The left join drops the 60 orphans and their losses, so they need investigating before any losses are summarized.

Keys and cardinality

When the key is unique in one table, as a policy number is in the policy table, the join is one-to-many and each row of the other table matches at most once. A left join keeps every left row at least once, but a left row with three matches appears three times. So a left join returns exactly as many rows as the left table only when the key is unique on the right. When the key repeats in both tables the join is many-to-many and multiplies rows within each key value, which inflates any sum taken afterwards. Relational Database works through both cases with exposure.

Orphan records and null keys

A row whose foreign key has no match, such as a claim citing a policy number that is not in the policy table, is an orphan. An inner join drops it without any message, and so does a left join from the policy table. Only a right or full outer join from the policy table keeps it. The orphans are found deliberately with an anti-join: a left join from the claims followed by WHERE policy.policy_id IS NULL.

A row whose key is null has no match even against another null, because null equals nothing in SQL. Such rows drop out of an inner join and come through an outer join unmatched, which can look like orphans.

Join conditions beyond equality

A join condition can combine several columns or a range. A claim belongs to a policy term only when its accident date falls between that term's effective and expiration dates. Joining claims to a table with one row per policy term needs both the policy number and the date range in the ON clause.

How many rows does this join give for policy 501, and what condition fixes it? A policy table has one row per annual term. Policy 501 has a 2024 term (effective 2024-03-01, expiring 2025-03-01) and a 2025 term (effective 2025-03-01, expiring 2026-03-01). The claim table holds claim K1 on policy 501 with accident date 2024-11-14 and claim K2 on policy 501 with accident date 2025-06-30. An analyst writes:

SELECT t.policy_id, t.eff_date, c.claim_id
FROM policy_term t
LEFT JOIN claim c ON t.policy_id = c.policy_id

Answer

  • Policy 501 has 2 term rows and 2 claim rows with the same key, so the join pairs every term with every claim.
  • The join gives 2 \times 2 = 4 rows for policy 501.
  • Each term now shows both claims, so a count by term gives 2 claims in each year when there was one, and the 2025 term carries a claim from before it began.
  • The fix adds the accident date to the condition: AND c.acc_date >= t.eff_date AND c.acc_date < t.exp_date.
  • Now K1 matches only the 2024 term and K2 only the 2025 term, and the join gives 2 rows.
  • The upper bound must be strictly less than the expiration date, because a claim on 2025-03-01 would otherwise match both terms.

The unit of observation

The unit of observation is what each row of the analysis data set represents, such as a policy term. The table whose rows are those units goes on the left of a left join, so that none of them is lost. For a claim frequency data set that table is the policy or exposure table, because a policy with no claims is an observation with zero claims, not a missing one. This choice is part of data preparation, and the result should be tidy.

After such a join, a policy with no claims has one row with null claim fields. COUNT(*) counts every row and gives that policy 1. COUNT(c.claim_id) counts only the rows where the field is not null and gives it the correct 0.

Practice questions

Actuarial Notes