SQL / Data Engineering

SQL JOIN Duplicate Rows: Why Revenue Doubles & How to Fix It

Imagine you’re working on a sales report for a retail business.

The report originally shows £250 in revenue. You add a SQL JOIN to include customer marketing information, then run the query again.

Suddenly, the reported revenue is £500.

But the business hasn’t made any additional sales, and the original sales data hasn’t changed.

So why has the revenue doubled?

This is an example of SQL JOIN fan-out. It happens when a JOIN produces multiple matching rows for an original record. If we then sum the repeated sales amounts, we can end up reporting more revenue than the business actually earned.

In this article, we’ll recreate the problem using PostgreSQL, investigate why it happens, and explore different ways to correct it.

We’ll also see why the right SQL solution depends on what the business wants to measure.

1. Understanding our business scenario and datasets

We’re working with a fictional retail company that records sales transactions and assigns marketing tags to its customers.

For our main example, we’ll use two tables.

Table 1: fact_sales

The fact_sales table stores individual sales transactions.

sale_id

customer_id

sale_amount

1

101

£50

2

101

£80

3

102

£40

4

102

£30

5

103

£50

Each row represents one completed sale.

A customer can appear more than once because they can make several purchases. However, each sale has its own unique sale_id.

Our total revenue is:

£50 + £80 + £40 + £30 + £50 = £250

Table 2: customer_tags

The customer_tags table stores marketing classifications assigned to customers.

customer_id

tag

101

Newsletter

101

VIP

102

Promotion

102

Returning

103

Loyalty

103

VIP

A customer can have more than one marketing tag.

For example, customer 101 belongs to both the Newsletter and VIP categories.

These are valid records. They aren’t necessarily duplicates that need to be removed.

Creating the datasets in PostgreSQL

We’ll use a schema called dip_fanout to keep our demonstration tables together.

The following script creates our two tables and inserts the sample records.

Note: Run this in a development database. The script resets the two demonstration tables, so existing data in those tables would be deleted.

CREATE SCHEMA IF NOT EXISTS dip_fanout;

DROP TABLE IF EXISTS dip_fanout.customer_tags;
DROP TABLE IF EXISTS dip_fanout.fact_sales;

CREATE TABLE dip_fanout.fact_sales (
    sale_id INTEGER PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    sale_amount NUMERIC(12,2) NOT NULL
        CHECK (sale_amount >= 0)
);

CREATE TABLE dip_fanout.customer_tags (
    customer_id INTEGER NOT NULL,
    tag VARCHAR(50) NOT NULL,
    PRIMARY KEY (customer_id, tag)
);

INSERT INTO dip_fanout.fact_sales
    (sale_id, customer_id, sale_amount)
VALUES
    (1, 101, 50.00),
    (2, 101, 80.00),
    (3, 102, 40.00),
    (4, 102, 30.00),
    (5, 103, 50.00);

INSERT INTO dip_fanout.customer_tags
    (customer_id, tag)
VALUES
    (101, 'Newsletter'),
    (101, 'VIP'),
    (102, 'Promotion'),
    (102, 'Returning'),
    (103, 'Loyalty'),
    (103, 'VIP')

CREATE SCHEMA IF NOT EXISTS dip_fanout;

DROP TABLE IF EXISTS dip_fanout.customer_tags;
DROP TABLE IF EXISTS dip_fanout.fact_sales;

CREATE TABLE dip_fanout.fact_sales (
    sale_id INTEGER PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    sale_amount NUMERIC(12,2) NOT NULL
        CHECK (sale_amount >= 0)
);

CREATE TABLE dip_fanout.customer_tags (
    customer_id INTEGER NOT NULL,
    tag VARCHAR(50) NOT NULL,
    PRIMARY KEY (customer_id, tag)
);

INSERT INTO dip_fanout.fact_sales
    (sale_id, customer_id, sale_amount)
VALUES
    (1, 101, 50.00),
    (2, 101, 80.00),
    (3, 102, 40.00),
    (4, 102, 30.00),
    (5, 103, 50.00);

INSERT INTO dip_fanout.customer_tags
    (customer_id, tag)
VALUES
    (101, 'Newsletter'),
    (101, 'VIP'),
    (102, 'Promotion'),
    (102, 'Returning'),
    (103, 'Loyalty'),
    (103, 'VIP')

