
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Find and display duplicate records in MySQL?
First, a table is created with the help of the CREATE command. This is given as follows −
mysql> CREATE table DuplicateFound -> ( -> ID int, -> Name varchar(100), -> Location varchar(200) -> ); Query OK, 0 rows affected (0.45 sec)
After creating the table, the records are inserted with the help of the INSERT command as follows −
mysql> INSERT into DuplicateFound values(1,'John','US'); Query OK, 1 row affected (0.10 sec) mysql> INSERT into DuplicateFound values(2,'Bob','UK'); Query OK, 1 row affected (0.18 sec) mysql> INSERT into DuplicateFound values(3,'David','US'); Query OK, 1 row affected (0.14 sec) mysql> INSERT into DuplicateFound values(4,'Smith','US'); Query OK, 1 row affected (0.16 sec) mysql> INSERT into DuplicateFound values(5,'Carol','UK'); Query OK, 1 row affected (0.16 sec)
The records are displayed with the help of the SELECT statement. This is given below −
mysql> SELECT * from DuplicateFound;
The following is the output obtained −
+------+-------+----------+ | ID | Name | Location | +------+-------+----------+ | 1 | John | US | | 2 | Bob | UK | | 3 | David | US | | 4 | Smith | US | | 5 | Carol | UK | +------+-------+----------+ 5 rows in set (0.00 sec)
The query to find and display the duplicate records together is given as follows −
mysql> SELECT * from DuplicateFound -> where location in (select location from DuplicateFound group by location having count(location) >1 ) -> order by location;
The following is the output obtained
+------+-------+----------+ | ID | Name | Location | +------+-------+----------+ | 2 | Bob | UK | | 5 | Carol | UK | | 1 | John | US | | 3 | David | US | | 4 | Smith | US | +------+-------+----------+ 5 rows in set (0.06 sec)
We can display those records which belong to a single location. This is given as follows −
mysql> select Name,location from DuplicateFound -> group by location -> having count(location) > 1;
The following is the output
+------+----------+ | Name | Location | +------+----------+ | John | US | | Bob | UK | +------+----------+ 2 rows in set (0.04 sec)
- Related Articles
- Display an error while inserting duplicate records in a MySQL table
- Find duplicate column values in MySQL and display them
- MySQL select only duplicate records from database and display the count as well?
- Display the count of duplicate records from a column in MySQL and order the result
- MySQL query to display the count of distinct records from a column with duplicate records
- SUM corresponding duplicate records in MySQL
- Find duplicate records in MongoDB?
- MySQL query to find duplicate tuples and display the count?
- Find and display duplicate values only once from a column in MySQL
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Display records ignoring NULL in MySQL
- Display and concatenate records ignoring NULL values in MySQL
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Find “greatest” between two columns and display with some records already null in MySql
- Display all records except one in MySQL

Advertisements