Tutorial on SQL DISTINCT statement

 
The “SELECT” keyword permit user to grab all available information from a column (or columns) on a table. This may implies that there will be redundancies.
 
In a situation where we only wish to select each DISTINCT element, this is easy to perform in SQL. All we require to do is to add DISTINCT after SELECT.
 
The Syntax is shown below:
 
SELECT DISTINCT “column_name”
FROM “table_name”;
 
For example, to choose all distinct stores present in the Table Product_Information,

Table Product_Information

Country_Name Sales Transaction_Date
L A 1500 Jan-05-1994
San Diego 550 Jan-07-1996
L A 3500 Jan-08-2000
Boston 700 Jan-08-1998

 
we key in,
SELECT DISTINCT Country_Name FROM Store_Information;

Result:

Country Name
 
L A
San Diego
Boston

Practice Exercise

For perform these exercises, assume we contain a table known as Users having the following data:
 
Table name : Users

First_Name Last_Name Birth_Date Gender Join_Date
Sophie Lee Jan-05-1960 F Apr-05-2015
Richard Brown Jan-07-1975 M Apr-05-2015
Jamal Santo Oct-08-1983 M Apr-09-2015
Casey Healy Sep-20-1969 M Apr-09-2015
Jill Wilkes Nov-20-1979 F Apr-15-2015

 
Exercise 1) which of the following SQL statement is valid?
 
a) SELECT DISTINCT * FROM Users;
b) SELECT DISTINCT First_Name FROM Users;
c) SELECT DISTINCT First_Name Last_Name FROM Users;
 
Exercise 2) what’s the result of the following query?
 
SELECT DISTINCT Join_Date From Users;
 
Exercise 3) what’s the result of the following query?
 
SELECT DISTINCT Gender, Join_Date From Users;

Answers :

Exercise 1) b)
 
Exercise 2) The result is:

Join_Date
Apr-05-2015
Apr-09-2015
Apr-15-2015

 
Exercise 3) The result is:

Gender Join_Date
F Apr-05-2015
M Apr-05-2015
M Apr-09-2015
F Apr-15-2015

Kristin is a content strategist at Techarex Networks. Kristin follows the B2B technology space closely and loves to write on the latest changes in technology, futuretech and fixes for day to day how to issues. Besides writing Kristin also loves music, moves and skating.