ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Databases  >>  SQL Server
 
 


 

 
 Oracle interview questions  Oracle Interview Questions
 SQL Server interview questions  SQL Server Interview Questions
 MS Access interview questions  MS Access Interview Questions
 MySQL interview questions  MySQL Interview Questions
 Postgre interview questions  Postgre Interview Questions
 Sybase interview questions  Sybase Interview Questions
 DB Architecture interview questions  DB Architecture Interview Questions
 DB Administration interview questions  DB Administration Interview Questions
 DB Development interview questions  DB Development Interview Questions
 SQL PLSQL interview questions  SQL PLSQL Interview Questions
 Databases AllOther interview questions  Databases AllOther Interview Questions
Question
How to retrieve duplicate rows in a table?
How to delete the duplicate entries in a table?
 Question Submitted By :: Harry
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 1
1.
Select from <table name A>
where rowid > (select min(rowid) from <table name B>
where A.keyvalues=B.keyvalues);


2.
Delete from <table name A>
where rowid > (select min(rowid) from <table name B>
where A.keyvalues=B.keyvalues);
 
Is This Answer Correct ?    6 Yes 4 No
Anil Kumar Karasi
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 2
///////////Cursor for delete duplicate record in 
table//////////

DECLARE @ID int
DECLARE @NAME NVARCHAR(50)
DECLARE @COUNT int 

DECLARE CUR_DELETE CURSOR FOR
SELECT [ID],[NAME],COUNT([ID]) FROM [Example] GROUP BY [ID],
[NAME] HAVING COUNT([ID]) > 1 

OPEN CUR_DELETE 

FETCH NEXT FROM CUR_DELETE INTO @ID,@NAME,@COUNT
/* Loop through cursor for remaining ID */
WHILE @@FETCH_STATUS = 0
BEGIN 

DELETE TOP(@COUNT -1) FROM [Example] WHERE ID = @ID 

FETCH NEXT FROM CUR_DELETE INTO @ID,@NAME,@COUNT
END 

CLOSE CUR_DELETE
DEALLOCATE CUR_DELETE 

Mohit D Jethva
 
Is This Answer Correct ?    4 Yes 2 No
Mohit D Jethva
 
 
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 3
delete from employee 
where Emp_id not in (select min(Emp_id) from employee 
group by Emp_Name)
 
Is This Answer Correct ?    2 Yes 7 No
Pawan K. Dubey
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 4
/* selecting duplicate rows in a table */
select col1, col2, ..., colN, count(*)
from TableName
group by col1, col2, ..., colN
having count(*) > 1

/* deleting duplicate rows from a table */
select col1, col2, ..., colN, count(*) as "Duplicates"
into #duplicates
from TableName
group by col1, col2, ..., colN
having count(*) > 1

delete TableName
from TableName t, #duplicates d
where t.col1 = d.col1 and
  ....
      t.colN = d.colN

/* damn I'm good! */
 
Is This Answer Correct ?    2 Yes 3 No
Skybeaver
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 5
drop table #TEMP select distinct * into #TEMP from 
TABLE_NAME delete from TABLE_NAME insert into TABLE_NAME 
select * from #TEMP
 
Is This Answer Correct ?    0 Yes 2 No
Arun Kumar K S
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 6
Ans for 1st Query
How to retrieve duplicate rows in a table?

SELECT * FROM EMP1 WHERE (EMP_ID IN (SELECT emp_id FROM 
emp1 GROUP BY emp_id  HAVING COUNT(emp_id) > 1))
 
Is This Answer Correct ?    1 Yes 1 No
Dilip
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 7
--select * from #TempR
select * into #temp2 from #TempR

--  select * from #temp2

alter table #temp2 add record_id numeric(5,0) identity not 
null

/*        select those row which are repeated           */

  select * into #qwe
  from #temp2 
  where exists(
  
  select null from #temp2 b
  where b.ID = #temp2.ID
  and b.TYPE = #temp2.TYPE
  group by b.ID, b.TYPE
  having 
  count (*) >=2
  )

  --select * from #qwe

  


/*               delete those row which are repeted     */

delete from #TempR where ID in ( select ID from #qwe)

/*               insert those row which are deleted     */

