Oracle DISTINCT

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Oracle DISTINCT clause is used to remove the duplicate records from the result set. It is only used with SELECT statement.

Syntax:

  1. SELECT DISTINCT expressions  
  2. FROM tables  
  3. WHERE conditions;  

Parameters:

expressions:It specifies the columns that you want to retrieve.

tables: It specifies the table from where you want to retrieve records.

conditions: It specifies the conditions that must be fulfilled.

Oracle DISTINCT Example: (with single expression)

Let's take a table "customers"

Customer table:

  1. CREATE TABLE  "CUSTOMERS"   
  2.    (    "NAME" VARCHAR2(4000),   
  3.     "AGE" NUMBER,   
  4.     "SALARY" NUMBER,   
  5.     "STATE" VARCHAR2(4000)  
  6.    )  
  7. /  

Customer Table

Execute this query:

  1. SELECT DISTINCT state  
  2. FROM customers  
  3. WHERE name = 'charu';  

Output:

oracle distinct query 1

Oracle DISTINCT Example: (with multiple expressions)

Execute this query:

  1. SELECT DISTINCT name, age, salary  
  2. FROM customers  
  3. WHERE age >= '60';  

Output:

oracle distinct query 1

This example specifies distinct name, age and salary of the customer where age is greater than or equal to 65.