
- 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
Can I use MySQL COUNT() and DISTINCT together?
Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows.
The syntax is as follows −
SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName;
To understand the above syntax, let us create a table.
The query to create a table is as follows −
mysql> create table CountDistinctDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (3.11 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into CountDistinctDemo(Name) values('Carol'); Query OK, 1 row affected (0.48 sec) mysql> insert into CountDistinctDemo(Name) values('Bob'); Query OK, 1 row affected (0.43 sec) mysql> insert into CountDistinctDemo(Name) values('Carol'); Query OK, 1 row affected (0.26 sec) mysql> insert into CountDistinctDemo(Name) values('John'); Query OK, 1 row affected (0.27 sec) mysql> insert into CountDistinctDemo(Name) values('Bob'); Query OK, 1 row affected (0.35 sec) mysql> insert into CountDistinctDemo(Name) values('Carol'); Query OK, 1 row affected (0.98 sec) mysql> insert into CountDistinctDemo(Name) values('John'); Query OK, 1 row affected (0.26 sec) mysql> insert into CountDistinctDemo(Name) values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into CountDistinctDemo(Name) values('Mike'); Query OK, 1 row affected (0.53 sec) mysql> insert into CountDistinctDemo(Name) values('Carol'); Query OK, 1 row affected (0.31 sec) mysql> insert into CountDistinctDemo(Name) values('David'); Query OK, 1 row affected (0.40 sec)
Display all records from the table using select statement.
The query is as follows.
mysql> select *from CountDistinctDemo;
The following is the output.
+----+-------+ | Id | Name | +----+-------+ | 1 | Carol | | 2 | Bob | | 3 | Carol | | 4 | John | | 5 | Bob | | 6 | Carol | | 7 | John | | 8 | Sam | | 9 | Mike | | 10 | Carol | | 11 | David | +----+-------+ 11 rows in set (0.07 sec)
If you do not use DISTINCT, then COUNT() function gives the count of all rows.
The query is as follows −
mysql> select count(Name) as TotalName from CountDistinctDemo;
The following is the output −
+-----------+ | TotalName | +-----------+ | 11 | +-----------+ 1 row in set (0.04 sec)
Here is the query to use COUNT() and DISTINCT together −
mysql> SELECT COUNT(DISTINCT Name) as UniqueName FROM CountDistinctDemo;
The following is the output
+------------+ | UniqueName | +------------+ | 6 | +------------+ 1 row in set (0.00 sec)
- Related Articles
- Using DISTINCT and COUNT together in a MySQL Query?
- MySQL SELECT DISTINCT and count?
- Can we use LIKE and OR together in MySql?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- Get distinct values and count them in MySQL
- How to use distinct and count in Android sqlite?
- Can we use NOT and AND together in MongoDB?
- How to count distinct values in MySQL?
- How to return distinct values in MySQL and their count?
- How to count the distinct column in MySQL?
- Count occurrences of known distinct values in MySQL
- Count the number of distinct values in MySQL?
- Is it possible to combine 'DISTINCT' and 'COUNT' queries, so that I can see how many times each distinct value appears in a MySQL table column?
- Can I use SUM() with IF() in MySQL?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?

Advertisements