4

For instance, I have a table say "Name" with duplicate records in it:

Id    |  Firstname
--------------------
1     |  John
2     |  John
3     |  Marc
4     |  Jammie
5     |  John
6     |  Marc

How can I fetch duplicate records and display them with their receptive primary key ID?

0

2 Answers 2

10

Use Count()Over() window aggregate function

Select * from
(
select Id, Firstname, count(1)over(partition by Firstname) as Cnt
from yourtable
)a
Where Cnt > 1
Sign up to request clarification or add additional context in comments.

Comments

3
SELECT t.*
FROM t
INNER JOIN
(SELECT firstname
 FROM t
 GROUP BY firstname
 HAVING COUNT(*) > 1) sub
ON t.firstname = sub.firstname

A sub-query would do the trick. Select the first names that are found more than once your table, t. Then join these names back to the main table to pull in the primary key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.