CREATE SCHEMA IF NOT EXISTS dip_fanout;

DROP TABLE IF EXISTS dip_fanout.customer_tags;
DROP TABLE IF EXISTS dip_fanout.fact_sales;

CREATE TABLE dip_fanout.fact_sales (
    sale_id INTEGER PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    sale_amount NUMERIC(12,2) NOT NULL
        CHECK (sale_amount >= 0)
);

CREATE TABLE dip_fanout.customer_tags (
    customer_id INTEGER NOT NULL,
    tag VARCHAR(50) NOT NULL,
    PRIMARY KEY (customer_id, tag)
);

INSERT INTO dip_fanout.fact_sales
    (sale_id, customer_id, sale_amount)
VALUES
    (1, 101, 50.00),
    (2, 101, 80.00),
    (3, 102, 40.00),
    (4, 102, 30.00),
    (5, 103, 50.00);

INSERT INTO dip_fanout.customer_tags
    (customer_id, tag)
VALUES
    (101, 'Newsletter'),
    (101, 'VIP'),
    (102, 'Promotion'),
    (102, 'Returning'),
    (103, 'Loyalty'),
    (103, 'VIP')

Now that we have our datasets, let’s understand how they relate to each other.

2. Why the structure of the data matters

Before joining tables, it’s important to understand what one row represents in each table.

In data engineering, this is called the grain.

For our example:

  • fact_sales contains one row per sale.

  • customer_tags contains one row per customer and marketing tag combination.

The two tables have different grains.

Consider customer 101.

This customer has two sales and two marketing tags.

When we join the tables using customer_id, each of the customer’s sales will match both marketing tags.

This means a single sale can appear more than once in the result.

Sales transactions and customer marketing tags joined using customer_id, showing that each sale matches two tag records.

The important point is that SQL follows the JOIN condition. It doesn’t automatically know that we want each sale counted only once when calculating revenue.

3. The SQL JOIN that doubles reported revenue

The reporting team wants to include customer marketing tags alongside sales.

We start with this query:

SELECT
    s.sale_id,
    s.customer_id,
    s.sale_amount,
    t.tag
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON s.customer_id = t.customer_id
ORDER BY s.sale_id,

SELECT
    s.sale_id,
    s.customer_id,
    s.sale_amount,
    t.tag
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON s.customer_id = t.customer_id
ORDER BY s.sale_id,

SELECT
    s.sale_id,
    s.customer_id,
    s.sale_amount,
    t.tag
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON s.customer_id = t.customer_id
ORDER BY s.sale_id,

The query runs successfully.

However, instead of five rows, it returns ten.

Let’s look at sale 1.

sale_id

sale_amount

tag

1

£50

Newsletter

1

£50

VIP

The same £50 sale appears twice because customer 101 has two matching tags.

This also happens to the other sales in our dataset.

Now imagine we calculate revenue from the joined result:

SELECT
    COUNT(*) AS joined_rows,
    SUM(s.sale_amount) AS revenue_after_join
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON

SELECT
    COUNT(*) AS joined_rows,
    SUM(s.sale_amount) AS revenue_after_join
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON

SELECT
    COUNT(*) AS joined_rows,
    SUM(s.sale_amount) AS revenue_after_join
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON

Our PostgreSQL result:

joined_rows

revenue_after_join

10

500.00

We started with five genuine sales worth £250.

After the JOIN, the result contains ten rows, and summing their sales amounts produces £500.

Five original sales worth £250 become ten joined records totalling £500 because the JOIN repeats sales amounts.

No additional sales have taken place.

The problem is that the JOIN has repeated the original sales amounts across multiple matching rows.

The JOIN itself isn’t necessarily wrong. If the business wants to analyse every sale and its associated marketing tags, those additional rows may be expected.

The issue arises when we calculate revenue as though each joined row represents a separate sale.

4. Investigating the unexpected revenue increase

Suppose we encounter this problem in a reporting pipeline.

Rather than immediately changing the SQL, we should first investigate where the additional rows are coming from.

Check 1: Which sales appear more than once?

We can count how many times each sale appears after the JOIN.

SELECT
    s.sale_id,
    COUNT(*) AS rows_after_join
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON s.customer_id = t.customer_id
GROUP BY s.sale_id
HAVING COUNT(*) > 1
ORDER BY

