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.

 CREATE TABLE  Orders (
   Company     VARCHAR(32),
   OrderNumber NUMBER(4)  NOT NULL );

 Table created.

 INSERT INTO  Orders  VALUES( 'Sega', 3412 );
 INSERT INTO  Orders  VALUES( 'W3Schools', 2312 );
 INSERT INTO  Orders  VALUES( 'Trio', 4678 );
 INSERT INTO  Orders  VALUES( 'W3Schools', 6798 );
 SELECT  Company  FROM  Orders;

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

 SELECT DISTINCT  Company  FROM  Orders;

 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




      “I’m not in this world to live up to your expectations and    
      you’re not in this world to live up to mine.”    
      ― Bruce Lee