How to efficiently select all duplicates

I want to select all rows that have a value that already exists in the table. I have not found a better solution than

select * from provisioning_requests tt where code in (select code from provisioning_requests tt2 where tt2.id <> tt.id) 

It seems a bit naive. Does anyone have a better solution?

+6
source share
8 answers
 select * from provisioning_requests t1 join (select code from provisioning_requests group by code having count(*)>1) t2 ON t1.code = t2.code 

OR

 select * from provisioning_requests WHERE code in (select code from provisioning_requests group by code having count(*)>1) 
+7
source

Auto Connect is completing a task

 select tt.* from provisioning_requests tt INNER JOIN provisioning_requests tt2 ON tt.code = tt2.code AND tt2.id <> tt.id 
+7
source
 select t.* from( select *, count(1) over(partition by code) as cnt from test ) as t where t.cnt > 1 
+2
source

you can track line codes with

 select code from provisioning_requests tt group by code having count(code) > 1 
+1
source

How to use a different keyword?

 SELECT col1, col2, col3, ..., DISTINCT(code) from provisioning_requests; 
+1
source

You can use the exists operator, it provides better performance:

 select * from provisioning_requests tt where exists ( select 1 from provisioning_requests tt2 where tt2.id <> tt.id and tt2.code = tt.code ) 
+1
source

What about:

 SELECT *,COUNT(*) FROM provisioning_requests HAVING COUNT(*)>1 
0
source

Perhaps using self join, as well as the index above the Code column will make it more efficient.

 select pr1.* from provisioning_requests pr1 join provisioning_requests pr2 on pr1.code = pr2.code and pr1.id <> pr2.id 
0
source

Source: https://habr.com/ru/post/891530/


All Articles