Copy USE testdb;
CREATE TABLE contacts (
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL
);
Copy INSERT INTO contacts (first_name,last_name,email)
VALUES ('Carine ','Schmitt','carine.schmitt@qq.com'),
('Jean','King','jean.king@yiibai.com'),
('Peter','Ferguson','peter.ferguson@google.com'),
('Janine ','Labrune','janine.labrune@aol.com'),
('Jonas ','Bergulfsen','jonas.bergulfsen@mac.com'),
('Janine ','Labrune','janine.labrune@aol.com'),
('Susan','Nelson','susan.nelson@qq.com'),
('Zbyszek ','Piestrzeniewicz','zbyszek.piestrzeniewicz@qq.com'),
('Roland','Keitel','roland.keitel@yahoo.com'),
('Julie','Murphy','julie.murphy@yahoo.com'),
('Kwai','Lee','kwai.lee@google.com'),
('Jean','King','jean.king@qq.com'),
('Susan','Nelson','susan.nelson@qq.comt'),
('Roland','Keitel','roland.keitel@yahoo.com');
Copy +----+------------+-----------------+--------------------------------+
| id | first_name | last_name | email |
+----+------------+-----------------+--------------------------------+
| 1 | Carine | Schmitt | carine.schmitt@qq.com |
| 2 | Jean | King | jean.king@yiibai.com |
| 3 | Peter | Ferguson | peter.ferguson@google.com |
| 4 | Janine | Labrune | janine.labrune@aol.com |
| 5 | Jonas | Bergulfsen | jonas.bergulfsen@mac.com |
| 6 | Janine | Labrune | janine.labrune@aol.com |
| 7 | Susan | Nelson | susan.nelson@qq.com |
| 8 | Zbyszek | Piestrzeniewicz | zbyszek.piestrzeniewicz@qq.com |
| 9 | Roland | Keitel | roland.keitel@yahoo.com |
| 10 | Julie | Murphy | julie.murphy@yahoo.com |
| 11 | Kwai | Lee | kwai.lee@google.com |
| 12 | Jean | King | jean.king@qq.com |
| 13 | Susan | Nelson | susan.nelson@qq.comt |
| 14 | Roland | Keitel | roland.keitel@yahoo.com |
+----+------------+-----------------+--------------------------------+
14 rows in set
Copy SELECT
col,
COUNT(col)
FROM
table_name
GROUP BY col
HAVING COUNT(col) > 1;
Copy SELECT
email,
COUNT(email)
FROM
contacts
GROUP BY email
HAVING COUNT(email) > 1;
Copy +-------------------------+--------------+
| email | COUNT(email) |
+-------------------------+--------------+
| janine.labrune@aol.com | 2 |
| roland.keitel@yahoo.com | 2 |
+-------------------------+--------------+
2 rows in set
Copy SELECT
col1, COUNT(col1),
col2, COUNT(col2),
...
FROM
table_name
GROUP BY
col1,
col2, ...
HAVING
(COUNT(col1) > 1) AND
(COUNT(col2) > 1) AND
...
Copy SELECT
first_name, COUNT(first_name),
last_name, COUNT(last_name),
email, COUNT(email)
FROM
contacts
GROUP BY
first_name ,
last_name ,
email
HAVING COUNT(first_name) > 1
AND COUNT(last_name) > 1
AND COUNT(email) > 1;
Copy +------------+-------------------+-----------+------------------+-------------------------+--------------+
| first_name | COUNT(first_name) | last_name | COUNT(last_name) | email | COUNT(email) |
+------------+-------------------+-----------+------------------+-------------------------+--------------+
| Janine | 2 | Labrune | 2 | janine.labrune@aol.com | 2 |
| Roland | 2 | Keitel | 2 | roland.keitel@yahoo.com | 2 |
+------------+-------------------+-----------+------------------+-------------------------+--------------+
2 rows in set