Databricks · Duplicate keys

[DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE] in a Databricks job

The source of the MERGE holds more than one row for the same key, so two source rows tried to change one target row. Delta will not guess which of them wins. Reduce the source to one row per key, usually the latest, before merging.

The error
[DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE] Cannot perform Merge as multiple source rows matched and attempted to modify the same target row in the Delta table in possibly conflicting ways. By SQL semantics of Merge, when multiple source rows match on the same target row, the result may be ambiguous as it is unclear which source row should be used to update or delete the matching target row. You can preprocess the source table to eliminate the possibility of multiple matches.

What this error means

MERGE pairs source rows with target rows through the ON condition. When two source rows match the same target row and an update or a conditional delete applies, the result would depend on the order in which the rows are processed. Delta stops the statement instead, and the target table is left as it was.

From Databricks Runtime 16.0, the conditions in the WHEN MATCHED clauses are taken into account as well as the ON clause when duplicate matches are detected; earlier runtimes look at the ON clause only. Several matches are allowed when the matched rows are deleted unconditionally, because the outcome is the same either way.

Why it happens

  • Duplicate keys in the batch. A change feed delivered several versions of the same record in one load, or the same file was ingested twice.
  • The key in the ON clause is incomplete. The real key has two columns and the merge matches on one of them, so different records look like duplicates.
  • A join multiplied the source. The source query joins to a table with several rows per key, and every source row is repeated.

How to fix it

  1. Count the duplicates

    Run this on the source of the merge, with your key columns. Every row it returns is a key that appears more than once.

    Keys that appear more than once in the source
    01SELECT order_id, COUNT(*) AS n02FROM updates03GROUP BY order_id04HAVING COUNT(*) > 1;
  2. Decide whether they are real duplicates

    Look at a few of them. If the rows differ in a column that belongs to the key, add that column to the ON clause. If they are versions of the same record, keep the latest.

  3. Keep one row per key before the merge

    Rank the rows of each key by their change time and keep the first. The Databricks documentation describes this preprocessing: keep only the latest change for each key before it is applied to the target.

    Merge from the latest row of each key
    01 MERGE INTO sales.orders AS t02-USING updates AS s03+USING (04+  SELECT * FROM updates05+  QUALIFY ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY updated_at DESC) = 106+) AS s07 ON t.order_id = s.order_id08 WHEN MATCHED THEN UPDATE SET *09 WHEN NOT MATCHED THEN INSERT *;
  4. Fix the cause upstream, if there is one

    If a join in the source query multiplies rows, correct the join. If files are ingested twice, fix the ingestion so the same file is not loaded again.

  5. Repair the run

    Rerun the failed task with Repair run. The failed merge did not change the target table.

How to stop it coming back

  • Deduplicate every merge source by its key as a standard step, not only after a failure.
  • Order by a reliable change time or sequence number, so the latest row really is the latest.
  • For change feeds, consider the AUTO CDC APIs of Lakeflow pipelines, which handle out-of-order records for you.

With PipelineMender AI

The next failed run explains itself.

PipelineMender reads each failed task’s error and stack trace and the job settings through the Jobs API with Can View. For this failure the report identifies the merge from the task’s error, explains that the source holds more than one row per key, and proposes the deduplication for you to review.

Is the error in front of you now? Start a trial, choose Report an incident and paste it in. No connection is needed for that.

  • Read-only access
  • You review every change
  • No model training

Questions

Common questions about this error.

Why does MERGE not simply take the last row?

Because nothing in the statement says which row is last. By the SQL semantics of merge, several source rows matching one target row is ambiguous, so Delta stops instead of choosing for you.

Did the failed merge change my table?

No. The statement fails as a whole and the target table stays at its previous version.

Is MERGE_CARDINALITY_VIOLATION the same problem?

Yes. It reports the same situation: the ON condition matched one row of the target table with several rows of the source table.

Checked against: MERGE INTO Upsert into a Delta Lake table using merge Error conditions in Databricks QUALIFY clause Troubleshoot and repair job failures

Last reviewed 13 November 2026 against the Databricks documentation. PipelineMender AI is an independent product and is not affiliated with Microsoft or Databricks; product names are trademarks of their owners.