
- 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
Finding Duplicate Rows in MySQL (Composite Key)?
To find duplicate rows, use GROUP BY and HAVING in MySQL. Let us first create a table −
mysql> create table DemoTable1494 -> ( -> ShippingDate datetime, -> CustomerCountryName varchar(20) -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1494 values('2018-09-10 12:34:50','US'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1494 values('2019-09-10 12:34:50','AUS'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable1494 values('2018-09-10 11:00:00','US'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1494 values('2017-12-31 01:10:40','UK'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1494 values('2019-09-10 05:00:50','AUS'); Query OK, 1 row affected (0.49 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1494;
This will produce the following output −
+---------------------+---------------------+ | ShippingDate | CustomerCountryName | +---------------------+---------------------+ | 2018-09-10 12:34:50 | US | | 2019-09-10 12:34:50 | AUS | | 2018-09-10 11:00:00 | US | | 2017-12-31 01:10:40 | UK | | 2019-09-10 05:00:50 | AUS | +---------------------+---------------------+ 5 rows in set (0.00 sec)
Following is the query to find duplicate rows in MySQL −
mysql> select ShippingDate,CustomerCountryName,count(*) as c -> from DemoTable1494 -> group by date(ShippingDate),CustomerCountryName -> having c >=2 -> order by c;
This will produce the following output −
+---------------------+---------------------+---+ | ShippingDate | CustomerCountryName | c | +---------------------+---------------------+---+ | 2018-09-10 12:34:50 | US | 2 | | 2019-09-10 12:34:50 | AUS | 2 | +---------------------+---------------------+---+ 2 rows in set (0.00 sec)
- Related Articles
- Composite Key in RDBMS
- ALTER TABLE to add a composite primary key in MySQL?
- Implement INSERT … ON DUPLICATE KEY UPDATE in MySQL
- Implementing INSERT… ON DUPLICATE KEY UPDATE in MySQL
- How to form a composite key to be unique in MySQL?
- How to identify composite primary key in any MySQL database table?
- How to avoid inserting duplicate rows in MySQL?
- How to prevent duplicate rows in MySQL INSERT?
- Correct MySQL INSERT ... ON DUPLICATE KEY syntax?
- Add constraint for on duplicate key update in MySQL
- MySQL Composite Index
- Return only a single row from duplicate rows with MySQL
- Finding the sum of integers from multiple MySQL rows in same column?
- Finding total number of rows of tables across multiple databases in MySQL?
- Select all duplicate MySQL rows based on one or two columns?

Advertisements