
- 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 the count of duplicate records from a column in MySQL and order the result
For this, you need to use GROUP BY for the column name. To get the count, use COUNT(*) and order the result with ORDER BY. Following is the syntax −
select count(*) as anyAliasName from yourTableName group by yourColumnName order By yourAliasName DESC;
Let us first create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(110); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 90 | | 100 | | 110 | | 90 | | 100 | | 90 | | 120 | | 100 | | 90 | | 120 | +--------+ 10 rows in set (0.00 sec)
Following is the query to display the count of duplicate records and order the result −
mysql> select count(*) as Occured from DemoTable group by Number order By Occured DESC;
This will produce the following output −
+---------+ | Occured | +---------+ | 4 | | 3 | | 2 | | 1 | +---------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to display the count of distinct records from a column with duplicate records
- Count duplicate ids and display the result in a separate column with MySQL
- MySQL query to count the duplicate ID values and display the result in a separate column
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- MySQL select only duplicate records from database and display the count as well?
- How do we count the total duplicate records in a column of MySQL table?
- Find and display duplicate records in MySQL?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Get the count of duplicate values from a single column in MySQL?
- How do we count the records from MySQL table where column holds duplicate/triplicates data?
- Find and display duplicate values only once from a column in MySQL
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- Order by a single field and display rest of the records in the same order with MySQL
- Order MySQL records randomly and display name in Ascending order
- Count the occurrences of specific records (duplicate) in one MySQL query

Advertisements