oracle
  1. oracle-distinct

DISTINCT - ( Oracle Clauses )

In Oracle databases, DISTINCT is a clause that is used in a SELECT statement to eliminate duplicate records from the result set. It is used to retrieve only unique values from a column or set of columns in a table.

Syntax

The basic syntax for using DISTINCT in Oracle is as follows:

SELECT DISTINCT column_name(s) FROM table_name;

Here, DISTINCT is used to specify that only unique records should be retrieved, and column_name(s) specifies the name of the column or set of columns to retrieve unique values from.

Example

Consider the following employees table:

+------+-----------+--------+
|  id  |  name     |  dept  |
+------+-----------+--------+
|  1   |  John     |  IT    |
|  2   |  Jane     |  Sales |
|  3   |  John     |  HR    |
|  4   |  Mary     |  IT    |
|  5   |  John     |  Sales |
+------+-----------+--------+

Suppose we want to retrieve the unique department names in the employees table. We can do this using the following SQL query:

SELECT DISTINCT dept FROM employees;

This will return the following result set:

+--------+
|  dept  |
+--------+
|  IT    |
|  Sales |
|  HR    |
+--------+

Output

The DISTINCT clause eliminates all duplicate records from the result set, so only unique records are displayed.

Explanation

In the above example, we use the DISTINCT clause to retrieve only unique values from the dept column of the employees table. As a result, only the unique department names are displayed in the output.

Use

The DISTINCT clause is useful for retrieving unique values from a column or set of columns in a table. It is typically used in conjunction with the SELECT statement to eliminate duplicate records from the result set.

Important Points

  • The DISTINCT clause is used in a SELECT statement to eliminate duplicate records from the result set.
  • It is used to retrieve only unique values from a column or set of columns in a table.
  • The DISTINCT clause eliminates all duplicate records from the result set.
  • The DISTINCT clause can be used with any column or set of columns in a table.

Summary

In summary, the DISTINCT clause in Oracle is used to retrieve only unique values from a column or set of columns in a table. It eliminates duplicate records from the result set and is typically used in conjunction with the SELECT statement. By retrieving only unique values, the DISTINCT clause helps to simplify data analysis and reporting tasks.

Published on: