Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the duplicate values in my table on multiple columns.

But the output looks like

Name city Name city
chandu america chandu america
chandu america chandu america
tom UK tom UK
tom UK tom UK

How can i show only two columns? Is there any easy way of finding all duplicate records?

What I have tried:

select * from EMP a join(select Name,city from EMP group by Name,city having count (*)>1)b
on a.Name=b.Name
and a.city=b.city
Posted
Updated 26-Jun-18 19:41pm

You need only this:
SQL
SELECT Name, city
FROM EMP
GROUP BY Name, city
HAVING COUNT(*)>1


Another SELECT statement is redundant!
 
Share this answer
 
To show only two colums, reduce the columns you select:
SQL
select a.Name, a.city from EMP a join(select Name,city from EMP group by Name,city having count (*)>1)b
on a.Name=b.Name
and a.city=b.city


To show distinct results only:
SQL
select distinct a.Name, a.city from EMP a join(select Name,city from EMP group by Name,city having count (*)>1)b
on a.Name=b.Name
and a.city=b.city
 
Share this answer
 
To find duplicate records
select Name,city,count(*) Cnt from TableName group by Name,city having count(*)>1

To delete duplicate records from table
;with del as
(select row_number() over(partition by Name,city order by Name,city) as rownumber,* from TableName)
delete from del where rownumber >1
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900