Slide 7.8: SQL select statement
Slide 7.10: SQL where clause
Home

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
The following is an SQL test area from W3Schools, which uses the well-known Northwind sample database and the tables are for read only.
For security reasons, the following demonstration may only work in Chrome.

SQL Statement:

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

Result:

Click “Run SQL” to execute the SQL statement above.
W3Schools has created an SQL database in your browser.
The menu to the right displays the database, and will reflect any changes.
Feel free to experiment with any SQL statement.
You can restore the database at any time.
The Database includes:
The Database includes:

TablenamesRecords
Customers91
Categories8
Employees10
OrderDetails518
Orders196
Products77
Shippers3
Suppliers29