How to Remove Duplicate Rows in SQL Server

Last updated July 25, 2026 · By the SaturnSQL team

Rank rows with ROW_NUMBER() OVER (PARTITION BY the duplicate-defining columns) inside a CTE, then DELETE where the row number is greater than 1. Deleting from the CTE deletes from the underlying table.

WITH ranked AS (
  SELECT ROW_NUMBER() OVER (
    PARTITION BY email
    ORDER BY id
  ) AS rn
  FROM customers
)
DELETE FROM ranked
WHERE rn > 1;

The ORDER BY inside OVER decides which duplicate survives; ORDER BY id keeps the oldest row. Add a unique constraint or index afterwards so the duplicates cannot come back.

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Related SQL Server guides

© 2026 Panda Capital Oy Ab. All rights reserved.