delete from #qwe where record_id not in (  

    select record_id
  from #qwe
  group by ID, TYPE
  having record_id = max (record_id)
)

 -- select * from #qwe
alter table #qwe drop record_id

insert into #TempR
select * from #qwe 

/*             see output     */


select * from #TempR

/*         check for row getting repeted         */


select * 
  from #TempR 
  where exists(
  
  select null from #TempR b
  where b.ID = #TempR.ID
  and b.TYPE = #TempR.TYPE
  group by b.TT_ID, b.EQP_TYPE
  having 
  count (*) >=2
  )
 
Is This Answer Correct ?    1 Yes 1 No
Sameer
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 8
begin 
select distinct * into #one  from four  where id in (select 
id from four group by id 
having count(*)>1)
delete from four where id in (select id from four group by 
id   having count(*)>1)
insert into four select * from #one
end
 
Is This Answer Correct ?    0 Yes 2 No
Gaurav Jain
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 9
Tbale emp had some duplicate entries and i wanted to retain 
the first of all duplicates, the others could be deleted as 
follows . Please give your comments if this is the most 
optimum way :

delete from emp where rowid in 
(select rowid from emp o where rowid != 
 (select min(rowid) from emp i where i.empno=o.empno));
 
Is This Answer Correct ?    0 Yes 1 No
Anoop Rajan
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 10
select ROW_NUMBER() OVER (ORDER BY names ASC) AS ROWID, *  
into #temp from emp
select * from #temp where ROWID not in(
select b.ROWID from 
(
select ROW_NUMBER() OVER (ORDER BY names ASC) AS ROWID, * 
from emp 
except
SELECT ROW_NUMBER() OVER (ORDER BY names ASC) AS ROWID, * 
FROM 
(
select names , sal from emp
union 
select   distinct names,sal  from emp) as a ) as b)

drop table #temp
 
Is This Answer Correct ?    1 Yes 0 No
Madhur/amrutha
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 11
Consider a table emp with employee details. The Correct 
code to retrieve duplicate rows :


select distinct * from emp where names in
(select names from emp group by names having count(sal)>1)
 
Is This Answer Correct ?    1 Yes 0 No
Madhur/amrutha
 
  Re: How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?
Answer
# 12
These are duplicates....

select distinct(col1),col2,col3.... into #temp from table 
group by col1 having count(1) > 1

by the below delete duplicates
delete table from table A, #temp B where A.col1= B.col1

by this insert only a single record
insert into table select col1, col2,col3... from #temp
 
Is This Answer Correct ?    0 Yes 0 No
Nagabhushan Adhikari
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
how many types of store procedre in sqlserver 2000? ATI2
write query for fourth maximum salary from employee table Mind-Tree5
is it possible to use a variable in a query with the IN clause (a,b,c..z), without getting quotes or conversion errors?  2
After using delete statement in sql query to delete some records...to retrieve the deleted records we can get using rollback command but till that where it stores means particular location name i need....(after deleting and rollback ) iGate2
What all db objects can be found in MSDB database of a SQL Server instance? Accenture1
Say if we have a table that contains only a single column , say OrderID, which has IDENTITY attribute defined on it. So how can we insert data in this table. I am reframing my question, that how can we make the table to increment the column "OrderID" value several times???  3
How do we get current date in SQL Server 2000, Oracle, MS Access?  11
What should we do to copy the tables, schema and views from one SQL Server to another?  3
What value could be assigned to Varchar Type?  3
how to get 25th row in any table in sqlserver can u tell me syntax  4
What is database replicaion? What are the different types of replication you can set up in SQL Server? HCL4
How to Run a Query on a Remote SQL Server?  2
Is it possible to create a stored procedure that runs a query and outputs the results to a text file and allows me to add extra delimeters and static field info. If so How?  1
What r sql reporting services and analysis services? how can we use it. Microsoft2
how can u select the Distinct values in the table, table having 20 columns , i want all columns  1
What is normalization?  6
What are constraints? Explain different types of constraints?  6
What are cursors?  8
What is row by row processing ?  1
Please give me the SP for the below scenario. I have two tables named Table1 and Table2...I need to fetch record by record from Table1 and insert the record in to table2 where the value in the sno column of the table1 is even number. Value-Labs4
 
For more SQL Server Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com