0

How can I write the following query in SQL? I have a table where column B has erroneously been filled with duplicate values. The easiest way to find these duplicates is by comparing to another column A in the same table which should generally be a 1-to-1 correspondence, the table will possibly contain multiple rows of the repeated column B value

Something like this

column A | column B
-------------------
00234211 | T 156   
00011331 | T 157   
00011331 | T 157   
04600100 | T 157  <--- This is probably an erroneous value, show me every row for "T 157"
07740010 | T 159

How would I write a SQL query resembling this terrible pseudo code

  for all rows:
    let A = columnA_value, B = columnB_value
    for rows where columnA = A:
      IF COUNT(columnB) > 1, 
          SHOW all rows WHERE columnA = A
Nils Guillermin
  • 1,867
  • 3
  • 21
  • 51
  • Possible duplicate of [Find duplicate records in MySQL](https://stackoverflow.com/questions/854128/find-duplicate-records-in-mysql) – dfundako Mar 27 '18 at 19:24
  • 1
    SQL is a set based language. Why would you need double for loop in SQL????? – Eric Mar 27 '18 at 19:48

1 Answers1

1

Hmmm. I think you want:

select t.*
from t
where exists (select 1 from t t2 where t2.b = t.b and t2.a <> t.a);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786