I have a table in this form:
id | firstname | lastname
---+-----------+----------
1 | alex | marti
2 | mark | finger
3 | alex | marti
4 | ted | port
Need to return the firstname, lastname duplicates in this form:
1 | alex | marti
3 | alex | marti
I tried doing select firstname, lastname from t group by firstname, lastname having count(*) > 1 but that will return something like
firstname | lastname
----------+----------
mark | finger
alex | marti
ted | port
And I need the id of the duplicates but of course select id, firstname, lastname from t group by id, firstname, lastname won’t work.
Any ideas? Thanks.
You need to aggregate the id. If you need only the ID of one of them, for, say, deletion, you could do:
If you want both id’s and know there will never be more than 2, you could do the following:
If you want all duplicates, along with their id’s, you’ll have to use a derived table, as in Nitin Midha’s answer.