1

I want to find rows in my table that have duplicate values accross columns in a the same row

Example:

id   column_1    column_2    column_3
1    123            44           100
2    555            555          555
3    101            396          100
4    99             99           99
5    123            44           100

I need a query that returns rows 2 & 4. So far, I have only found questions with similar titles that refer to finding rows that have the same values in multiple columns that would for example return 1 & 5. That's not what I am looking for :)

1
  • where col1=col2 and col2=col3 Commented May 29, 2018 at 16:27

2 Answers 2

1

This is your query:

SELECT * FROM your_table WHERE column_1 = column_2 AND column_2 = column_3
Sign up to request clarification or add additional context in comments.

Comments

1

Substraction may be used for three columns :

select *
  from mytable
 where ( column_1 - column_2  =  column_3 - column_2 )
   and column_1 = column_2
order by 1;

or least and greatest functions may be used together :

select *
  from mytable    
 where greatest(column_1,column_2,column_3) = least(column_1,column_2,column_3)
order by 1;

SQL Fiddle Demo

2 Comments

least and greatest are not aggregate functions. No need for group by in your 2nd query, it's a simple WHERE greatest(column_1,column_2,column_3) = least(column_1,column_2,column_3)
@dnoeth exactly, danke schön.

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.