by Dinesh Thakur Category: SQL Tutorials

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.

Related SQL


About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content