Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query to find the duplicate records

Tags:

sql

duplicates

what is the sql query to find the duplicate records and display in descending, based on the highest count and the id display the records.

for example:

getting the count can be done with

select title, count(title) as cnt from kmovies group by title order by cnt desc

and the result will be like

title cnt

ravi   10
prabhu  9
srinu   6

now what is the query to get the result like below:

ravi
ravi
ravi
...10 times
prabhu
prabhu..9 times
srinu
srinu...6 times
like image 829
Tan Avatar asked Jul 21 '11 16:07

Tan


People also ask

How do I find duplicate records in SQL query?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

What is duplicate check in SQL?

Duplicates in SQL are mostly the data points that exist more than once in our data store. For example: If we consider a customer table and in it, we store the details of customers of any particular shop. Then here, duplicate records would be the entry of a single customer more than once.


1 Answers

If your RDBMS supports the OVER clause...

SELECT
   title
FROM
    (
    select
       title, count(*) OVER (PARTITION BY title) as cnt
    from
      kmovies
    ) T
ORDER BY
   cnt DESC
like image 62
gbn Avatar answered Sep 19 '22 09:09

gbn