SELECT
    s.sale_id,
    COUNT(*) AS rows_after_join
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON s.customer_id = t.customer_id
GROUP BY s.sale_id
HAVING COUNT(*) > 1
ORDER BY

SELECT
    s.sale_id,
    COUNT(*) AS rows_after_join
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON s.customer_id = t.customer_id
GROUP BY s.sale_id
HAVING COUNT(*) > 1
ORDER BY

PostgreSQL returned:

sale_id

rows_after_join

1

2

2

2

3

2

4

2

5

2

Every sale appears twice.

This tells us that the JOIN is increasing the number of rows representing each sale.

Check 2: Why are the sales being multiplied?

Next, let’s investigate the matching records in customer_tags.

SELECT
    customer_id,
    COUNT(*) AS matching_tag_rows
FROM dip_fanout.customer_tags
GROUP BY customer_id
HAVING COUNT(*) > 1
ORDER BY

SELECT
    customer_id,
    COUNT(*) AS matching_tag_rows
FROM dip_fanout.customer_tags
GROUP BY customer_id
HAVING COUNT(*) > 1
ORDER BY

SELECT
    customer_id,
    COUNT(*) AS matching_tag_rows
FROM dip_fanout.customer_tags
GROUP BY customer_id
HAVING COUNT(*) > 1
ORDER BY

Our results:

customer_id

matching_tag_rows

101

2

102

2

103

2

We have now identified the cause.

Each customer has two matching tag records. Consequently, every sale appears twice after the JOIN.

This explains why our revenue increased from £250 to £500.

Now we need to decide how to correct the calculation.

But before choosing a SQL function, we must understand the business requirement.

5. Choosing the right SQL solution

There are several ways to prevent unwanted row multiplication.

However, the best approach depends on what the business actually needs from the data.

Let’s explore four different reporting requirements using our retail example.

Use case 1: Calculate revenue from customers with at least one marketing tag

Business request: “Calculate the total revenue generated by customers who have at least one marketing tag.”

For this requirement, we don’t need the individual tags.

We only need to know whether a customer has at least one matching record in customer_tags.

We can use EXISTS.

SELECT
    s.sale_id,
    s.customer_id,
    s.sale_amount
FROM dip_fanout.fact_sales AS s
WHERE EXISTS (
    SELECT 1
    FROM dip_fanout.customer_tags AS t
    WHERE t.customer_id = s.customer_id
)
ORDER BY

SELECT
    s.sale_id,
    s.customer_id,
    s.sale_amount
FROM dip_fanout.fact_sales AS s
WHERE EXISTS (
    SELECT 1
    FROM dip_fanout.customer_tags AS t
    WHERE t.customer_id = s.customer_id
)
ORDER BY

SELECT
    s.sale_id,
    s.customer_id,
    s.sale_amount
FROM dip_fanout.fact_sales AS s
WHERE EXISTS (
    SELECT 1
    FROM dip_fanout.customer_tags AS t
    WHERE t.customer_id = s.customer_id
)
ORDER BY

Our PostgreSQL result:

sale_id

customer_id

sale_amount

1

101

£50

2

101

£80

3

102

£40

4

102

£30

5

103

£50

We now have five rows instead of ten.

Notice that our original fact_sales table hasn’t changed. The five sales are still there.

We’ve changed how we retrieve the data so that each qualifying sale appears once.

Let’s calculate the revenue:

SELECT
    COUNT(*) AS qualifying_sales,
    SUM(s.sale_amount) AS corrected_revenue
FROM dip_fanout.fact_sales AS s
WHERE EXISTS (
    SELECT 1
    FROM dip_fanout.customer_tags AS t
    WHERE t.customer_id = s.customer_id
)

SELECT
    COUNT(*) AS qualifying_sales,
    SUM(s.sale_amount) AS corrected_revenue
FROM dip_fanout.fact_sales AS s
WHERE EXISTS (
    SELECT 1
    FROM dip_fanout.customer_tags AS t
    WHERE t.customer_id = s.customer_id
)

SELECT
    COUNT(*) AS qualifying_sales,
    SUM(s.sale_amount) AS corrected_revenue
FROM dip_fanout.fact_sales AS s
WHERE EXISTS (
    SELECT 1
    FROM dip_fanout.customer_tags AS t
    WHERE t.customer_id = s.customer_id
)

