Distinct Keyword in SQL | SQL Select Distinct

1. Distinct Keyword in SQL

In this SQL Distinct Keyword tutorial, we will discuss the concept of distinct Keyword in SQL (Structured Query Language). Moreover, we will discuss select keyword in SQL and example of the select distinct keyword. Along with this, we will learn the parameters in distinct keywords in SQL. We will also see finding uniques values in the column and handle Null values using Distinct Clause.
Distinct Keyword in SQL – The distinct keyword is used in conjunction with the select keyword. It is used when we need to avoid the duplicate values that may be present in some specific column.
So, let’s start Select Distinct Keyword in SQL.

Distinct Keyword in SQL | SQL Select Distinct

Distinct Keyword in SQL | SQL Select Distinct

2. Example of Select Distinct Keyword in SQL

We can understand a distinct keyword example with the help of select distinct syntax. So, let’s start the example-
Do you the concept of SQL RDBMS
The basic syntax for SQL Select Distinct Keyword:

SELECT DISTINCT column1,column2 FROM table_name
column1 , column2: names of the fields of the table
table_name: from wherever we would like to fetch

This query can return all the distinctive combination of rows within the table with fields
column1, column2.
NOTE: If the distinct keyword is employed with multiple columns, the distinct combination is displayed within the result set.
To fetch distinctive names from the NAME field

SELECT DISTINCT NAME FROM Student;

Output –
NAME
Ram
RAMESH
SUJIT
SURESH
For the unique combination of rows from the whole table

SELECT DISTINCT * FROM Student;

Output:

ROLL_NONAMEADDRESSPHONEAge
1RamDelhi945512345118
2RAMESHGURGAON965243154318
3SUJITROHTAK915625313120
4SURESHDelhi915676897118

3. Parameters or Arguments in SQL Distinct Keyword

i. Expressions

For retrieval of the columns or calculation.
Let’s discuss SQL operators

ii. Tables

These are the tables that you wish to retrieve records from.

iii. WHERE Conditions

This is optional, i.e., when the conditions that must be met for the records to be selected.

iv. Note

The query will return the unique values for that expression when only one expression is provided in the DISTINCT clause.
The query will retrieve unique combinations for the expressions listed when more than one expression is provided in the DISTINCT clause.
While using the DISTINCT clause in your SQL statement, your result set will include NULL as a distinct value, the DISTINCT clause doesn’t ignore NULL values. 

4. Finding Unique Values in a Distinct Column

In this we will discuss SQL Distinct example, we will learn about the DISTINCT clause to find the unique values within one column in a table.
Have a look at SQL Create Database and Drop Database
So, let’s start SQL Distinct Column –

Supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Distinct Query in SQL-

SELECT DISTINCT state
FROM suppliers
ORDER BY state;

There will be 6 records selected. These are the results that you should see:
state
Arkansas
California
Georgia
Texas
Washington
Wisconsin
Let’s revise the SQL Data Types

5. How DISTINCT Clause Handles NULL Values

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Statement of Distinct Clause–

SELECT DISTINCT category_id
FROM products
ORDER BY category_id;

There will be 4 records selected. These are the results that you should see:
category_id
NULL
25
50
75
So, this was all in Distinct Keyword in SQL. Hope you like our explanation.

6. Conclusion: Distinct Keyword in SQL

In this SQL Distinct Keyword tutorial, we saw the concept of the distinct keyword in SQL. Moreover, we discussed SQL distinct column, SQL Distinct example, SQL distinct multiple columns, and also where use distinct SQL. Still, if any doubt regarding SQL Distinct Keyword, ask in the comment tab.
See also –
SQL RDBMS Database System
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.