Other related articles:

Recently viewed articles:

SQL DISTINCT

To get only unique values in a column, you would use the DISTINCT keyword. We just need to add DISTINCT keyword to the SELECT statement’s column listing, just after the SELECT keyword. For example, if you want to check how many different department number are there in emp table, you could try a query similar to the following:

SELECT deptno FROM emp;

this query will gives you the following results:

image 3

 

As you can see, there are many department numbers which are listed many times because there are number of employees from that department. But if we add the DISTINCT keyword, it will simply list the unique departments that employees belongs to:

SELECT DISTINCT deptno FROM emp;

this modified query will give the following results:

image 4

Now every department is mentioned just once. DISTINCT keyword works on all columns depending that

all the columns mentioned in the SELECT statement must be unique. If we change the above query

to include empid, which is unique for every single row, and execute the query, you will see all

the rows:

SELECT DISTINCT deptno, empid FROM emp;

Image 5