
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the maximum count of distinct values in a separate column with MySQL
Use COUNT() function along with GROUP BY clause for this. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------+ | Name | +-------+ | John | | Sam | | John | | Mike | | John | | Sam | | Carol | +-------+ 7 rows in set (0.00 sec)
Here is the query to maximum count of distinct values in SQL −
mysql> select Name, count(*) as freq from DemoTable group by Name order by count(*) desc limit 1;
Output
This will produce the following output −
+------+------+ | Name | freq | +------+------+ | John | 3 | +------+------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- MySQL query to get the count of distinct records in a column
- Get distinct values and count them in MySQL
- Can I get the count of repeated values in a column with MySQL?
- Count the number of distinct values in MySQL?
- How to get the count of each distinct value in a column in MySQL?
- Get the count of duplicate values from a single column in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- Concatenate the column values with separate text in MySQL and display in a single column
- Get distinct values from a column in MongoDB?
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Get the maximum value of a column with MySQL Aggregate function
- How to count the distinct column in MySQL?
- Count occurrences of known distinct values in MySQL
- Averaging a total from a Score column in MySQL with the count of distinct ids?
Advertisements