0

I have a table with one column called name_of, I have a lot of duplicates in this table, I have some python code, that takes all the duplicates and then concat them into the SQL query. But I have tried for some time with no luck.

TableName = users_from_group

Column = name_of

i have tried the following sql query:

DELETE FROM users_from_group
WHERE name_of = ('AskeMeyer'), ('testuser'), ('AskeMeyer'), ('testuser'), ('testuser'), ('AskeMeyer')

and I don't understand why this is not working, as this is the format of data for querying adding into the table?

3
  • Do you want to keep one AskeMeyer etc? Commented Feb 23, 2022 at 14:05
  • 1
    A [mcvce] is a great start when asking SQL questions. We still don't know if you want to delete all duplicates, or if you want to keep one instance of each. (Which one?) Commented Feb 23, 2022 at 15:19
  • 1
    Add a unique constraint to your table, to avoid similar mishaps in the future. Commented Feb 23, 2022 at 15:21

2 Answers 2

1

the best way to do this is:

DELETE FROM users_from_group
WHERE name_of IN ('AskeMeyer', 'testuser', 'AskeMeyer', 'testuser', 'testuser', 'AskeMeyer')

If this is useful for you, I would appreciate to mark as resolved. Cheers

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

2 Comments

Why repeat values?
@jarlh it is duplicates found from the python code
1

As @jarlh mentioned, you probably don't want to delete ALL of the records with these name_of's. I'm guessing you want to keep exactly 1 of each?

The best practice for this scenario is to do your work all in SQL, following a "Delete Duplicate Rows" pattern. There are a couple ways to do this, and it can matter which version of SQL you are using, but this question is a starting point.

Also, just a couple S/O tips:

  1. Include which SQL server you are using in your questions in the future
  2. Include the error message that you receive

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.