
- 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
How can we count a number of unique values in a column in MySQL table?
By using DISTINCT keyword along with column name as the argument of COUNT() function we can count the number of unique values in a column. The syntax is as follows −
SELECT COUNT(DISTINCT Col_name) FROM table_name;
Example
Suppose we have the following table
mysql> Select * from tender; +----------+--------------+--------------+-------+ | clientid | client_Fname | Client_Lname | value | +----------+--------------+--------------+-------+ | 100 | Mohan | Kumar | 60000 | | 101 | Sohan | Singh | 50000 | | 101 | Somil | Rattan | 55000 | | 103 | Gaurav | Kumar | 75000 | | 103 | Rahul | Singh | 63000 | +----------+--------------+--------------+-------+ 5 rows in set (0.00 sec)
Now if we want to count the total number of unique values in the column named ‘clientid’ then it can be done with the help of the following query −
mysql> Select COUNT(DISTINCT Clientid) from tender; +--------------------------+ | COUNT(DISTINCT Clientid) | +--------------------------+ | 3 | +--------------------------+ 1 row in set (0.00 sec)
And if we want to count the total number of unique values in column named ‘Client_Lname’ then it can be done with the help of following query −
mysql> Select COUNT(DISTINCT Client_Lname) from tender; +------------------------------+ | COUNT(DISTINCT Client_Lname) | +------------------------------+ | 3 | +------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How can we get only unique values of a column in MySQL result set?
- MySQL query to count number of duplicate values in a table column
- How can we update values in a MySQL table?
- How can I count unique records from a column in MySQL database?
- How can we use MySQL SELECT statement to count number of rows in a table?
- How can we drop UNIQUE constraint from a MySQL table?
- How can we put comments in a column of existing MySQL table?
- How can we remove a column from MySQL table?
- How do we count the total duplicate records in a column of MySQL table?
- How can we insert current date automatically in a column of MySQL table?
- How can we update MySQL table after removing a particular string from the values of column?
- How can we update MySQL table after padding a string with the values of the column?
- How can we use LPAD() or RPAD() functions with the values in the column of a MySQL table?
- Can we add a column to a table from another table in MySQL?
- How can we extract a substring from the value of a column in MySQL table?

Advertisements