MySQL DISTINCT Clause

MySQL | DISTINCT Clause: Learn about the MySQL DISTINCT Clause, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 05, 2022

DISTINCT Clause

In MySQL we have some clauses; these clauses help us to filter the data as per our requirements. The clauses play an important part when you write queries. We have many clauses in MYSQL.

DISTINCT Clause is one of the special clauses in MYSQL as this clause removes duplicate data entries from the tables. This clause is only used with the SELECT statement.

If we only want to keep unique records then in that case we can simply use the DISTINCT Clause.

DISTINCT Clause Syntax

SELECT DISTINCT column_name FROM table_name;

Note: We can write as many columns as we want in a single query, also we can use WHERE conditions with this clause.

Suppose we have a table named "student_details" and inside this database, we have the following columns.

DISTINCT Clause (Example 1)

Assume the case where I only want to how many departments are there in the table if we'll simply write the select statement like:

SELECT Student_department FROM student_details;

Then, as a result, it will simply give the whole column of "student_department" such that duplicate values will be included in a case like this we won't be able to identify the number of departments.

But if we use a DISTINCT clause then:

SELECT DISTINCT Student_department FROM student_details;

In this case, we'll get unique records.

DISTINCT Clause (Example 2)

With the help of a DISTINCT clause, we'll only get the unique records of the table.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.