Select Distinct in Sql
SELECT DISTINCT:- The SQL DISTINCT keyword is used with SELECT statement to eliminate all the duplicate records and extract only those records which are unique.



In a table, there is a chance of a duplicate value and sometimes we want to retrieve only unique values. In such scenarios, SQL SELECT DISTINCT statement is used.
SYNTAX:-
SELECT DISTINCT column1, column2, ...
FROM table_name
WHERE condition;
EXAMPLE:-
Consider the STUDENT table having the following records −
SELECT EXAMPLE Without DISTINCT:-
The following SQL Statement Selects all (including duplicates) values from the "Hometown" column in the "STUDENT" Table:-
EXAMPLE:-
SELECT Hometown
FROM STUDENT;
OUTPUT:-
Without using DISTINCT Keyword it will give all the records of the column name "Hometown" including duplicates.
SELECT DISTINCT EXAMPLE:-
The following SQL statement selects only the DISTINCT Values from the "Hometown" column in the "STUDENT" table"-
EXAMPLE:-
SELECT DISTINCT Hometown
FROM STUDENT;
OUTPUT:-
This output will produce only the unique values for the column "Hometown" .
Comments
Post a Comment