Task 4 – SQL SELECT DISTINCT
- This topic is empty.
-
AuthorPosts
-
October 10, 2017 at 4:37 am #22708
Mayank Goyal
KeymasterThe 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 DISTINCT [i]column1[/i],[i] column2, …[/i]
FROM [i]table_name[/i];
[hr]
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:Example
Code:SELECT Country FROM Customers;
Now, let us use the DISTINCT keyword with the above SELECT statement and see the result.Try it Yourself »
[hr]
SELECT DISTINCT ExamplesThe following SQL statement selects only the DISTINCT values from the “Country” column in the “Customers” table:Example
Code:SELECT DISTINCT Country FROM Customers;
The following SQL statement lists the number of different (distinct) customer countries:Try it Yourself »
Code:SELECT COUNT(DISTINCT Country) FROM Customers;
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 Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers); -
AuthorPosts
- You must be logged in to reply to this topic.