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

  • Home
  • SEARCH
  • 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 4595432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:58:11+00:00 2026-05-21T22:58:11+00:00

I have unique keys id keys in my table but I have a column

  • 0

I have unique keys id keys in my table but I have a column with duplicate values? how do I get rid of those, while preserving only one of them like this :

Duplicate records :

id  | name   | surname |
1   | test   | one     |
2   | test   | two     |
3   | test3  | three   |
4   | test7  | four    |
5   | test   | five    |
6   | test11 | eleven  |

Without duplicates :

id  | name   | surname |
1   | test   | one     |
3   | test3  | three   |
4   | test7  | four    |
6   | test11 | eleven  |

I’ve googled this but it seems not to be working :

DELETE  ct1
FROM    mytable ct1
        , mytable ct2
WHERE   ct1.name = ct2.name 
        AND ct1.id < ct2.id 

ERROR:  syntax error at or near "ct1"
LINE 1: DELETE  ct1
                ^

********** Error **********

I’m using postgres database.

  • 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-05-21T22:58:11+00:00Added an answer on May 21, 2026 at 10:58 pm

    You can try this running multiple times:

    delete from mytable where id in (
        select max(id)
          from mytable
         group by name
        having count(1) > 1
    );
    

    Where multiple times equals the maximum number of repetitions you have in name column.

    Otherwise, you can try this more complex query:

    delete from mytable where id in (
        select id from mytable
        except 
        (
        select min(id)
          from mytable
         group by name
        having count(1) > 1
        union all
        select min(id)
          from mytable
         group by name
        having count(1) = 1
        )
    );
    

    Running this query one time only should delete all you need. Haven’t tried it though…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have small tables(files) with unique keys (most of those have
I Want to have only unique values in a SharePoin List. To achieve this
Using MySQL, when I have a table with (for example) three keys (one primary,
I have a table full of items that each have a unique ItemID .
How do I best design a database where I have one table of players
I have a table named Orders that has a column named OrderCode that stores
I have a table with a unique key for two columns: CREATE TABLE `xpo`.`user_permanent_gift`
I have a table with 2 primary keys (so the combination of both of
I have a large table (~10 million records) that contains several keys into other,
I have a table (Major) without any foreign keys and just two fields: ID

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.