DISTINCT in DBMS
DISTINCT in DBMS
In this article, we will learn about DISTINCT in DBMS.
- Inside table columns may contain many duplicate values and sometimes we require to list only unique values, this is done by using
DISTINCTclause along with a select statement
- DISTINCT statement is used to return only unique values present in a column or combination of columns
- The DISTINCT clause is only for display purpose and will not affect the original database table
SELECT DISTINCT column1, column2, FROM table name;
Consider a sample table EMP
DISTINCT on a single column
select DISTINCT job from emp;
4 rows selected.
The above query Returns unique values present in the job column i.e. even if a value is appeared for more than once but it is displayed only for one time.
DISTINCT on more than one column
Whenever DISTINCT is applied on the select statement which contains more than one column then the combination of values of all the columns is considered as a single value even if any of the column value is different in that row it is considered as a DISTINCT value
select DISTINCT job,sal from emp;
6 rows selected.
The above query Returns 6 values because each value is considered as a key combination of two values and [ANALYST, 3000] combination would appear twice and hence it is eliminated during display.
Another example for DISTINCT
select DISTINCT ename, job from emp;
6 rows selected
The above query results in all the seven rows in the table because even if job column contains duplicate values name column contain only unique values hence the key combination both ename and job is considered as one entity and all values become unique