(SQL Tutorials for Citizen Data Scientist)
SQL DISTINCT Clause
In this tutorial you will learn how to remove duplicate values from a result set.
Retrieving Distinct Values
When fetching data from a database table, the result set may contain duplicate rows or values. If you want to remove these duplicate values you can specify the keyword DISTINCT
directly after the SELECT
keyword, as demonstrated below:
Syntax
The DISTINCT
clause is used to remove duplicate rows from the result set:
column_list
FROM table_name
;Here, column_list is a comma separated list of column or field names of a database table (e.g. name, age, country, etc.) whose values you want to fetch.
Note: The DISTINCT
clause behaves similar to the UNIQUE
constraint, except in the way it treats nulls. Two NULL
values are considered unique, while at the same time they are not considered distinct from each other.
Let’s check out some examples that demonstrate how it actually works.
Suppose we’ve a customers table in our database with the following records:
+---------+--------------------+-----------+-------------+ | cust_id | cust_name | city | postal_code | +---------+--------------------+-----------+-------------+ | 1 | Maria Anders | Berlin | 12209 | | 2 | Fran Wilson | Madrid | 28023 | | 3 | Dominique Perrier | Paris | 75016 | | 4 | Martin Blank | Turin | 10100 | | 5 | Thomas Hardy | Portland | 97219 | | 6 | Christina Aguilera | Madrid | 28001 | +---------+--------------------+-----------+-------------+
Now execute the following statement which returns all the rows from the city column of this table.
Example
SELECT city FROM customers;
After execution, you’ll get the output something like this:
+-----------+ | city | +-----------+ | Berlin | | Madrid | | Paris | | Turin | | Portland | | Madrid | +-----------+
If you see the output carefully, you’ll find the city “Madrid” appears two times in our result set, which is not good. Well, let’s fix this problem.
Removing Duplicate Data
The following statement uses DISTINCT
to generate a list of all city in the customers table.
Example
SELECT DISTINCT city FROM customers;
After executing the above command, you’ll get the output something like this:
+-----------+ | city | +-----------+ | Berlin | | Madrid | | Paris | | Turin | | Portland | +-----------+
As you see this time there is no duplicate values in our result set.
Beginners Guide to SQL – SQL DISTINCT Clause
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.