DISTINCT Command in SQL a very important keyword for a select statement In a table. In a particular table, columns may contain duplicate or repeated values and you want to fetch only distinct values from the table.
The DISTINCT keyword can be used to return only distinct (different) values. for eg: in a city column if New york exists more than one time than with the help of the distinct keyword we eliminate duplicate values for the city column. Some key points are given below-
- DISTINCT for multiple columns is not supported.
- Select DISTINCT returns only distinct (different) values from the table.
- SELECT DISTINCT eliminates duplicate rows from the table.
- DISTINCT can be used with aggregates functions like COUNT, AVG, MAX, etc.
The syntax of distinct in SQL is given below-
Distinct in SQL Syntax
Select Distinct Column_Name(s) From Table_Name;
SELECT DISTINCT Department FROM employee ;
SELECT DISTINCT Example
The “Persons” table:
Now we want to select only the distinct values from the column named “City” from the table above.
We use the following SELECT statement-
SELECT DISTINCT City FROM person ;
SELECT COUNT(DISTINCT City) FROM employee;