Join keys and data types

Concepts covered: Inner Joins, Type Casting

The columns compared in a join's test are called the join keys. The database will compare any 2 columns you name, as long as their types allow it. It has no idea whether the comparison means anything. Choosing keys that mean something is your job.

Same meaning comes first

A good pair of keys holds the same real-world identifier in both tables. Below, products lists what the store sells and order_items lists each line of each order. Both tables carry a product_id, and in both it means 'which product'.

order_items also has item_id, a second INTEGER column filled with small whole numbers. Nothing stops a query from comparing item_id to product_id. The 2 tabs below run the join both ways. Before you run them, predict which product name each tab gives item 1.

products
product_idnameprice
1Kettle30
2Mug5
3Teapot25
order_items
item_idorder_idproduct_idquantity
110131
210125
310211
410333

Right key

Compares product_id with product_id: the same identifier on both sides.

SELECT
  order_items.item_id,
  order_items.quantity,
  products.name
FROM order_items
INNER JOIN products
  ON order_items.product_id = products.product_id
Result
item_idquantityname
11Teapot
25Mug
31Kettle
43Teapot

Wrong key

Compares item_id with product_id: 2 integers that count different things.

SELECT
  order_items.item_id,
  order_items.quantity,
  products.name
FROM order_items
INNER JOIN products
  ON order_items.item_id = products.product_id
Result
item_idquantityname
11Kettle
25Mug
31Teapot

The right key returns 4 rows and says item 1 is a Teapot, which agrees with the data. The wrong key returns 3 rows and says item 1 is a Kettle. It raised no error, it gave item 2 the correct name by coincidence, and it silently lost item 4. A join on the wrong key produces rows that look reasonable and are false.

A shared column name is a strong hint and nothing more. 2 tables can each have a column called id or name that means entirely different things. The same identifier can also be called product_id in one table and sku_id in another. Read a few rows of each column before trusting its name.

Types decide what equal means

Suppose a returns table was loaded from a spreadsheet export, and its product_code column arrived as VARCHAR. Its values are text that happens to contain digits: '3', '2', '3'. The query below compares that text with the INTEGER product_id.

SELECT
  returns.return_id,
  products.name
FROM returns
INNER JOIN products
  ON returns.product_code = products.product_id

The fix is one you already know: convert one side with CAST so both sides have the same type. Here the text column is cast to INTEGER before the comparison.

SELECT
  returns.return_id,
  products.name
FROM returns
INNER JOIN products
  ON CAST(returns.product_code AS INTEGER) = products.product_id
returns
return_idproduct_code
9013
9022
9033
products
product_idnameprice
1Kettle30
2Mug5
3Teapot25
Result
return_idname
901Teapot
902Mug
903Teapot

3 returns come back with product names. CAST only works when every value can be read as a number. One 'N/A' in product_code stops the whole query, so read through a text key's values before you cast it.

Type also changes which values count as equal. Text is equal only when every character is the same, including capital letters, spaces and leading zeros. Numbers are equal when their values are the same.

Left valueRight valueCompared asEqual?
'TEA-3''TEA-3'VARCHARtrue
'TEA-3''tea-3'VARCHARfalse
'TEA-3''TEA-3 'VARCHARfalse
'0042''42'VARCHARfalse
CAST('0042' AS INTEGER)CAST('42' AS INTEGER)INTEGERtrue

Row 3 fails because the right value ends in a space you can barely see. Rows 4 and 5 start from the same 2 values and give opposite answers. As text, '0042' and '42' differ by 2 characters. As numbers, both are 42. When a join returns fewer rows than you expected, mismatched formatting in a text key is a common cause.

An employees table has employee_id (INTEGER), name (VARCHAR) and department_id (INTEGER). A departments table has department_id (INTEGER), name (VARCHAR) and floor (INTEGER). Which test pairs each employee with their own department?

Meaning, type, format: check them in that order. Here a type mismatch failed loudly with an error, though a database that converts one side silently would hide it. A mismatch in meaning or format fails quietly with wrong or missing rows, which is why those 2 deserve the closer look.

About this lesson

This section is part of the Joins: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.