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.
1SELECT  DISTINCT column_name(s)
2  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.

01CREATE TABLE  Orders (
02  Company      VARCHAR(32),
03  OrderNumber  NUMBER(4)  NOT NULL);
04 
05Table created.
06 
07INSERT INTO  Orders  VALUES ('Sega', 3412);
08INSERT INTO  Orders  VALUES ('W3Schools', 2312);
09INSERT INTO  Orders  VALUES ('Trio', 4678);
10INSERT INTO  Orders  VALUES ('W3Schools', 6798);
11SELECT  Company  FROM  Orders;
12 
13COMPANY
14--------------------------------
15Sega
16W3Schools
17Trio
18W3Schools
19 
20SELECT DISTINCT  Company  FROM  Orders;
21 
22COMPANY
23--------------------------------
24Sega
25Trio
26W3Schools


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