YogeshChauhan . com

The SELECT DISTINCT Statement in SQL

in SQL/MySQL on August 24, 2019

When we work with database, we are going to have many different values and with the duplicate entires as well and many times we want to know the total number of unique values only. For example, let's say there is a table of international students in a table who are a part of some university. Now there will be more than one student form same country. What if you want to get the records which gives you the number of unique countries only? That's when this DISTINCT statement helps us.

The SELECT DISTINCT Statement in SQL returns distinct or different values from the database table.

Let's take look at the query results without SELECT DISTINCT Statement. I'm going to apply following query to my database. For simplicity purposes I am adding LIMIT to the query results and will only ask for 10 results.


  SELECT CountryCode FROM city LIMIT 10;

You can see the results in the DEMO link provided at the end of the article.

As you can see we see all the duplicate values in Country Code results like AFG, AGO and so on. So it doesn't tell us about the total unique CountryCode in our database. Now let's apply the same query with SELECT DISTINCT Statement.

Let's apply the following query to the database. 


SELECT DISTINCT CountryCode FROM city LIMIT 10;

As you can see the results in the DEMO (Link Below), now we have all the unique results set.

Note: The example above will not work in Firefox and Microsoft Edge! Because COUNT(DISTINCT column_name) is not supported in Microsoft Access databases.

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

How to add transparent text on top of an image using CSS?CSSWhat’s Interpolation in SCSS (Sass)?SCSSHow to change perception and behavior of a person for acceptance of new systems?Misc4 ways to create Date Objects in JavaScriptJavaScriptLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPThe SQL EXISTS OperatorSQL/MySQL