Task 4 - SQL SELECT DISTINCT

Post Reply
User avatar
Mayank
Site Admin
Posts: 217
Joined: 06 Oct 2017, 11:19
Status: Offline

10 Oct 2017, 10:07

The SQL SELECT DISTINCT StatementThe SELECT DISTINCT statement is used to return only distinct (different) values.
 Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.The SELECT DISTINCT statement is used to return only distinct (different) values.
 SELECT DISTINCT Syntax

Code: Select all

SELECT DISTINCT [i]column1[/i],[i] column2, ...[/i]
FROM [i]table_name[/i];

Demo DatabaseBelow is a selection from the "Customers" table in the Northwind sample database:SELECT ExampleThe following SQL statement selects all (and duplicate) values from the "Country" column in the "Customers" table:

Image
Example

Code: Select all

SELECT Country FROM Customers;
Try it Yourself »Now, let us use the DISTINCT keyword with the above SELECT statement and see the result.

SELECT DISTINCT ExamplesThe following SQL statement selects only the DISTINCT values from the "Country" column in the "Customers" table:Example

Code: Select all

SELECT DISTINCT Country FROM Customers;
Try it Yourself »The following SQL statement lists the number of different (distinct) customer countries:

Code: Select all

SELECT COUNT(DISTINCT Country) FROM Customers;
ExampleTry it Yourself »
 Note: The example above will not work in Firefox and Microsoft Edge! Because COUNT(DISTINCT column_name) is not supported in Microsoft Access databases. Firefox and Microsoft Edge are using Microsoft Access in our examples.Here is the workaround for MS Access:Example

Code: Select all

SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers);
Try it Yourself »
Mayank Goyal

Post Reply
Actuarial Blogs Insurology

icon icon