
- 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
Count the number of distinct values in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100), -> Code varchar(100) -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','0001'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert','0002'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert','0003'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris','0001'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+------+ | Name | Code | +--------+------+ | Chris | 0001 | | Robert | 0002 | | Robert | 0003 | | Chris | 0001 | +--------+------+ 4 rows in set (0.00 sec)
Following is the query to count the number of distinct values −
mysql> select Name,count(distinct Code) from DemoTable group by Name;
Output
+--------+----------------------+ | Name | count(distinct Code) | +--------+----------------------+ | Chris | 1 | | Robert | 2 | +--------+----------------------+ 2 rows in set (0.06 sec)
- Related Questions & Answers
- Count occurrences of known distinct values in MySQL
- How to count distinct values in MySQL?
- Get distinct values and count them in MySQL
- Get the maximum count of distinct values in a separate column with MySQL
- How to count number of distinct values per field/ key in MongoDB?
- How to return distinct values in MySQL and their count?
- MySQL SELECT DISTINCT and count?
- How to count the distinct column in MySQL?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Count number of Distinct Substring in a String in C++
- MySQL Count Distinct values process is very slow. How to fasten it?
- Count distinct points visited on the number line in C++
- How to find the number of distinct values in an R vector?
- MySQL query to get the count of distinct records in a column
- Program to count number of distinct substrings in s in Python
Advertisements