
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; sql</title>
	<atom:link href="https://www.nikeshshk.com.np/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.nikeshshk.com.np</link>
	<description></description>
	<lastBuildDate>Tue, 04 Nov 2025 10:25:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.4</generator>
	<item>
		<title>Remove Duplicate Rows from a Table in SQL Server</title>
		<link>https://www.nikeshshk.com.np/news/remove-duplicate-rows-from-a-table-in-sql-server/</link>
		<comments>https://www.nikeshshk.com.np/news/remove-duplicate-rows-from-a-table-in-sql-server/#comments</comments>
		<pubDate>Mon, 21 Apr 2014 08:01:54 +0000</pubDate>
		<dc:creator><![CDATA[Nikesh Shakya]]></dc:creator>
				<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[remove duplicates]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlserver]]></category>

		<guid isPermaLink="false">http://www.nikeshshk.com.np/?p=670</guid>
		<description><![CDATA[Firstly, we will create a table, where we will insert some duplicate rows to understand the topic properly. Create a table called ATTENDANCE by using the following code: CREATE TABLE [dbo].[ATTENDANCE]( [EMPLOYEE_ID] [varchar](50) NOT NULL, [ATTENDANCE_DATE] [date] NOT NULL ) ON [PRIMARY] Now insert some data into this table. INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES ('A001',CONVERT(DATETIME,'01-01-11',5)) INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES ('A001',CONVERT(DATETIME,'01-01-11',5)) INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES ('A002',CONVERT(DATETIME,'01-01-11',5)) INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES ('A002',CONVERT(DATETIME,'01-01-11',5)) INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES ('A002',CONVERT(DATETIME,'01-01-11',5)) INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES ('A003',CONVERT(DATETIME,'01-01-11',5)) After inserting the data, check the data of the below table. If we grouped the employee_id and attendance_date, then A001 and A002 become duplicates. EMPLOYEE_ID ATTENDANCE_DATE A001 2011-01-01 A001 2011-01-01 A002 2011-01-01 A002 2011-01-01 A002 2011-01-01 A003 2011-01-01 So how can we delete those duplicate data? Solution First, insert an identity column in that table by using the following code: ALTER TABLE dbo.ATTENDANCE ADD AUTOID INT IDENTITY(1,1) Now the table data will be like the following table: EMPLOYEE_ID ATTENDANCE_DATE AUTOID A001&#160;<a href="https://www.nikeshshk.com.np/news/remove-duplicate-rows-from-a-table-in-sql-server/" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>Firstly, we will create a table, where we will insert some duplicate rows to understand the topic properly. Create a table called ATTENDANCE by using the following code:</p>
<p><code>CREATE TABLE [dbo].[ATTENDANCE](<br />
[EMPLOYEE_ID] [varchar](50) NOT NULL,<br />
[ATTENDANCE_DATE] [date] NOT NULL<br />
) ON [PRIMARY] </code></p>
<p>Now insert some data into this table.</p>
<p><code>INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES<br />
('A001',CONVERT(DATETIME,'01-01-11',5))<br />
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES<br />
('A001',CONVERT(DATETIME,'01-01-11',5))<br />
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES<br />
('A002',CONVERT(DATETIME,'01-01-11',5))<br />
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES<br />
('A002',CONVERT(DATETIME,'01-01-11',5))<br />
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES<br />
('A002',CONVERT(DATETIME,'01-01-11',5))<br />
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES<br />
('A003',CONVERT(DATETIME,'01-01-11',5)) </code></p>
<p>After inserting the data, check the data of the below table. If we grouped the employee_id and attendance_date, then A001 and A002 become duplicates.<br />
EMPLOYEE_ID ATTENDANCE_DATE<br />
A001 2011-01-01<br />
A001 2011-01-01<br />
A002 2011-01-01<br />
A002 2011-01-01<br />
A002 2011-01-01<br />
A003 2011-01-01</p>
<p>So how can we delete those duplicate data?<br />
Solution</p>
<p>First, insert an identity column in that table by using the following code:</p>
<p><code>ALTER TABLE dbo.ATTENDANCE ADD AUTOID INT IDENTITY(1,1) </code></p>
<p>Now the table data will be like the following table:<br />
EMPLOYEE_ID ATTENDANCE_DATE AUTOID<br />
A001 2011-01-01 1<br />
A001 2011-01-01 2<br />
A002 2011-01-01 3<br />
A002 2011-01-01 4<br />
A002 2011-01-01 5<br />
A003 2011-01-01 6</p>
<p>Check the AUTOID column. Now we will start playing the game with this column.</p>
<p>Now use the following code to find out the duplicate rows that exist in the table.</p>
<p><code>SELECT * FROM dbo.ATTENDANCE WHERE AUTOID NOT IN (SELECT MIN(AUTOID) FROM dbo.ATTENDANCE GROUP BY EMPLOYEE_ID,ATTENDANCE_DATE)</code></p>
<p>The above code will give us the following result:<br />
EMPLOYEE_ID ATTENDANCE_DATE AUTOID<br />
A001 2011-01-01 2<br />
A002 2011-01-01 4<br />
A002 2011-01-01 5</p>
<p>Ultimately, these are the duplicate rows which we want to delete to resolve the issue. Use the following code to resolve it.</p>
<p><code>DELETE FROM dbo.ATTENDANCE WHERE AUTOID NOT IN (SELECT MIN(AUTOID) FROM dbo.ATTENDANCE GROUP BY EMPLOYEE_ID,ATTENDANCE_DATE) </code></p>
<p>Now check the data. No duplicate rows exist in the table.</p>
<p>Is it too complicated? I dont think so <img src="https://www.nikeshshk.com.np/wp-includes/images/smilies/simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
			<wfw:commentRss>https://www.nikeshshk.com.np/news/remove-duplicate-rows-from-a-table-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
