Beginners Guide to SQL – SQL DISTINCT Clause

Hits: 4

(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:

SELECT DISTINCT column_list FROM table_name;

Here, column_list is a comma separated list of column or field names of a database table (e.g. nameagecountry, 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

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

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

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!