How to fetch duplicate records

  • Hi,

    I am using SQL Server 2010 with Visual Studio for my application. Now I have a table say "EmEmployeesand that have some 5000 records. This table has a column "Name" which is not a primary field. There is a possibility of more than one records having same name. Now I have to fetch all the records having duplicate names. What should be the efficient way to do this?

    Please let me know.

    Regards,

    Girish

  • girish.nehte-1109699 (1/23/2013)


    Hi,

    I am using SQL Server 2010 with Visual Studio for my application. Now I have a table say "EmEmployeesand that have some 5000 records. This table has a column "Name" which is not a primary field. There is a possibility of more than one records having same name. Now I have to fetch all the records having duplicate names. What should be the efficient way to do this?

    Please let me know.

    Regards,

    Girish

    to get the faster and possible correct help , always post table definition , sampled data plus expected output. see the link in my signature

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • select name from

    (

    select name, row_number() over ( partition by name order by name ) rnk

    from EmEmployeesand

    ) t where t.rnk > 1

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Yes, or use GROUP BY and HAVING. Sounds suspiciously like an interview or homework question to me.

    John

  • select <field list req'd>

    from <your table>

    where <field with dupes> in (select <field with dupes> from <your table> group by <field with dupes>

    having count(*) > 1)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply