Delete duplicate rows from a table without primary key by
using a single query
Table Employee
empname salary
A 200
B 300
A 200
C 400
D 500
D 500

Output should be

A 200
B 300
C 400
D 500

Answers were Sorted based on User's Feedback



Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / swati tripathi

declare myCursor cursor for
select empid
from employee
group by empid
having count(*) > 1

declare @EmpId int


OPEN myCursor;
FETCH NEXT FROM myCursor INTO @EmpId
WHILE @@FETCH_STATUS = 0
BEGIN

delete top(select count(*)-1 from employee where
empid=@EmpId) from employee where empid=@EmpId

FETCH NEXT FROM myCursor INTO @EmpId
END
close myCursor

deallocate myCursor

Is This Answer Correct ?    11 Yes 0 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / sumathy

Execute the following query for each duplicate value.

1.Delete duplicate of empname='A'

delete top(select count(*)-1 from employee where
empname='A') from employee where empname='A'

2.Delete duplicate of empname='D'

delete top(select count(*)-1 from employee where
empname='D') from employee where empname='D'

Is This Answer Correct ?    9 Yes 2 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / navneet sharma

select distinct * from tablename

Is This Answer Correct ?    4 Yes 1 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / honey

with myCTE as(
select row_number() over( partition by empname order by
empname) as myCount from Employee
)delete from myCTE where myCount >1

Is This Answer Correct ?    3 Yes 0 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / sunil

set rowcount 1 -- set row count 1

delete a from Employee a
where (select count(*) from Employee e where e.empname =
a.empname) > 1

while @@rowcount > 0
begin
delete a from Employee a
where (select count(*) from Employee e where e.empname =
a.empname) > 1
end
set rowcount 0

Is This Answer Correct ?    2 Yes 0 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / pradeep

DECLARE @TEMP_TABLE TABLE (EMPNAME VARCHAR(10),SALARY
VARCHAR(10))

INSERT INTO @TEMP_TABLE (EMPNAME ,SALARY )
SELECT 'A','200' UNION ALL
SELECT 'B','300' UNION ALL
SELECT 'C','400' UNION ALL
SELECT 'A','200' UNION ALL
SELECT 'A','200' UNION ALL
SELECT 'D','500' UNION ALL
SELECT 'D','500' UNION ALL
SELECT 'B','300'

/* TABLE RAW DATA */
SELECT * FROM @TEMP_TABLE

SELECT * FROM @TEMP_TABLE AS A
WHERE (SELECT COUNT(*) FROM @TEMP_TABLE AS B WHERE
A.EMPNAME=B.EMPNAME AND A.SALARY=B.SALARY) > 1

/* OUTPUT SHOULD BE */
SELECT DISTINCT * FROM @TEMP_TABLE

/* DELETE DUPLICATE/TRIPLICATE.... RECORDS */
SET ROWCOUNT 1

DELETE @TEMP_TABLE FROM @TEMP_TABLE AS A
WHERE (SELECT COUNT(*) FROM @TEMP_TABLE AS B WHERE
A.EMPNAME=B.EMPNAME AND A.SALARY=B.SALARY) > 1
WHILE @@ROWCOUNT > 0
DELETE @TEMP_TABLE FROM @TEMP_TABLE AS A
WHERE (SELECT COUNT(*) FROM @TEMP_TABLE AS B WHERE
A.EMPNAME=B.EMPNAME AND A.SALARY=B.SALARY) > 1

SET ROWCOUNT 0
/**********************************************/
/* EXPECTED OUTPUT TABLE */
SELECT * FROM @TEMP_TABLE

Is This Answer Correct ?    1 Yes 0 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / shankaranarayanan v

while exists(select count(*) from employee group by empname having count(*)>1)
begin

delete top(1) from employee where empname in
(
select min(empname) as deletedname
from employee
group by empname
having count(*)>1
)

end

Is This Answer Correct ?    1 Yes 0 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / devender kumar

This query is for sql server 2005 and higher version of sql
server. It will not run on older versions.



with myCTE as(
select row_number() over( partition by empname order by
empname) as myCount from Employee
)delete from myCTE where myCount >3

Is This Answer Correct ?    1 Yes 1 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / gaurav sharma

SELECT DISTINCT * INTO Employee1 FROM Employee
DROP Table Employee
sp_rename 'Employee1','Employee'

Is This Answer Correct ?    0 Yes 1 No

Delete duplicate rows from a table without primary key by using a single query Table Employee em..

Answer / samba shiva reddy . m

delclare @temp(empname varchar(50),sal int)
select * into @temp
FROM employee
GROUP BY empname, salary
HAVING count(*) > 1
delete from employee
select * into employee from @temp as temp

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More SQL Server Interview Questions

How to a Query to copy data from on table to another table.

6 Answers   Cap Gemini,


Difference between Cluster and Non-cluster index?

32 Answers   Accenture, Agility e-Services, eClinicalWorks, HCL, Infosys, Oracle, Satyam, Yardi,


What is the difference between Stored Procedures and triggers?

0 Answers   HCL,


What is difference between inner join and full join?

0 Answers  


How to find the source of a table in sql server?

0 Answers  


Explain about builtinadministrator?

0 Answers  


What is a materialized view?

3 Answers  


What is bulkcopy in sql?

0 Answers  


What are the different types of columns types constraints in the sql server?

0 Answers  


What is the maximum size of column in sql server?

0 Answers  


In my application I have a process which picks the scanned files (tif format) from a shared location and it links to application and shown on it.The actuall issue is that my process picks the file before it is completly written or scanned which results in displaying few parts of the image or incomplete image.I need to check if the file is not completly scanned or written then do not link it to application.Please help if any body tell me that how can i check that file is in written phase or locked through DTS.thanking you in advance

0 Answers  


What is an inner join?

0 Answers  


Categories