
- 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
Get distinct values and count them in MySQL
To get distinct values and count them, you can use GROUP BY clause.
The syntax is as follows
select yourColumnName,count(*) as anyAliasName from yourTableName group by yourColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table GroupByAndCountDemo -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(100) -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Sam'); Query OK, 1 row affected (0.16 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.51 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Sam'); Query OK, 1 row affected (0.08 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Sam'); Query OK, 1 row affected (0.47 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Sam'); Query OK, 1 row affected (0.09 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Maxwell'); Query OK, 1 row affected (0.17 sec) mysql> insert into GroupByAndCountDemo(ClientName) values('Maxwell'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from GroupByAndCountDemo;
The output is as follows
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | John | | 2 | Carol | | 3 | Sam | | 4 | Sam | | 5 | John | | 6 | John | | 7 | John | | 8 | Sam | | 9 | Sam | | 10 | Sam | | 11 | John | | 12 | John | | 13 | John | | 14 | David | | 15 | Maxwell | | 16 | Maxwell | +----------+------------+ 16 rows in set (0.00 sec)
Now let us get distinct values and count them using the following query
mysql> select ClientName,count(*) as TotalCount from GroupByAndCountDemo group by ClientName;
The following is the output
+------------+------------+ | ClientName | TotalCount | +------------+------------+ | John | 7 | | Carol | 1 | | Sam | 5 | | David | 1 | | Maxwell | 2 | +------------+------------+ 5 rows in set (0.00 sec)
- Related Articles
- How to count distinct values in MySQL?
- How to return distinct values in MySQL and their count?
- Get the maximum count of distinct values in a separate column with MySQL
- Count occurrences of known distinct values in MySQL
- Count the number of distinct values in MySQL?
- MySQL SELECT DISTINCT and count?
- Python program to count distinct words and count frequency of them
- MySQL query to get the count of distinct records in a column
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- Get distinct record values in MongoDB?
- Using DISTINCT and COUNT together in a MySQL Query?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL Count Distinct values process is very slow. How to fasten it?
- Can I use MySQL COUNT() and DISTINCT together?
- MySQL Query to get count of unique values?

Advertisements