The SQL DISTINCT query is used with the SELECT keyword retrieves only unique data values depending on the fields you have specified after it. To illustrate it we use emp table as shown below.
Select ename from EMP;
EMP table has several ename in it, which retrieves the ename for each tuple? Typically, some enames will appear more than only once in the query result, that is, duplicate result tuples are not automatically eliminated.
Inserting the keyword distinct after the keyword select, however, forces the elimination of duplicates from the query result.
You can use the DISTINCT keyword with more than one fields. We illustrate it with our example.
SELECT DISTINCT ename, job FROM EMP;
ENAME | JOB | |
ADAMS | CLERK | |
ALLEN | SALESMAN | |
BLAKE | MANAGER | |
CLERK | MANAGER | |
FORD | ANALYST | |
JAMES | CLERK | |
JONES | MANAGER | |
KING | PRESIDENT | |
MARTIN | SALESMAN | |
MILLER | CLERK | |
SCOTT | ANALYST | |
SMITH | CLERK | |
TURNER | SALESMAN | |
WARD | SALESMAN |
What the above statement will do is to return all unique combinations between ename and job fields.