How to SELECT all values from a table only once if they're duplicated?


You can use distinct keyword to select all values from a table only once if they are repeated.

The syntax is as follows

select distinct yourColumnName from yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows.

mysql> create table displayOnlyDistinctValue
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(100),
   -> UserAge int
   -> );
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command.

The query is as follows.

mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Larry',23);
Query OK, 1 row affected (0.23 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Mike',23);
Query OK, 1 row affected (0.13 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Larry',21);
Query OK, 1 row affected (0.16 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Sam',23);
Query OK, 1 row affected (0.20 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Carol',25);
Query OK, 1 row affected (0.16 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Sam',26);
Query OK, 1 row affected (0.14 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Sam',27);
Query OK, 1 row affected (0.13 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Bob',22);
Query OK, 1 row affected (0.17 sec)
mysql> insert into displayOnlyDistinctValue(UserName,UserAge) values('Larry',22);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement.

The query is as follows.

mysql> select *from displayOnlyDistinctValue;

The following is the output.

+--------+----------+---------+
| UserId | UserName | UserAge |
+--------+----------+---------+
| 1      | Larry    | 23      |
| 2      | Mike     | 23      |
| 3      | Larry    | 21      |
| 4      | Sam      | 23      |
| 5      | Carol    | 25      |
| 6      | Sam      | 26      |
| 7      | Sam      | 27      |
| 8      | Bob      | 22      |
| 9      | Larry    | 22      |
+--------+----------+---------+
9 rows in set (0.00 sec)

Here is the query to select all values from table only once if they're duplicated.

Case 1- In UserName column name.

The query is as follows.

mysql> select distinct UserName from displayOnlyDistinctValue;

The output

+----------+
| UserName |
+----------+
| Larry    |
| Mike     |
| Sam      |
| Carol    |
| Bob      |
+----------+
5 rows in set (0.00 sec)

Case 2 -In UserAge column name.

The query is as follows.

mysql> select distinct UserAge from displayOnlyDistinctValue;

The output..

+---------+
| UserAge |
+---------+
| 23      |
| 21      |
| 25      | 
| 26      |
| 27      |
| 22      |
+---------+
6 rows in set (0.00 sec)

Updated on: 30-Jul-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements