What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 23010 |  Welcome, Guest!   Register  Login
 Home > Blogs > SQL Server > How to delete duplicate rows in SQL Table? ...
Sourabh07

How to delete duplicate rows in SQL Table?

 Blog author: Sourabh07 | Posted on: 10/14/2012 | Category: SQL Server Blogs | Views: 720 | Status: [Member] | Points: 75 | Alert Moderator   


To delete the duplicate rows from the sql table... i am using temperately table and with clause to do so. Also for practice point of view, I want you to run the following script in your local database and do your Rn D's  to extract more knowledge from it.

Declare @temp table(
id int,
name varchar(50)
)

Insert into @temp (id,name) values (1,'A')
Insert into @temp (id,name) values (2,'B')
Insert into @temp (id,name) values (3,'C')
Insert into @temp (id,name) values (1,'A')
Insert into @temp (id,name) values (2,'B')

Select * from @temp;

With cust as (select ROW_NUMBER() over (partition by id,name order by id) as Row_Cnt,id,name from @temp)
delete from cust where Row_Cnt>1

Select * from @temp order by id

The Output will be:

id          name
----------- --------------------------------------------------
1           A
2           B
3           C
1           A
2           B



id          name
----------- --------------------------------------------------
1           A
2           B
3           C


The 1st table listing duplicate rows and the 2nd table listing the rows after deleting the duplicate rows.

so, the rows will be displayed in the second table are unique.

It is one of the most frequent question asked in an Interview. So, hopefully it will be helpful for all.







Sourabh07
Found interesting? Add this to:



 More Blogs from Sourabh07

     More ...

    About Sourabh 07

    Experience:1 year(s)
    Home page:http://www.dotnetfunda.com
    Member since:Saturday, October 13, 2012
    Level:Starter
    Status: [Member]
    Biography:Dot Net Developer
    >> Write Response - Respond to this post and get points

    More Blogs

    About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
    General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
    Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 2:58:51 AM