Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 7 min read

Your JOIN Looks Correct. But Can You Trust It?

How to evaluate JOIN relationships, inspect cardinality, and catch fanout risks in dbt with IntaLink for dbt. A SQL query can compile successfully, run without errors, and still return incorrect results. One common rea

Your JOIN Looks Correct. But Can You Trust It?

How to evaluate JOIN relationships, inspect cardinality, and catch fanout risks in dbt with IntaLink for dbt.

A SQL query can compile successfully, run without errors, and still return incorrect results.

One common reason is the JOIN.

The columns look related. The values match. The query executes.

But none of that guarantees the relationship is appropriate for the model you're building.

When working with dbt, there are three questions worth asking before trusting a JOIN:

  1. Do these columns represent the same business relationship?
  2. What does the data tell us about their cardinality?
  3. Will joining them change the grain of the result?

Let's walk through a practical example and see how IntaLink for dbt brings some of that information directly into VS Code.

1. Finding a JOIN Is Not the Same as Validating It

Suppose you're building a dbt model that combines support interactions with ticket information.

Your source tables contain:

support_interactions
--------------------
cs_log_id
ticket_ref
interaction_type
interaction_time

support_tickets
---------------
ticket_id
customer_id
ticket_status
created_at

The likely relationship is:

interactions.ticket_ref = tickets.ticket_id

But how do you know it's the right one?

The usual approach involves inspecting schemas, searching existing SQL models, comparing values, and checking whether the identifiers behave as expected.

IntaLink for dbt provides a different starting point.

IntaLink Cloud analyzes authorized database metadata and column-value evidence to discover candidate relationships. Developers can review those relationships and make a reviewed relationship set available to the VS Code extension.

When writing SQL, the extension can then suggest related tables or models based on that information.

Figure 1. IntaLink suggests related tables while a developer writes a JOIN in a dbt model.

This helps developers find candidate relationships without repeatedly searching through the database structure.

But the more important question comes next.

2. Inspect the Evidence Behind a JOIN Suggestion

After selecting a table, developers still need to choose the correct JOIN condition.

IntaLink for dbt can suggest eligible conditions after ON, using the aliases already present in the SQL statement.

For example:

SELECT
    interactions.cs_log_id AS interaction_id,
    interactions.ticket_ref,
    tickets.customer_id,
    tickets.ticket_status
FROM {{ source('commerce', 'support_interactions') }} AS interactions
LEFT JOIN {{ source('commerce', 'support_tickets') }} AS tickets
    ON interactions.ticket_ref = tickets.ticket_id

The extension can also display supporting relationship information.

In the example shown below, the suggestion includes:

Evidence Value
Relationship type Key relationship
Value inclusion 100%
Distinct values 1,200 β†’ 1,200
Inferred cardinality N:1
Review status Confirmed

Figure 2. Relationship evidence appears alongside a suggested JOIN condition in VS Code.

Let's look at why these details matter.

Value inclusion

Value inclusion measures how much of one column's distinct value set appears in another.

A simplified SQL check might look like this:

WITH left_keys AS (
    SELECT DISTINCT ticket_ref AS join_key
    FROM support_interactions
    WHERE ticket_ref IS NOT NULL
),
right_keys AS (
    SELECT DISTINCT ticket_id AS join_key
    FROM support_tickets
    WHERE ticket_id IS NOT NULL
)
SELECT
    COUNT(*) AS left_distinct_keys,
    COUNT(r.join_key) AS matched_keys,
    100.0 * COUNT(r.join_key) / NULLIF(COUNT(*), 0)
        AS inclusion_pct
FROM left_keys l
LEFT JOIN right_keys r
    ON l.join_key = r.join_key;

This example calculates the percentage of distinct, non-null keys on the left that appear on the right.

It is an illustrative check, not the exact internal algorithm used by IntaLink.

A high inclusion rate is useful evidence, but it doesn't prove that the columns represent the same business entity.

Distinct-value counts

Distinct-value statistics help show how many different identifiers exist on each side.

For example:

SELECT
    COUNT(*) AS total_rows,
    COUNT(DISTINCT ticket_id) AS distinct_ticket_ids
FROM support_tickets;

If the two numbers are equal and the key is non-null, the field is unique in the examined dataset.

If they differ, the field contains repeated values or nulls, which may affect how the JOIN behaves.

Cardinality

Cardinality describes how records relate across two tables.

Common patterns include:

  • 1:1 β€” one record matches at most one record on either side.
  • 1:N β€” one record can match multiple records on the other side.
  • N:1 β€” multiple records can match one record on the other side.
  • N:M β€” multiple records can match multiple records.

For support interactions and tickets, N:1 may be reasonable: several interactions can belong to the same ticket.

But inferred cardinality is still evidence, not a database constraint or a guarantee about future data.

Review status

IntaLink also exposes relationship review status.

A relationship marked Confirmed has been reviewed within the relationship workflow.

That is useful, but it doesn't mean the JOIN is automatically correct for every analytical question.

Developers still need to consider business meaning and the grain of the model.

3. Why a Valid JOIN Can Still Produce Incorrect Results

Now consider a different example.

You have an order-level table:

order_id order_amount
101 100
102 200

And an order-items table:

order_id item_id
101 A
101 B
101 C
102 D

The original order amount totals 300.

Now run:

SELECT
    SUM(o.order_amount) AS total_amount
FROM orders o
LEFT JOIN order_items i
    ON o.order_id = i.order_id;

The result is 500.

Why?

Order 101 matches three items, so its amount of 100 appears three times. Order 102 matches one item, so its amount of 200 appears once.

The query executes successfully.

The JOIN condition is valid.

But the aggregation is wrong if the intended metric is total order amount.

This is a classic JOIN fanout problem.

Detecting the risk

IntaLink for dbt can flag certain non-unique JOIN keys that may multiply rows.

In the example below, the extension warns that items.order_id is not unique and suggests checking the aggregation grain.

Figure 3. A fanout warning highlights a non-unique JOIN key and suggests reviewing the aggregation before trusting the result.

The warning doesn't mean every one-to-many JOIN is incorrect.

If your intended output is one row per order item, the relationship may be exactly what you need.

The risk appears when the resulting grain conflicts with the metric or model you're trying to build.

4. Fixing the Aggregation Grain

There isn't one universal fix for JOIN fanout.

The correct approach depends on what the model is supposed to represent.

If your goal is simply to calculate total order amount, you don't need to join order items at all:

SELECT SUM(order_amount) AS total_amount
FROM orders;

If you need order-level information together with item counts, you can aggregate the item table before joining:

WITH items_by_order AS (
    SELECT
        order_id,
        COUNT(*) AS item_count
    FROM order_items
    GROUP BY order_id
)

SELECT
    o.order_id,
    o.order_amount,
    COALESCE(i.item_count, 0) AS item_count
FROM orders o
LEFT JOIN items_by_order i
    ON o.order_id = i.order_id;

Now the output preserves one row per order, assuming orders.order_id is unique.

That assumption should also be tested.

For example, in dbt you can define a uniqueness test on the relevant order key:

models:
  - name: stg_orders
    columns:
      - name: order_id
        data_tests:
          - not_null
          - unique

You can also add relationship tests to validate referential integrity between related models.

These tests complement relationship evidence and editor diagnostics. They serve different purposes.

5. From Individual JOIN Checks to Reusable Relationship Knowledge

A developer may spend time validating a relationship today, only for another developer to repeat the same investigation next month.

IntaLink for dbt is designed to make reviewed relationship information easier to reuse.

Developers can confirm relationships or stop unwanted suggestions, with decisions maintained in the project's relationship configuration.

The extension also supports relationship lineage inheritance through eligible dbt transformations, including certain renames, casts, CTEs, SELECT *, and nested ref() calls.

This can help preserve relationship context across supported model transformations.

Calculated expressions aren't automatically treated as equivalent to their source fields, because their meaning or behavior may have changed.

The objective is not to replace developer judgment.

It's to reduce repeated investigation and make existing relationship evidence available where developers need it.

6. Try IntaLink for dbt

IntaLink for dbt is available on the VS Code Marketplace.

The extension works with reviewed relationship information from IntaLink Cloud and brings relationship-aware suggestions and diagnostics into local dbt development.

To get started, you'll need a supported dbt project, a generated target/manifest.json, and access to an IntaLink relationship set.

IntaLink for dbt β€” VS Code Marketplace

Product Overview

We're also inviting 10 developers with real dbt projects to participate in our Founding User Program, with 12 months of free access and updates plus dedicated 1:1 engineering support under agreed program terms.

If you have a JOIN that you're not entirely confident about, we'd be interested in hearing how you investigate it.

Final Thoughts

A JOIN that runs is not necessarily a JOIN you should trust.

Before relying on the result, developers need to understand the relationship, its cardinality, and the grain of the output.

Relationship evidence helps explain why two fields may connect.

Fanout warnings help identify when a JOIN deserves a closer look.

And dbt tests help enforce the assumptions your models depend on.

Better JOINs don't come from autocomplete alone. They come from understanding the data behind the SQL.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.