MySql DISTINCT query syntax with example

In this article we will see "How to use DISTINCT in MySql" to return only distinct (different) values and "DISTINCT query syntax with example".

DISTINCT query in MySql syntax

The DISTINCT statement is used to return only distinct (different) values, syntax is as follows.


SELECT DISTINCT column1, column2, ...
FROM tableName;


DISTINCT query in MySql example

The DISTINCT statement is used to return only distinct (different) values, as shown below:


/*Example EMPLOYEE table */
+----+--------+---------+
| id | name   | email   |
+----+--------+---------+
|  1 | name-1 | email-1 |
|  2 | name-2 | email-2 |
|  3 | name-3 | email-3 |
|  4 | name-4 | email-4 |
|  5 | name-1 | email-5 |
|  6 | name-2 | email-6 |
+----+--------+---------+



We will now apply DISTINCT on above table to get rows with distinct 'name' only:


SELECT DISTINCT NAME FROM EMPLOYEE;

Output: Output will be something like this:


+--------+
| NAME   |
+--------+
| name-1 |
| name-2 |
| name-3 |
| name-4 |
+--------+


In this article we have seen "How to use DISTINCT in MySql" and "DISTINCT query syntax with example", in upcoming articles we will see more about Mysql syntax and queries with example.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.