Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Database >> Remove Delete Duplicate Records or Rows - SQL Server
Remove Delete Duplicate Records or Rows - SQL Server
Remove Delete Duplicate Records or Rows - SQL Server


by mohan kumar on 1/7/2012 1:20:49 PM

Views: 2141       Comments: 0

Remove Delete Duplicate Records or Rows - SQL Server

Hey Guys!! In this article, I will be explaining you, how to delete the duplicate records from the database table.

 

Remove Delete Duplicate Records or Rows - SQL Server

 

Remove or Delete duplicate records or rows from ms SQL server database table.

In this post i am going to describe different methods of deleting duplicate records or rows from sql server database table.

I am using Employees table with FirstName and Department columns.


Remove Delete Duplicate Records or Rows - SQL Server

First Method.

Delete duplicate records/rows by creating identity column.


Duplicate records in table looks like shown in first image.

First of all we need to create a identity column in our table by using code mentioned below.

And table will look like image on the left.

ALTER TABLE dbo.Employees ADD ID INT IDENTITY(1,1)


Now write this query to delete duplicate rows.

DELETE FROM dbo.Employees

WHERE ID NOT IN (SELECT MIN(ID)

FROM dbo.Employees GROUP BY FirstName,Department)


This should remove all duplicate records from table.


Second Method:-

Remove duplicate rows/Records using temporary table.


Use below mentioned code to delete duplicates by moving them to temporary table using DISTINCT.

SELECT DISTINCT * INTO TempTable FROM dbo.Employees

GROUP BY FirstName,Department

HAVING COUNT(FirstName) > 1

 

DELETE dbo.Employees WHERE FirstName

IN (SELECT FirstName FROM TempTable)

 

INSERT dbo.Employees SELECT * FROM TempTable

DROP TABLE TempTable

 

Remove Delete Duplicate Records or Rows - SQL Server

 

And result will be as shown.

Just post back a reply if this article was useful for you. Thanks for reading my article. Happy Coding!! Explore more and more as much as possible.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by mohan kumarRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5785
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.