Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:53:04+00:00 2026-06-13T05:53:04+00:00

Possible Duplicate: delete duplicate records in SQL Server I have a table in which

  • 0

Possible Duplicate:
delete duplicate records in SQL Server

I have a table in which unique records are denoted by a composite key, such as (COL_A, COL_B).

I have checked and confirmed that I have duplicate rows in my table by using the following query:

select COL_A, COL_B, COUNT(*)
from MY_TABLE
group by COL_A, COL_B
having count(*) > 1
order by count(*) desc

Now, I would like to remove all duplicate records but keep only one.

Could someone please shed some light on how to achieve this with 2 columns?

EDIT:
Assume the table only has COL_A and COL_B

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:53:05+00:00Added an answer on June 13, 2026 at 5:53 am

    1st solution,
    It is flexible, because you can add more columns than COL_A and COL_B :

    -- create table with identity filed
    -- using idenity we can decide which row we can delete
    create table MY_TABLE_COPY
    (
      id int identity,
      COL_A varchar(30), 
      COL_B  varchar(30)
      /*
         other columns
      */
    )
    go
    -- copy data
    insert into MY_TABLE_COPY (COL_A,COL_B/*other columns*/)
    select COL_A, COL_B /*other columns*/
    from MY_TABLE
    group by COL_A, COL_B
    having count(*) > 1
    -- delete data from  MY_TABLE
    -- only duplicates (!)
    delete MY_TABLE
    from MY_TABLE_COPY c, MY_TABLE t
    where c.COL_A=t.COL_A 
    and c.COL_B=t.COL_B
    go
    -- copy data without duplicates
    insert into MY_TABLE (COL_A, COL_B /*other columns*/)
    select t.COL_A, t.COL_B /*other columns*/
    from MY_TABLE_COPY t
    where t.id = (
                   select max(id) 
                   from MY_TABLE_COPY c  
                   where t.COL_A = c.COL_A
                   and  t.COL_B = c.COL_B
                 ) 
    go
    

    2nd solution
    If you have really two columns in MY_TABLE you can use:

    -- create table and copy data
    select distinct COL_A, COL_B
    into MY_TABLE_COPY
    from MY_TABLE
    -- delete data from  MY_TABLE 
    -- only duplicates (!)
    delete MY_TABLE
    from MY_TABLE_COPY c, MY_TABLE t
    where c.COL_A=t.COL_A 
    and c.COL_B=t.COL_B
    go
    -- copy data without duplicates
    insert into MY_TABLE
    select t.COL_A, t.COL_B
    from MY_TABLE_COPY t
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How Do You Delete Duplicate Records In SQL I have a table
Possible Duplicate: How to delete duplicate rows with SQL? I have a table with
Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I
Possible Duplicate: SQL: DELETE data from self-referencing table in specific order I need to
Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I'm
Possible Duplicate: MySQL Duplicate rows how to delete duplicate rows from a table in
Possible Duplicate: how to delete row from datagridview with a delete button? I have
Possible Duplicate: What's the best way to delete all data from a table? Clear
Possible Duplicates: SQL Server - bulk delete (truncate vs delete) What’s the difference between
Possible Duplicate: MySQL delete row from multiple tables I have 5 tables: members member_videos

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.