---
title: "How can I remove duplicate rows in SQL Server?"  
description: "How can I remove duplicate rows in SQL Server?"  
author: "Revati S Misra"  
published: 2023-07-11  
updated: 2023-07-12  
canonical: https://www.mindstick.com/forum/159034/how-can-i-remove-duplicate-rows-in-sql-server  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# How can I remove duplicate rows in SQL Server?

How can I [remove duplicate](https://www.mindstick.com/forum/157765/how-to-remove-duplicate-rows-in-sql) rows in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server)?

## Replies

### Reply by Aryan Kumar

There are a few ways to [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) [duplicate](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server) rows in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over). Here are some of the most common methods:

- **Using the** `DISTINCT` **keyword**

The `DISTINCT` keyword can be used to select only the distinct rows from a table. For example, the following code will select all of the distinct rows from the `Customers` table:

SQL

```plaintext
SELECT DISTINCT *
FROM Customers;
```

This code will remove all of the duplicate rows from the `Customers` table.

- **Using the** `ROW_NUMBER()` **function**

The `ROW_NUMBER()` function can be used to assign a unique number to each row in a table. For example, the following code will assign a unique number to each row in the `Customers` table:

SQL

```plaintext
SELECT ROW_NUMBER() OVER (ORDER BY CustomerID) AS RowNumber, *
FROM Customers;
```

This code will create a new column called `RowNumber` that contains a unique number for each row in the `Customers` table. You can then use the `RowNumber` column to filter out the duplicate rows. For example, the following code will delete all of the duplicate rows from the `Customers` table:

SQL

```plaintext
DELETE FROM Customers
WHERE RowNumber > 1;
```

This code will delete all of the rows in the `Customers` table where the `RowNumber` column is greater than 1. This will effectively remove all of the duplicate rows from the table.

- **Using the** `GROUP BY` **clause**

The `GROUP BY` clause can be used to group rows together based on a common value. For example, the following code will group the rows in the `Customers` table together by the `CustomerID` column:

SQL

```plaintext
SELECT CustomerID, COUNT(*) AS NumCustomers
FROM Customers
GROUP BY CustomerID;
```

This code will create a new column called `NumCustomers` that contains the number of rows in each group. You can then use the `NumCustomers` column to filter out the duplicate rows. For example, the following code will delete all of the duplicate rows from the `Customers` table:

SQL

```plaintext
DELETE FROM Customers
WHERE NumCustomers > 1;
```

This code will delete all of the rows in the `Customers` table where the `NumCustomers` column is greater than 1. This will effectively remove all of the duplicate rows from the table.


---

Original Source: https://www.mindstick.com/forum/159034/how-can-i-remove-duplicate-rows-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