Our result:

qualifying_sales

corrected_revenue

5

250.00

Why does this work?

EXISTS checks whether a matching record is present.

Even if a customer has multiple marketing tags, their sales aren’t repeated for each tag.

Each qualifying sale contributes to the revenue calculation once.

This makes EXISTS suitable when the business only needs to know whether a match exists.

Use case 2: Join sales to a unique list of tagged customers

Business request: “Create a sales dataset containing customers who appear in our marketing classification list.”

Another approach is to create a list containing one row per tagged customer before joining it to sales.

WITH tagged_customers AS (
    SELECT DISTINCT
        customer_id
    FROM dip_fanout.customer_tags
)
SELECT
    COUNT(*) AS qualifying_sales,
    SUM(s.sale_amount) AS corrected_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN tagged_customers AS t
    ON

WITH tagged_customers AS (
    SELECT DISTINCT
        customer_id
    FROM dip_fanout.customer_tags
)
SELECT
    COUNT(*) AS qualifying_sales,
    SUM(s.sale_amount) AS corrected_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN tagged_customers AS t
    ON

WITH tagged_customers AS (
    SELECT DISTINCT
        customer_id
    FROM dip_fanout.customer_tags
)
SELECT
    COUNT(*) AS qualifying_sales,
    SUM(s.sale_amount) AS corrected_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN tagged_customers AS t
    ON

Our PostgreSQL result is again £250.

Why does this work?

The tagged_customers query creates a unique list of customer IDs.

When we join this list to the sales table, each sale can match at most one customer record.

We are using DISTINCT for a specific purpose: creating one row per customer.

We aren’t deleting valid sales or arbitrarily choosing which marketing tag should be retained.

Use case 3: Calculate revenue using each customer’s latest classification

Now consider a different business request.

Business request: “Show revenue grouped by each customer’s most recent marketing classification.”

Our original customer_tags table cannot fully answer this question.

It tells us which tags belong to each customer, but it doesn’t tell us when those classifications were recorded.

We therefore need additional information.

Introducing a third dataset: customer_tags_history

For this example, we’ll create a new table called dip_fanout.customer_tags_history.

It contains customer marketing classifications and an additional column called updated_at, which records the date associated with each classification.

customer_id

tag

updated_at

101

Newsletter

2026-10-01

101

VIP

2026-11-01

102

Promotion

2026-10-05

102

Returning

2026-11-02

103

Loyalty

2026-10-10

103

VIP

2026-11-03

This is a separate demonstration table. Our original fact_sales and customer_tags tables remain unchanged.

We’ll keep it in the same dip_fanout schema so all our examples are easy to locate.

Create and populate the table:

CREATE TABLE IF NOT EXISTS
    dip_fanout.customer_tags_history (
        customer_id INTEGER NOT NULL,
        tag VARCHAR(50) NOT NULL,
        updated_at DATE NOT NULL,
        PRIMARY KEY (customer_id, tag, updated_at)
    );

TRUNCATE TABLE dip_fanout.customer_tags_history;

INSERT INTO dip_fanout.customer_tags_history
    (customer_id, tag, updated_at)
VALUES
    (101, 'Newsletter', DATE '2026-10-01'),
    (101, 'VIP', DATE '2026-11-01'),
    (102, 'Promotion', DATE '2026-10-05'),
    (102, 'Returning', DATE '2026-11-02'),
    (103, 'Loyalty', DATE '2026-10-10'),
    (103, 'VIP', DATE '2026-11-03')

CREATE TABLE IF NOT EXISTS
    dip_fanout.customer_tags_history (
        customer_id INTEGER NOT NULL,
        tag VARCHAR(50) NOT NULL,
        updated_at DATE NOT NULL,
        PRIMARY KEY (customer_id, tag, updated_at)
    );

TRUNCATE TABLE dip_fanout.customer_tags_history;

INSERT INTO dip_fanout.customer_tags_history
    (customer_id, tag, updated_at)
VALUES
    (101, 'Newsletter', DATE '2026-10-01'),
    (101, 'VIP', DATE '2026-11-01'),
    (102, 'Promotion', DATE '2026-10-05'),
    (102, 'Returning', DATE '2026-11-02'),
    (103, 'Loyalty', DATE '2026-10-10'),
    (103, 'VIP', DATE '2026-11-03')

