0

I have a table like this:

id column2 column3 column4
1 value1 value2 somevalue
2 value3 value4 somevalue
3 value5 value5 somevalue

I want to delete any rows in the table where column 2 is equal to column 3. My table is 3000 rows long, and I want to perform this for every row that has the same value in column 2 and column 3. in this example, the row with value5 in column 2 and 3 would be deleted.

ive tried

DELETE FROM exampleTable t1 
WHERE EXISTS (SELECT 1 from Table t2
              WHERE t1.column2= t2.column3
              AND t2.Id = t1.Id) 

with no success. any help would be great thank you

4
  • from same table or different table ? what is t2? Commented Aug 18, 2022 at 9:23
  • Which DBMS you are talking about? Commented Aug 18, 2022 at 9:25
  • its the same table. im using snowflake Commented Aug 18, 2022 at 9:27
  • 1
    Am I missing something? Did you try: DELETE FROM exampleTable WHERE column2= column3? Commented Aug 18, 2022 at 9:28

3 Answers 3

4

Since it is in the same table, it can be done way easier:

DELETE FROM exampleTable WHERE column2 = column3

Fiddle

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

1 Comment

thank you! i was clearly ovethinking it!
1

Hi you missing table name in sub query, it must like

SELECT * FROM [1test] t2  WHERE t2.column3 = t2.column2

So it will delete 3rd row, bcz column 2 and column 3 have same value.

2 Comments

thanks! for some reason i get the error ' invalid identifier 'T2.ID''
as u say its same table, then directly can delete it.
0

Test this:

delete from yourTable t1 where (select count(*) from yourTable t2 where t2.id=t1.id group by t2.id) >=2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.