How to delete duplicate rows in SQL Server

0 votes

How will I be able to delete the duplicate rows where no unique row id exists?

My table is

col1 col2 col3 col4 col5 col6 col7 
john 1 1 1 1 1 1 
john 1 1 1 1 1 1 
sally 2 2 2 2 2 2 
sally 2 2 2 2 2 2

I want to be left with the following after the duplicate removal:

john 1 1 1 1 1 1 
sally 2 2 2 2 2 2

I also tried a few queries but I think they depend on having a row id as I don't get the desired result. For example:

DELETE 
FROM table 
WHERE col1 IN ( 
          SELECT id 
          FROM table 
          GROUP BY id 
          HAVING (COUNT(col1) > 1)
)
Feb 10 in Others by Rahul
• 9,680 points
262 views

1 answer to this question.

0 votes

To answer your query, note that CTEs and ROW_NUMBER are the both combined, which allow us to see which rows are deleted and hence, just make the change to the DELETE FROM CTE... to SELECT * FROM CTE:

WITH CTE AS( 
      SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7], 
                RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1) 
      FROM dbo.Table1 
) 

DELETE FROM CTE WHERE RN > 1

The DEMO result is different and hence, I assume that it's due to a typo on your part.
 

COL1 COL2 COL3 COL4 COL5 COL6 COL7 
john 1 1 1 1 1 1 
sally 2 2 2 2 2 2

This example determines duplicates by a single column col1 because of the PARTITION BY col1. If you want to include multiple columns simply add them to the PARTITION BY:


ROW_NUMBER()OVER(PARTITION BY Col1, Col2, ... ORDER BY OrderColumn)
answered Feb 10 by Soham
• 9,670 points

Related Questions In Others

0 votes
1 answer

How to delete duplicate rows in SQL Server?

WITH CTE AS ( SELECT *,ROW_NUMBER() OVER (PARTITION BY ...READ MORE

answered Jun 21 in Others by nisha
• 2,190 points
63 views
0 votes
1 answer

Convert Rows to columns using 'Pivot' in SQL Server

If you are using SQL Server 2005+, ...READ MORE

answered Jun 20 in Others by nisha
• 2,190 points
561 views
0 votes
1 answer

How to import excel file in Oracle SQL live

Hello, there are a few steps You'll ...READ MORE

answered Feb 18 in Others by gaurav
• 22,040 points
581 views
0 votes
1 answer

How to delete duplicate rows in SQL Server?

CTEs and ROW_NUMBER can be combined together which will ...READ MORE

answered Feb 10 in Database by Vaani
• 7,020 points
168 views
0 votes
0 answers

How can I delete using INNER JOIN with SQL Server?

In SQL Server 2008, I want to ...READ MORE

Aug 23 in Database by Kithuzzz
• 20,660 points
90 views
0 votes
1 answer

How to duplicate IDs with Hive QL / Impala / Python

Based on your example, supposing id2 always ...READ MORE

answered Oct 1, 2018 in Python by Priyaj
• 58,100 points
361 views
0 votes
1 answer

Rename column SQL Server 2008

Use sp_rename EXEC sp_RENAME 'TableName.PreviousColumnName' , 'NewColumnName', 'COLUMN' See: SQL SERVER ...READ MORE

answered Feb 23 in Database by Vaani
• 7,020 points
97 views
0 votes
1 answer

How to take a screenshot of a current Activity and then share it?

For me, I captured and then shared ...READ MORE

answered Feb 8 in Others by Soham
• 9,670 points
55 views
0 votes
1 answer

Unable to detect adb version, adb output in Android Studio 3.3.2

To avoid this ERROR, start with changing ...READ MORE

answered Feb 11 in Others by Soham
• 9,670 points
729 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP