Click here to Skip to main content
Click here to Skip to main content

How to remove duplicate rows in SQL Server 2008 when no key is present

, 20 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip describes how to delete duplicate rows from a table that doesn't have a key.
An easy way to remove duplicate rows from a table in SQL Server 2008 is to use undocumented feature called %%physloc%%. This pseudo column shows the physical location of a row.
 
Note that this feature is undocumented and unsupported so use at your own risk!
 
A simple test-case. Create a test table:
CREATE TABLE TestTable (
   Column1 varchar(1),
   Column2 int
);
Add some rows with few duplicates:
INSERT INTO TestTable VALUES ('A', 1);
INSERT INTO TestTable VALUES ('A', 1); -- duplicate
INSERT INTO TestTable VALUES ('A', 2);
INSERT INTO TestTable VALUES ('B', 1);
INSERT INTO TestTable VALUES ('B', 2);
INSERT INTO TestTable VALUES ('B', 2); -- duplicate
INSERT INTO TestTable VALUES ('C', 2);
You can select the data to see that all seven rows are present:
SELECT *
FROM   TestTable a
ORDER BY a.Column1, a.Column2;
Now let's delete the two duplicates using the %%physloc%%:
DELETE
FROM  TestTable
WHERE TestTable.%%physloc%%
      NOT IN (SELECT MIN(b.%%physloc%%)
              FROM   TestTable b
              GROUP BY b.column1, b.Column2);
And if you run the query again you'll see that only five rows remain and duplicates have been deleted.
SELECT *
FROM   TestTable a
ORDER BY a.Column1, a.Column2;
 
For more information about %%physloc%%, see Physical location of a row in SQL Server[^].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mika Wendelius
Architect
Finland Finland
I've been a programmer since mid 80's using languages like assembler, C/C++, PL/I (mainframe environment), pascal, VB (I know, I know, no comments please) and C# and utilizing different techniques and tools.
 
However I'm specialized in databases and database modeling. Mostly I have used products like Oracle (from version 6), SQL Server (from version 4.2), DB2 and Solid Server (nowadays an IBM product).
 
For the past 10+ years my main concerns have been dealing with different business processes and how to create software to implement and improve them. At my spare time (what ever that actually means) I'm also teaching and consulting on different areas of database management, development and database oriented software design.

Comments and Discussions

 
QuestionGreat PinmemberAn2dSingh8-Feb-13 2:06 
QuestionNo need PinmemberMartinSmith10000009-Jun-12 1:53 
GeneralMy vote of 5 Pinmembermanoj kumar choubey17-Apr-12 1:07 
GeneralRe: My vote of 5 PinmvpMika Wendelius17-Apr-12 4:32 
GeneralReason for my vote of 5 I didn't know that trick too Pinmemberbeginner20115-Sep-11 16:01 
GeneralRe: Thanks PinmvpMika Wendelius5-Sep-11 18:31 
GeneralReason for my vote of 5 This is something everybody should k... Pinmemberjonathanbeyre22-Feb-11 5:07 
GeneralRe: Thank you PinmvpMika Wendelius3-Aug-11 9:43 
GeneralReason for my vote of 5 I didn't know that trick :) PinmemberJcmorin22-Feb-11 1:20 
GeneralRe: Thanks PinmvpMika Wendelius3-Aug-11 9:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411022.1 | Last Updated 20 Feb 2011
Article Copyright 2011 by Mika Wendelius
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid