How can we get only unique values of a column in MySQL result set?


While querying data from a MySQL table, we may get duplicate values from a column. With the help of the DISTINCT clause in the SELECT statement, we can get rid of duplicate data in the result set.

Syntax

SELECT DISTINCT Columns FROM Table_name WHERE conditions;

Example

For example, we have a table named ‘tender’ having the following columns −

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 get only the unique values of the column named ‘Client_Lname’ then following would be query −

mysql> Select DISTINCT client_Lname from tender;
+--------------+
| client_Lname |
+--------------+
| Kumar        |
| Singh        |
| Rattan       |
+--------------+
3 rows in set (0.05 sec)

The query below will do the same with a column named ‘client_Fname’.

mysql> Select DISTINCT client_Fname from tender;
+--------------+
| client_Fname |
+--------------+
| Mohan        |
| Sohan        |
| Somil        |
| Gaurav       |
| Rahul        |
+--------------+
5 rows in set (0.00 sec)

Updated on: 22-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements