
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to find duplicate tuples and display the count?
To find duplicate tuples, use GROUP BY HAVING clause. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.80 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(101,'David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(101,'Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(100,'Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 100 | Chris | | 101 | David | | 101 | Mike | | 100 | Carol | | 100 | Chris | | 100 | Chris | +------+-------+ 6 rows in set (0.00 sec)
Following is the query to find duplicate tuples −
mysql> select Id,Name,count(*) as t from DemoTable -> group by Id,Name -> having count(*) > 2;
This will produce the following output −
+------+-------+---+ | Id | Name | t | +------+-------+---+ | 100 | Chris | 3 | +------+-------+---+ 1 row in set (0.03 sec)
- Related Questions & Answers
- MySQL query to group results by date and display the count of duplicate values?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Find and display duplicate records in MySQL?
- MySQL query to display the count of distinct records from a column with duplicate records
- Find duplicate column values in MySQL and display them
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL select only duplicate records from database and display the count as well?
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL query to select the values having multiple occurrence and display their count
- Count the occurrences of specific records (duplicate) in one MySQL query
- Display the count of duplicate records from a column in MySQL and order the result
- MySQL query to group by names and display the count in a new column
- Find and display duplicate values only once from a column in MySQL
- MySQL query to count number of duplicate values in a table column
- MySQL query to alphabetize records and count the duplicates?
Advertisements