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.
| product_id | name | price |
|---|---|---|
| 1 | Kettle | 30 |
| 2 | Mug | 5 |
| 3 | Teapot | 25 |
| item_id | order_id | product_id | quantity |
|---|---|---|---|
| 1 | 101 | 3 | 1 |
| 2 | 101 | 2 | 5 |
| 3 | 102 | 1 | 1 |
| 4 | 103 | 3 | 3 |
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| item_id | quantity | name |
|---|---|---|
| 1 | 1 | Teapot |
| 2 | 5 | Mug |
| 3 | 1 | Kettle |
| 4 | 3 | Teapot |
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| item_id | quantity | name |
|---|---|---|
| 1 | 1 | Kettle |
| 2 | 5 | Mug |
| 3 | 1 | Teapot |
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_idThe 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| return_id | product_code |
|---|---|
| 901 | 3 |
| 902 | 2 |
| 903 | 3 |
| product_id | name | price |
|---|---|---|
| 1 | Kettle | 30 |
| 2 | Mug | 5 |
| 3 | Teapot | 25 |
| return_id | name |
|---|---|
| 901 | Teapot |
| 902 | Mug |
| 903 | Teapot |
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 value | Right value | Compared as | Equal? |
|---|---|---|---|
| 'TEA-3' | 'TEA-3' | VARCHAR | true |
| 'TEA-3' | 'tea-3' | VARCHAR | false |
| 'TEA-3' | 'TEA-3 ' | VARCHAR | false |
| '0042' | '42' | VARCHAR | false |
| CAST('0042' AS INTEGER) | CAST('42' AS INTEGER) | INTEGER | true |
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?
- employees.name = departments.name
- employees.employee_id = departments.department_id
- employees.department_id = departments.department_id
- employees.department_id = departments.floor
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.