SQL > SQL Commands >
In SQL, the DISTINCT keyword is used in the SELECT statement to retrieve unique values from a database table. Any value that has a duplicate will only show up once.
SELECT DISTINCT "column_name"
"table_name" is the name of the table where data is stored, and "column_name" is the name of the column containing the data to be retrieved.
The examples will use the following table:
Example 1: Use DISTINCT on one column
To select all distinct stores in Table Store_Information, we key in,
SELECT DISTINCT Store_Name FROM Store_Information;
Example 2: Use DISTINCT on multiple columns
We can apply DISTINCT to multiple columns. If we want to get a list showing all unique combinations of stores and transaction dates, we would type in the following,
SELECT DISTINCT Store_Name, Txn_Date FROM Store_Information;
For these exercises, assume we have a table called Users with the following data:
1. Which of the following SQL statement is valid?
2. What's the result of the following query?
3. What's the result of the following query?