Languages
[Edit]
EN

MySQL - find duplicated values in multiple columns

0 points
Created by:
illona
526

In this article, we would like to show you how to find duplicated values in multiple columns in MySQL.

Quick solution:

SELECT 
    `column1`, COUNT(`column1`),
    `column2`, COUNT(`column2`),
    `columnN`, COUNT(`columnN`)
FROM
    `table_name`
GROUP BY 
    `column1`,
    `column2`,
    `columnN`
HAVING 
    (COUNT(`column1`) > 1) AND 
    (COUNT(`column2`) > 1) AND
    (COUNT(`columnN`) > 1);

Practical example

To show how to find duplicated values in multiple columns, we will use the following table:

MySQL - example data used to find duplicated values in multiple columns
MySQL - example data used to find duplicated values in multiple columns

Note:

At the end of this article you can find database preparation SQL queries.

Example

In this example, we will display duplicated users and countries where:

  • name_quantity - number of duplicated names,
  • country_quantity - number of duplicated countries.

Note:

This works only when both name and country in a row are duplicated.

Query:

SELECT 
    `name`, COUNT(`name`) AS 'name_quantity',
    `country`, COUNT(`country`) AS 'country_quantity'
FROM
    `users`
GROUP BY 
    `name`,
    `country`
HAVING 
       (COUNT(`name`) > 1) AND 
       (COUNT(`country`) > 1);

Result:

MySQL - find and count duplicated values in multiple columns - result
MySQL - find and count duplicated values in multiple columns - result

Note:

Notice that Chris user both name_quantity and country_quantity equals 2 because one user is from a different country.

Database preparation

create_tables.sql file:

CREATE TABLE `users` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(100) NOT NULL,
	`email` VARCHAR(100) NOT NULL,
	`country` VARCHAR(15) NOT NULL,
	PRIMARY KEY (`id`)
)
ENGINE=InnoDB;

insert_data.sql file:

INSERT INTO `users`
	(`name`, `email`, `country`)
VALUES
	('Tom', 'tom1@email.com', 'Poland'),
	('Tom', 'tom2@email.com', 'Poland'),
    ('Tom', 'tom3@email.com', 'Poland'),
    ('Kim', 'kim1@email.com', 'Vietnam'),
    ('Kim', 'kim2@email.com', 'Vietnam'),
    ('Chris', 'chris1@email.com', 'Spain'),
	('Chris', 'chris2@email.com', 'Spain'),
	('Chris', 'chris3@email.com', 'USA');

Alternative titles

  1. MySQL - count duplicated values in multiple columns
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - Problems

MySQL - find duplicated values in multiple columns
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join