- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to count the distinct column in MySQL?
You need to use GROUP BY for this. Let us first create a table −
mysql> create table DemoTable ( StudentFirstName varchar(20) ); Query OK, 0 rows affected (0.74 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (1.34 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+------------------+ | StudentFirstName | +------------------+ | John | | Carol | | John | | John | | Bob | | David | | Bob | | Carol | | Robert | +------------------+ 9 rows in set (0.00 sec)
Here is the query to count the distinct column in MySQL −
mysql> select StudentFirstName,count(*) from DemoTable group by StudentFirstName;
This will produce the following output −
+------------------+----------+ | StudentFirstName | count(*) | +------------------+----------+ | John | 3 | | Carol | 2 | | Bob | 2 | | David | 1 | | Robert | 1 | +------------------+----------+ 5 rows in set (0.00 sec)
- Related Articles
- How to get the count of each distinct value in a column in MySQL?
- MySQL query to get the count of distinct records in a column
- How to count distinct values in MySQL?
- Get the maximum count of distinct values in a separate column with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?
- How to return distinct values in MySQL and their count?
- MySQL query to display the count of distinct records from a column with duplicate records
- Averaging a total from a Score column in MySQL with the count of distinct ids?
- Count the number of distinct values in MySQL?
- MySQL SELECT DISTINCT and count?
- Display distinct column name in MySQL
- How to display the count from distinct records in the same row with MySQL?
- How to select distinct value from one MySQL column only?
- How to get the sum for every distinct value in another column in MySQL?
- How to sum varchar column and display the count in MySQL?

Advertisements