SQL Basics | DML: Data Manipulation Language | DISTINCT
The DISTINCT keyword removes duplicate rows from the result of a SELECT statement.
With a SELECT statement, you can load all data from one or more fields in a table. Even when the same value is repeated, all data can be loaded. In data processing, you often need to know which different values exist. In other words, rather than the number of times each value appears, you need to know which distinct values are present in the table or field. SQL makes this easy. Add DISTINCT after SELECT.
DISTINCT syntax
The DISTINCT statement is as follows.
SELECT DISTINCT column_name1, column_name2, ...
FROM table_name;
DISTINCT example
For example, suppose you want to find the names of different stores in the following Store_Information table.
store_information table
| store_name | sales | txn_date |
|---|---|---|
| Los Angeles | 1500 | Jan-05-2018 |
| San Diego | 250 | Jan-07-2018 |
| Los Angeles | 300 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |
Enter the following command.
SELECT DISTINCT store_name FROM store_information;
The result is as follows.
| store_name |
|---|
| Los Angeles |
| San Diego |
| Boston |