CREATE TABLE IF NOT EXISTS
    dip_fanout.customer_tags_history (
        customer_id INTEGER NOT NULL,
        tag VARCHAR(50) NOT NULL,
        updated_at DATE NOT NULL,
        PRIMARY KEY (customer_id, tag, updated_at)
    );

TRUNCATE TABLE dip_fanout.customer_tags_history;

INSERT INTO dip_fanout.customer_tags_history
    (customer_id, tag, updated_at)
VALUES
    (101, 'Newsletter', DATE '2026-10-01'),
    (101, 'VIP', DATE '2026-11-01'),
    (102, 'Promotion', DATE '2026-10-05'),
    (102, 'Returning', DATE '2026-11-02'),
    (103, 'Loyalty', DATE '2026-10-10'),
    (103, 'VIP', DATE '2026-11-03')

Now we can use ROW_NUMBER() to select each customer’s latest classification.

WITH ranked_classifications AS (
    SELECT
        customer_id,
        tag,
        ROW_NUMBER() OVER (
            PARTITION BY customer_id
            ORDER BY updated_at DESC, tag
        ) AS rn
    FROM dip_fanout.customer_tags_history
),
latest_classifications AS (
    SELECT
        customer_id,
        tag
    FROM ranked_classifications
    WHERE rn = 1
)
SELECT
    lc.tag AS latest_classification,
    COUNT(*) AS sales_count,
    SUM(s.sale_amount) AS total_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN latest_classifications AS lc
    ON s.customer_id = lc.customer_id
GROUP BY lc.tag
ORDER BY

WITH ranked_classifications AS (
    SELECT
        customer_id,
        tag,
        ROW_NUMBER() OVER (
            PARTITION BY customer_id
            ORDER BY updated_at DESC, tag
        ) AS rn
    FROM dip_fanout.customer_tags_history
),
latest_classifications AS (
    SELECT
        customer_id,
        tag
    FROM ranked_classifications
    WHERE rn = 1
)
SELECT
    lc.tag AS latest_classification,
    COUNT(*) AS sales_count,
    SUM(s.sale_amount) AS total_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN latest_classifications AS lc
    ON s.customer_id = lc.customer_id
GROUP BY lc.tag
ORDER BY

WITH ranked_classifications AS (
    SELECT
        customer_id,
        tag,
        ROW_NUMBER() OVER (
            PARTITION BY customer_id
            ORDER BY updated_at DESC, tag
        ) AS rn
    FROM dip_fanout.customer_tags_history
),
latest_classifications AS (
    SELECT
        customer_id,
        tag
    FROM ranked_classifications
    WHERE rn = 1
)
SELECT
    lc.tag AS latest_classification,
    COUNT(*) AS sales_count,
    SUM(s.sale_amount) AS total_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN latest_classifications AS lc
    ON s.customer_id = lc.customer_id
GROUP BY lc.tag
ORDER BY

Our PostgreSQL result:

latest_classification

sales_count

total_revenue

Returning

2

£70

VIP

3

£180

Total

5

£250

Why does this work?

ROW_NUMBER() assigns a number to each customer’s classification records.

PARTITION BY customer_id starts a separate numbering sequence for each customer.

ORDER BY updated_at DESC places the most recent classification first.

We then select rn = 1, which gives us one latest classification per customer.

The sales table can now be joined to those selected classifications without multiplying revenue.

We use tag as a simple tie-breaker when dates are equal. In a real system, we’d need an agreed business rule for deciding which classification takes priority.

There’s another important consideration.

This example uses each customer’s latest recorded classification. If the business wants to know which classification applied when an earlier sale occurred, we’d need additional date-based logic.

If you need to preserve customer changes over time, our SCD Type 2 guide explains how historical records and effective dates support accurate reporting.

When is ROW_NUMBER() useful?

Use it when the business has a meaningful rule for selecting one record, such as the latest classification, rather than simply wanting to remove repeated rows.

For a deeper explanation of SQL ranking functions, explore our guide to ROW_NUMBER vs RANK vs DENSE_RANK.

Use case 4: Report revenue and marketing tag counts per customer

Let’s consider one more business requirement.

Business request: “Show each customer’s total revenue, number of purchases and number of marketing tags.”

