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

Java Program to Convert Binary to Decimal - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala