SQL SELECT DISTINCT
The SELECT DISTINCT
statement in SQL is used to retrieve only the unique values from a column (or a set of columns). It filters out duplicate rows from the query's results, giving you a list of distinct values.
SELECT DISTINCT: Definition and Usage
SELECT DISTINCT
is very helpful when you need only a list of unique entries and don't want to see repeated values. This is commonly used to get a list of all available options for a particular column (such as distinct countries, product categories, or any other attribute where you're interested only in unique entries).
Syntax
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
Example
Retrieving Unique Countries
This query selects only the unique country names from the 'Customers' table. (Assumes a 'Customers' table exists with a 'Country' column.)
Syntax
SELECT DISTINCT Country FROM Customers;
Output
Country
-------
(A list of unique country names from the Customers table will be displayed here.)