How to Handle Duplicate Records in Snowflake?

Duplicate records are one of those problems that almost every data engineer eventually runs into. You may have a pipeline that works perfectly, but suddenly the same customer, order, or transaction appears multiple times in a table. The issue may come from repeated file loads, incorrect joins, source-system problems, or a pipeline running more than once.

The good news is that Snowflake provides several SQL techniques to identify, remove, and prevent duplicates. If you’re building practical data engineering skills through Snowflake Training in Chennai, understanding these techniques is important because duplicate handling is a common real-world requirement.

What Are Duplicate Records?

A duplicate record occurs when the same logical piece of information appears more than once when it should exist only once.

For example, imagine an EMPLOYEE table:

SELECT * FROM EMPLOYEE;

The data might look like this:

EMP_ID | NAME  | DEPARTMENT

101    | Arun  | Finance

102    | Priya | HR

101    | Arun  | Finance

Here, employee 101 appears twice.

But identifying duplicates is not always as simple as comparing every column. Sometimes two records have different timestamps or technical fields but represent the same business entity.

That’s why the first step is deciding what makes a record unique.

How to Find Duplicate Records

One of the easiest ways to identify duplicates is with GROUP BY and COUNT().

Suppose EMP_ID should be unique:

SELECT EMP_ID, COUNT(*) AS RECORD_COUNT

FROM EMPLOYEE

GROUP BY EMP_ID

HAVING COUNT(*) > 1;

This query groups records by employee ID and returns only the IDs appearing more than once.

If you need to identify duplicates based on multiple columns, you can include those columns:

SELECT EMP_ID, NAME, DEPARTMENT, COUNT(*) AS RECORD_COUNT

FROM EMPLOYEE

GROUP BY EMP_ID, NAME, DEPARTMENT

HAVING COUNT(*) > 1;

This approach is simple and useful when you’re investigating data quality issues.

Using ROW_NUMBER() to Remove Duplicates

When you actually need to keep one record and remove the others, ROW_NUMBER() is one of the most useful techniques.

For example:

SELECT *

FROM (

    SELECT *,

           ROW_NUMBER() OVER (

               PARTITION BY EMP_ID

               ORDER BY CREATED_DATE DESC

           ) AS RN

    FROM EMPLOYEE

)

WHERE RN = 1;

Here, Snowflake groups records by EMP_ID and assigns a number to each record.

Because the query orders by CREATED_DATE DESC, the newest record receives RN = 1.

This means you can keep the latest version of each employee record while ignoring older duplicates.

Deleting Duplicate Records from a Table

If you want to permanently remove duplicates, you need to be more careful.

A common approach is to identify the records that should remain and then delete the unwanted ones. In production environments, it’s a good idea to test the selection query first before running a DELETE.

For example, you might use a unique identifier or a combination of columns to determine which rows should be retained.

Another practical approach is to create a clean version of the data using ROW_NUMBER() and then replace or merge the cleaned records into the target table.

The important point is simple: don’t delete duplicates until you clearly define which record is the correct one.

Handling Duplicates with MERGE

MERGE is particularly useful when duplicates are related to incremental data loading.

Suppose a source system sends updated customer records repeatedly. Instead of blindly inserting every incoming row, you can compare the incoming data with existing records.

A simplified example is:

MERGE INTO TARGET_CUSTOMER t

USING SOURCE_CUSTOMER s

ON t.CUSTOMER_ID = s.CUSTOMER_ID

WHEN MATCHED THEN

    UPDATE SET t.NAME = s.NAME

WHEN NOT MATCHED THEN

    INSERT (CUSTOMER_ID, NAME)

    VALUES (s.CUSTOMER_ID, s.NAME);

This allows Snowflake to update an existing record when a matching key is found and insert a new record when there isn’t one.

However, the source side should also be checked for duplicates. If multiple source rows match the same target row unexpectedly, the merge logic can produce errors or unwanted results.

Why Duplicates Happen in Snowflake Pipelines

Duplicate records usually don’t appear randomly. There is often a reason behind them.

One common cause is reprocessing the same file. For example, if a pipeline loads a CSV file into a table and the same file gets processed again, the records may be inserted twice.

Another cause is a source application sending the same transaction more than once.

Duplicates can also come from joins. If one customer has multiple matching rows in another table, a join may unintentionally multiply records.

Pipeline retries are another common cause. If a job fails after inserting data but before marking the process as completed, a retry may insert the same records again.

Understanding the source of duplication is therefore just as important as cleaning the data.

Preventing Duplicate Records

Cleaning duplicates after they appear is useful, but preventing them is even better.

One effective approach is using a reliable business key. For example, an order ID may uniquely identify an order. Your pipeline can use that key to determine whether incoming data is new or already processed.

Another useful practice is maintaining a load-tracking mechanism. Information such as file names, batch IDs, ingestion timestamps, or source transaction IDs can help identify whether a particular batch has already been processed.

For streaming or continuous ingestion scenarios, Snowflake features such as Streams and Tasks can also be used to build controlled incremental processing workflows.

Duplicate Data and Data Quality

Duplicate records can cause more than just a messy table.

Imagine a sales dashboard that calculates total revenue. If the same order is loaded twice, revenue could be overstated.

Similarly, duplicate customer records can affect marketing reports, customer counts, inventory calculations, and machine learning datasets.

That’s why duplicate detection should be considered part of overall data quality rather than simply a cleanup task.

Data engineers often combine duplicate checks with validation rules, null checks, referential checks, and other quality controls.

Best Practices for Managing Duplicates

Before removing duplicates, always determine what the unique business key should be.

Test your SQL with a SELECT before performing a DELETE or other destructive operation.

When multiple versions of a record exist, decide whether you want the latest record, earliest record, or a specific trusted version.

Also, investigate why duplicates are appearing. If the pipeline continues generating duplicates, repeatedly cleaning the target table is only treating the symptom.

Finally, build duplicate checks into your data pipeline wherever possible. Automated validation can catch problems much earlier.

Final Thoughts

Handling duplicate records is a basic but important part of Snowflake data engineering. Techniques such as GROUP BY, COUNT(), ROW_NUMBER(), and MERGE give data engineers practical ways to identify and manage duplicate data. More importantly, designing pipelines with reliable keys and proper load-tracking logic can prevent the problem from happening repeatedly.

 

With practical projects, SQL exercises, pipeline concepts, and interview preparation, Qmatrix Technologies helps learners build the hands-on skills needed to work confidently with Snowflake data engineering tasks.

Scroll to Top