- 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
MySQL query to get the count of distinct records in a column
To get the count of distinct records, use DISTINCT along with COUNT(). Following is the syntax −
select count(DISTINCT yourColumnName) from yourTableName;
Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(20), -> Score int -> ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John',56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Sam',89); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('John',56); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Carol',60); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Sam',89); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Carol',60); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable
Output
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | John | 56 | | Sam | 89 | | John | 56 | | Carol | 60 | | Sam | 89 | | Carol | 60 | +-------+-------+ 6 rows in set (0.00 sec)
Here is the query to get to get the count of distinct records in a column −
mysql> select count(DISTINCT Score) from DemoTable;
Output
This will produce the following output −
+-----------------------+ | count(DISTINCT Score) | +-----------------------+ | 3 | +-----------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to display the count of distinct records from a column with duplicate records
- How to get the count of each distinct value in a column 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?
- Add a column count in a MySQL query on the basis of last name records?
- How to count the distinct column in MySQL?
- MySQL query to remove Null Records in a column?
- Using DISTINCT and COUNT together in a MySQL Query?
- MySQL query to get the count of values and display the count in a new column ordered in descending order
- MySQL query to alphabetize records and count the duplicates?
- Get multiple count in a single MySQL query for specific column values
- MySQL query to select average from distinct column of table?
- Display distinct dates in MySQL from a column with date records
- Count the occurrences of specific records (duplicate) in one MySQL query
- Get MySQL DISTINCT to work correctly if the records contain whitespace?

Advertisements