---
title: "How to remove duplicate rows in SQL?"  
description: "How to remove duplicate rows in SQL?"  
author: "Revati S Misra"  
published: 2023-04-12  
updated: 2023-11-26  
canonical: https://www.mindstick.com/forum/157765/how-to-remove-duplicate-rows-in-sql  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How to remove duplicate rows in SQL?

How to [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) [duplicate rows in SQL](https://www.mindstick.com/forum/159034/how-can-i-remove-duplicate-rows-in-sql-server)?

## Replies

### Reply by Aryan Kumar

To [remove duplicate](https://www.mindstick.com/forum/33797/how-to-remove-duplicate-url-from-google) rows from a table in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database), you can use the **DISTINCT** keyword or the **GROUP BY** clause. Here are two common approaches:

### Using DISTINCT:

If you want to select distinct rows based on all columns, you can use the **DISTINCT** keyword:

```plaintext
SELECT DISTINCT *
FROM your_table;
```

This query will return only distinct rows across all columns in the specified table.

### Using GROUP BY:

If you want to remove duplicates based on specific columns, you can use the **GROUP BY** clause:

```plaintext
SELECT column1, column2, ..., columnN
FROM your_table
GROUP BY column1, column2, ..., columnN;
```

Replace **column1, column2, ..., columnN** with the columns you want to consider for uniqueness. This query will return one row for each unique combination of the specified columns.

### Removing Duplicates and Keeping One Copy:

If you want to delete the [duplicate rows](https://www.mindstick.com/forum/34367/delete-duplicate-rows-in-sql) and keep only one copy, you can use the **DELETE** statement with a common table expression (CTE) and the **ROW_NUMBER()** window function:

```plaintext
WITH CTE AS (
  SELECT
    column1, column2, ..., columnN,
    ROW_NUMBER() OVER (PARTITION BY column1, column2, ..., columnN ORDER BY (SELECT NULL)) AS RowNum
  FROM your_table
)
DELETE FROM CTE WHERE RowNum > 1;
```

In this example, the **ROW_NUMBER()** function assigns a unique number to each row within its partition. The **PARTITION BY** clause specifies the columns for determining duplicates. The **DELETE** statement then removes rows with **RowNum** greater than 1, effectively keeping only one copy of each unique combination.

Remember to replace **column1, column2, ..., columnN** with the actual column names in your table. Also, be cautious when performing deletions, especially if the table contains important data. Consider taking a backup before making changes, or test the query on a smaller dataset first.


---

Original Source: https://www.mindstick.com/forum/157765/how-to-remove-duplicate-rows-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
