
- 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
Display only the duplicate column names appearing atleast thrice in MySQL
Use HAVING COUNT() for this. Let us first create a table −
mysql> create table DemoTable1351 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(40) -> ); Query OK, 0 rows affected (1.08 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1351(StudentName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1351(StudentName) values('Mike'); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1351;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Bob | | 3 | Bob | | 4 | David | | 5 | Bob | | 6 | David | | 7 | Bob | | 8 | Mike | | 9 | David | +-----------+-------------+ 9 rows in set (0.00 sec)
Here is the query to display the duplicate column names appearing atleast thrice −
mysql> select * from DemoTable1351 -> group by StudentName -> having count(StudentName) >=3;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 2 | Bob | | 4 | David | +-----------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- Find and display duplicate values only once from a column in MySQL
- Display only a specific duplicate record in MySQL
- Find duplicate column values in MySQL and display them
- How to order return duplicate column values only once in MySQL?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- How to display the column names from a table excluding some in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL select only duplicate records from database and display the count as well?
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to group by names and display the count in a new column
- Display the count of duplicate records from a column in MySQL and order the result
- GROUP BY and display only non-empty column values in MySQL
- Get only the file extension from a column with file names as strings in MySQL?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Select a value from MySQL database only if it exists only once from a column with duplicate and non-duplicate values

Advertisements