SQL SELECT DISTINCT Statement


The DISTINCT keyword can be used to return only distinct (different) values. In a table, some of the columns may contain duplicate values.

This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.
 SELECT DISTINCT column_name(s)
   FROM table_name;

In the example below, the company W3Schools is listed twice in the result-set if the keyword DISTINCT is not used. To select only different values from the column named company, we use a SELECT DISTINCT statement.

 SQL> DROP TABLE order3;

 Table dropped.

 SQL> CREATE TABLE order3 (
   2    company      VARCHAR(32),
   3    order_number NUMBER(4) NOT NULL );

 Table created.

 SQL> INSERT INTO order3 VALUES( 'Sega', 3412 );

 1 row created.

 SQL> INSERT INTO order3 VALUES( 'W3Schools', 2312 );

 1 row created.

 SQL> INSERT INTO order3 VALUES( 'Trio', 4678 );

 1 row created.

 SQL> INSERT INTO order3 VALUES( 'W3Schools', 6798 );
 
 1 row created.

 SQL> SELECT company FROM order3;

 COMPANY
 --------------------------------
 Sega
 W3Schools
 Trio
 W3Schools

 SQL> SELECT DISTINCT company FROM order3;

 COMPANY
 --------------------------------
 Sega
 Trio
 W3Schools



Demonstration
Below is an SQL test area from W3Schools, which uses the well-known Northwind sample database. The tables here are for read only because of the problem of embedding the scripts. For a fully working example, check this by using Chrome.

SQL Statement:

Edit the SQL statement and click     to see the result, or  

Result:
The Database includes:
The Database includes:

TablenameRecord
Customers91
Categories8
Employees10
OrderDetails518
Orders196
Products77
Shippers3
Suppliers29




      The nation that destroys its soil destroys itself.    
      Forests are the lungs of our land,    
      purifying the air and giving fresh strength to our people.    
      — Franklin D. Roosevelt