This time, the report needs one row per customer.

We can calculate sales totals and tag counts separately before joining them.

This approach is called pre-aggregation. It means calculating summary values before combining datasets.

WITH customer_sales AS (
    SELECT
        customer_id,
        COUNT(*) AS sales_count,
        SUM(sale_amount) AS total_revenue
    FROM dip_fanout.fact_sales
    GROUP BY customer_id
),
customer_tag_counts AS (
    SELECT
        customer_id,
        COUNT(*) AS tag_count
    FROM dip_fanout.customer_tags
    GROUP BY customer_id
)
SELECT
    s.customer_id,
    s.sales_count,
    s.total_revenue,
    COALESCE(t.tag_count, 0) AS tag_count
FROM customer_sales AS s
LEFT JOIN customer_tag_counts AS t
    ON s.customer_id = t.customer_id
ORDER BY

WITH customer_sales AS (
    SELECT
        customer_id,
        COUNT(*) AS sales_count,
        SUM(sale_amount) AS total_revenue
    FROM dip_fanout.fact_sales
    GROUP BY customer_id
),
customer_tag_counts AS (
    SELECT
        customer_id,
        COUNT(*) AS tag_count
    FROM dip_fanout.customer_tags
    GROUP BY customer_id
)
SELECT
    s.customer_id,
    s.sales_count,
    s.total_revenue,
    COALESCE(t.tag_count, 0) AS tag_count
FROM customer_sales AS s
LEFT JOIN customer_tag_counts AS t
    ON s.customer_id = t.customer_id
ORDER BY

WITH customer_sales AS (
    SELECT
        customer_id,
        COUNT(*) AS sales_count,
        SUM(sale_amount) AS total_revenue
    FROM dip_fanout.fact_sales
    GROUP BY customer_id
),
customer_tag_counts AS (
    SELECT
        customer_id,
        COUNT(*) AS tag_count
    FROM dip_fanout.customer_tags
    GROUP BY customer_id
)
SELECT
    s.customer_id,
    s.sales_count,
    s.total_revenue,
    COALESCE(t.tag_count, 0) AS tag_count
FROM customer_sales AS s
LEFT JOIN customer_tag_counts AS t
    ON s.customer_id = t.customer_id
ORDER BY

Our PostgreSQL result:

customer_id

sales_count

total_revenue

tag_count

101

2

£130

2

102

2

£70

2

103

1

£50

2

The total revenue remains £250.

Why does this work?

The customer_sales query produces one row per customer.

The customer_tag_counts query also produces one row per customer.

When we join these two results, each customer has at most one matching row in the tag summary.

This prevents revenue from being repeated.

We also use a LEFT JOIN so customers with sales but no marketing tags can still appear in the report. COALESCE displays zero when a customer has no matching tag count.

Pre-aggregation is useful here because the final report needs customer-level information rather than individual sale-tag combinations.

6. Why SUM(DISTINCT) isn’t a reliable fix

When revenue doubles after a JOIN, it can be tempting to use DISTINCT to remove repeated values.

For example:

SELECT
    COUNT(*) AS joined_rows,
    SUM(s.sale_amount) AS inflated_revenue,
    SUM(DISTINCT s.sale_amount)
        AS distinct_amount_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON

SELECT
    COUNT(*) AS joined_rows,
    SUM(s.sale_amount) AS inflated_revenue,
    SUM(DISTINCT s.sale_amount)
        AS distinct_amount_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON

SELECT
    COUNT(*) AS joined_rows,
    SUM(s.sale_amount) AS inflated_revenue,
    SUM(DISTINCT s.sale_amount)
        AS distinct_amount_revenue
FROM dip_fanout.fact_sales AS s
INNER JOIN dip_fanout.customer_tags AS t
    ON

Our PostgreSQL result:

joined_rows

inflated_revenue

distinct_amount_revenue

10

500.00

200.00

Why does SUM(DISTINCT sale_amount) return £200?

Look again at our five original sales:

£50, £80, £40, £30 and £50.

Two genuine transactions have exactly the same value of £50.

SUM(DISTINCT sale_amount) counts the amount £50 only once, even though both transactions are valid.

It removes repeated monetary values rather than identifying which transactions should be counted.

