The SELECT DISTINCT Statement:

The DISTINCT keyword is used to return only distinct (different) values.

The SELECT statement returns information from table columns and rows. But what if we only want to select distinct elements from table?

With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:

Syntax:

SELECT DISTINCT column_name(s) FROM table_name;

Example:

Using Order Table

Company OrderNumber
Tizag 3412
W3Schools 2312
Trio 4678
W3Schools 6798

Query:
SELECT Company FROM Orders;

The Result:

Company
Sega
W3Schools
Trio
W3Schools

Notice in the above resulted table “W3Schools” is listed twice in the result-set.

But we want to select only DIFFERENT values from the column named “Company” so that we use a SELECT DISTINCT statement like this:

Query:

SELECT DISTINCT Company FROM Orders;

The Result:

Company
Sega
W3Schools
Trio

Now you can see in above resulted table “W3Schools” is listed only once in the result-set.

 

Leave a Reply

Your email address will not be published. Required fields are marked *