Distinct Keyword in SQL | SQL Select Distinct
Don't become Obsolete & get a Pink Slip
Follow DataFlair on Google News & Stay ahead of the game
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.
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
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;
For the unique combination of rows from the whole table
SELECT DISTINCT * FROM Student;
3. Parameters or Arguments in SQL Distinct Keyword
For retrieval of the columns or calculation.
Let’s discuss SQL operators
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.
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 –
|700||Dole Food Company||Westlake Village||California|
|900||Electronic Arts||Redwood City||California|
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:
Let’s revise the SQL Data Types
5. How DISTINCT Clause Handles NULL Values
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:
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