This is why SUM(DISTINCT) isn’t a reliable solution for correcting repeated sales caused by JOIN fan-out.

7. What happens when customers have different numbers of tags?

Our original example is deliberately simple.

Every customer has two marketing tags, which causes revenue to double after the JOIN.

But real datasets aren’t always this consistent.

To investigate this, we tested an extended scenario using temporary copies of our original tables.

We added two additional sales:

  • Customer 104 makes a £60 sale but has no marketing tags.

  • Customer 105 makes a £90 sale and has three marketing tags.

Our PostgreSQL results were:

Calculation

Revenue

Original sales

£400

Revenue after INNER JOIN

£770

Revenue from tagged customers using EXISTS

£340

The revenue after the JOIN is now 92.5% higher than the original total.

Two different things are happening.

Customer 104: The £60 sale disappears from the INNER JOIN result because there is no matching marketing tag.

Customer 105: The £90 sale appears three times because there are three matching tags.

The JOIN is therefore repeating some sales while excluding others.

This shows why JOIN fan-out doesn’t always produce exactly twice the original revenue.

It also demonstrates why checking only the final revenue total may not reveal the full problem.

We need to understand which records are repeated, which records are missing and whether the result matches the business requirement.

8. Preventing incorrect revenue calculations in reporting pipelines

How can we prevent this type of problem before incorrect figures reach dashboards or business reports?

A useful starting point is to build simple validation checks into the development process.

Understand what each row represents

Before joining tables, identify their grain.

Does each row represent a sale, customer, order or marketing classification?

Check the JOIN relationship

Can one record match several rows in the other table?

If so, consider whether that increase in rows is expected.

Compare row counts

Check the number of records before and after joining.

Unexpected increases or decreases can help identify possible issues.

Validate important business totals

Compare revenue from the source sales data with the results of the reporting query.

If the total changes, investigate whether the change is justified.

Check for missing matches

An INNER JOIN excludes records that have no match in the other table.

This can affect revenue just as much as repeated records.

Confirm the business requirement

Does the report need all matching records, a particular classification, a unique customer list or simply confirmation that a match exists?

Understanding the requirement helps determine the appropriate SQL approach.

These checks can help identify errors before data reaches downstream reporting solutions.

9. How would you explain this in a data engineering interview?

Imagine an interviewer asks:

“A revenue dashboard suddenly shows twice the expected revenue after you introduced a JOIN. How would you investigate?”

A good approach would be to explain your investigation in a logical sequence.

First, confirm the original revenue using the sales data.

Next, identify what one row represents in each table and check the JOIN condition.

Then compare the record counts before and after the JOIN.

Investigate whether individual sales are matching multiple records or whether some sales have no match.

Once you’ve identified the cause, select a correction based on the business requirement.

Finally, reconcile the corrected calculation with the appropriate source totals.

The goal isn’t simply to make the revenue return to its previous value.

It’s to understand why the figure changed and ensure the corrected result represents what the business intended to measure.

10. Key takeaways

Our PostgreSQL example demonstrated how five genuine sales worth £250 became ten joined rows with a reported total of £500.

The JOIN ran successfully, but aggregating the expanded result produced an incorrect revenue figure.

We also explored how different business requirements lead to different SQL approaches.

Business requirement

Suitable approach

Check whether customers have marketing tags

EXISTS

Join sales to a unique list of tagged customers

SELECT DISTINCT customer_id

Select each customer’s latest classification

ROW_NUMBER()

Combine customer-level revenue and tag counts

Pre-aggregation

We also saw why SUM(DISTINCT sale_amount) isn’t a reliable way to correct repeated sales.

The most important lesson is this:

The correct SQL approach depends on the business requirement and the intended grain, not simply on removing duplicate rows.

Understanding what each row represents, how datasets relate to one another, and what the business wants to measure will help you build more reliable SQL queries and reporting solutions.

About this article: The examples use fictional sales data created for demonstration purposes. All SQL queries and reported results were tested and validated using PostgreSQL 17.11.

Need help investigating duplicate rows or incorrect totals in your own SQL queries? Explore our SQL debugging service on Upwork.

Want to practise more SQL problems?

Explore our SQL Interview Practice workbook, featuring 50 practical challenges for data engineering and analytics professionals.

Data in Practice HQ

Practical data knowledge you can put to work.

© 2026 Data in Practice HQ