SQL DISTINCT KEYWORD -The Coding Shala

Home >> Learn SQL >> SQL DISTINCT Keyword

SQL DISTINCT KEYWORD

Tables in the database often contain duplicate values, SQL  'DISTINCT' keyword is used in conjunction with the SELECT statement to eliminate all the duplicate records and fetching only unique records.
SQL DISTINCT KEYWORD - The Coding Shala

SQL DISTINCT KEYWORD SYNTAX

The basic SQL query with the DISTINCT keyword is as follows - 
 SELECT DISTINCT column1, column2, ...  
 FROM table_name;  

SQL DISTINCT KEYWORD EXAMPLE 

The following table Persons will be used for the examples - 

 PersonID     LastName     FirstName     Address     City  
 1           Saini          Akshay        A-101      Pune  
 2            last          first         A-101      Pune  
 3            Smith           Jhon         B-02       Mum  

The following query without 'DISTINCT' keyword will fetch 'Address' from the table - 

SQL >> select Address from Persons;  
 
OUTPUT >>  
 Address  
 A-101  
 A-101  
 B-02  

If we use 'DISTINCT' keyword then it will give only unique values of address - 

SQL >> select distinct Address from Persons;  
 
OUTPUT >>  
 Address  
 A-101  
 B-02 


Other Posts You May Like
Prev<< SQL CONCAT Function                                                                  NEXT >>SQL GROUP BY
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

New Year Chaos Solution - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala