
- 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
Return the field with highest count in MySQL
To return the field with highest count, use ORDER BY COUNT(*). Let us first create a table −
mysql> create table DemoTable1940 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1940 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Adam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1940;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Chris | | Mike | | Adam | | Mike | | Chris | | David | | Mike | +-----------+ 7 rows in set (0.00 sec)
Here is the query to return the field with highest count:
mysql> select * from DemoTable1940 group by FirstName order by count(*) desc limit 1;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Mike | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- Count values from comma-separated field in MySQL?
- Return a list from different rows into a single field with MySQL
- Only display row with highest ID in MySQL
- MySQL query to count comma’s from field value?
- MySQL query to get the count of all the elements in the field?
- Count the documents with a field value beginning with 13
- How can I select the row with the highest ID in MySQL?
- MySQL UPDATE query where id is highest AND field is equal to variable?
- How to return distinct values in MySQL and their count?
- Count boolean field values within a single MySQL query?
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- Select highest salary in MySQL?
- Count Frequency of Highest Frequent Elements in Python
- How to count all characters in all rows of a field in MySQL?
- MongoDB query to create new field and count set the count of another field in it?

Advertisements