Wednesday, 24 September 2014

SQL Distinct Keyword

The  SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate
records and fetching only unique records.
There  may  be  a  situation  when  you  have  multiple  duplicate  records  in  a  table. While  fetching  such  records,  it makes more sense to fetch only unique records instead of fetching duplicate records.
Syntax :-
The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows :-

SELECT DISTINCT column1, column2,.....columnN 
FROM table_name
WHERE [condition]

Example :-
Consider the CUSTOMERS table having the following records :-
First, let us see how the following SELECT query returns duplicate salary records :-

SELECT SALARY FROM CUSTOMERS
ORDER BY SALARY;

This would produce  the  following result where salary 2000 is coming twice which is a duplicate record from the original table.
Now, let us use DISTINCT keyword with the above SELECT query and see the result :-

SELECT DISTINCT SALARY FROM CUSTOMERS
ORDER BY SALARY;

This would produce the following result where we do not have any duplicate entry :-



No comments:

Post a Comment