0

I am learning the SQL code and don't know how to find the duplicate row and show it out. Here is the table

person_no name birthday
01 John 01/01/2000
02 John 15/03/2000
03 Marry 21/06/2000
04 Peter 23/12/2000
05 Jerry 12/07/2000

The person_no is unique for everyone, but the name may be same, anyway to find the people with same name but different person_no ?

Sorry if my question bothers you

6
  • Sample table data is great, but you should also specify the expected result. Commented Sep 30, 2021 at 9:45
  • My apologies. The expected result is list all the personal detail of everyone who shares the same name with someone Commented Sep 30, 2021 at 10:05
  • Is it just the group by function you are asking about ? explained here Commented Sep 30, 2021 at 10:15
  • I think I am looking for a compare function between rows? And list the compare result, in which, the same in person's name Commented Sep 30, 2021 at 10:26
  • You should also specify the expected result, not describe it. Commented Sep 30, 2021 at 11:03

1 Answer 1

0
select * from person_table
join(
    select name from person_table
    group by name
    having count(person_no) > 1
) temp on person_table.name = temp.name;

First, find out the duplicate names from the subquery. Then using an inner join, find the rows in the table that have the names from the subquery result.

Without join:

select * from person_table
where name in (
    select name from person_table
    group by name
    having count(person_no) > 1
)

This should also give you the rows with duplicate values is the column name, without using join.

Sign up to request clarification or add additional context in comments.

2 Comments

Join is not necessary for my question, but thanks so much for your help!
Edited the answer to include another way. Hope that helps

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.