Sub topic 14
Removing Duplicate Rows with ROW_NUMBER
Assume you know basic SELECT, WHERE, and ORDER BY, understand GROUP BY, and can use an inline view, which is a subquery in the FROM clause.
Duplicate rows have the same value for the columns that define identity. For example, a customer table might contain several rows with the same email address. To keep one row from each email group, first number the rows, then delete every row whose number is greater than one.
Define which row to keep
ROW_NUMBER assigns a different number to every row in each partition. Use PARTITION BY to define a duplicate group. Use the window ORDER BY to decide which row is first.
In this example, the oldest customer is kept. customer_id is a tiebreaker when two rows have the same created_at.
SELECT customer_id,
email,
created_at,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY created_at, customer_id
) AS rn
FROM customers
ORDER BY email, rn;The result might look like this:
| customer_id | created_at | rn | |
|---|---|---|---|
| 101 | [email protected] | 2025-01-03 | 1 |
| 108 | [email protected] | 2025-02-14 | 2 |
| 115 | [email protected] | 2025-01-20 | 1 |
Rows with rn = 1 are the rows to keep. Rows with rn > 1 are duplicates according to the chosen rule.
Delete the extra rows
Oracle provides ROWID, a pseudocolumn identifying a physical row location. Select the ROWID inside an inline view, calculate rn, and delete the row locations whose number is greater than one.
DELETE FROM customers
WHERE rowid IN (
SELECT rid
FROM (
SELECT rowid AS rid,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY created_at, customer_id
) AS rn
FROM customers
)
WHERE rn > 1
);The inner query calculates the row number. The middle query exposes only the row locations marked for deletion. The outer DELETE removes those rows.
If the table has a primary key, deleting by that key is often clearer:
DELETE FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM (
SELECT customer_id,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY created_at, customer_id
) AS rn
FROM customers
)
WHERE rn > 1
);Review the affected rows before committing. In a transaction, you can use ROLLBACK if the result is wrong, or COMMIT when the deletion is confirmed.
Important edge cases
NULLemails form one analytic partition. AddWHERE email IS NOT NULLif rows with missing emails must not be grouped together.- If two rows have identical values in every
ORDER BYcolumn, Oracle can choose either row as number one. Add a unique column, such ascustomer_id, to make the result deterministic. ROWIDcan change after operations such as moving or rebuilding a table. It is suitable for this single statement, not as a permanent identifier.- Prevent future duplicates with a suitable unique constraint or unique index after cleaning the data.
Exercise
You want to keep the newest row for each email instead of the oldest. How should the window ORDER BY change?
Show answer
Order by createdat DESC, customerid DESC, so the newest row receives rn = 1. Use a unique tiebreaker that matches the desired rule.