Definition of Select Distinct
SQL select distinct is used with select statements in any RDBMS database system. Select distinct is retrieving all unique records from the table. It will eliminate all the duplicate records from the table. Suppose our table contains more duplicate records, and we want to fetch unique records when we use SQL to select a distinct keyword in our query.
Introduction to select distinct
- It is more useful and important in any RDBMS system to fetch unique records from the table.
- In ANSI standard, it is defined as the official RDBMS keyword. As we know, it is a conjunction of a SQL select statement. If we want to avoid duplicate data from the table or column, then we use this statement.
- We can use keywords on specified columns or specified tables. This function will remove duplicate records from the function. It will work on a single column.
Select Distinct statement
- It will not work on multiple columns; we can use the same on a single column from the table from which we have retrieved the unique records.
- We can use this statement with aggregation like min, max, avg, count, etc. Below is the syntax of the select distinct statement.
Select DISTINCT name_of_column1, name_of_column2, …., name_of_columnN
- Select DISTINCT name_of_column1, name_of_column2, …., name_of_columnN
From name_of_table where condition;
- Select DISTINCT name_of_column from name_of_table order by name_of_column;
Below is the description syntax as follows.
- Select – This is the SQL statement to select the specified data from a table. We can select the data per the condition given in the query. We can use select with distinct statement to retrieve unique records from the table.
- Name of column1 to name of column N – This is the table column used with distinct keywords to retrieve data from a table. Suppose we have used distinct statement with a specified column; it will retrieve the distinct records from the table.
- Name of table – This is the name of the table from which we have retrieved the unique records. We need to define the table name while using statements.
- Where condition – The where condition in any statement of SQL will be used to select or retrieve the specified row we have defined in the condition. Where a condition is very useful to retrieve a specific condition unique row by using statement.
- Order by – This condition is used to fetch the records per specified order. The default condition of the order is ascending order. Suppose we have given the explicit condition, then it will retrieve the data per the specified order we have given into the query.
- Our query will return the unique values from the expressions when using only one expression by using the distinct clause.
- Suppose we are defining more than one expression; our distinct clause will return the unique combination of the expressions.
- By using the statement, we cannot ignore the null values; while retrieving data; our result will contain the distinct value as null.
- In the below example, we can see that sql select statement will not ignore the null values from the specified column on which we are using distinct clauses.
Select distinct id, name from sql_distinct;
- By using this statement we can avoid the redundancy of data. This statement will eliminate the appearance of repetitive data.
- The below example shows statement keyword is case sensitive. In the first example, we have used keywords in an uppercase letter while in the second example we have used keywords in lowercase letters in both times it will return the same result without issuing any error.
select DISTINCT id, name from sql_distinct; select distinct id, name from sql_distinct;
- We are using the sql_distinct table from a distinct database. We are using the Postgres database to see the example of sql select distinct. Below is the sample data from the sql_distinct table.
Select * from sql_distinct;
- In the below example, we have found the distinct count of records from the id column. We can see that the unique records count of the id table is 4.
SELECT COUNT(DISTINCT id) FROM sql_distinct;
- In the below example, we have found the distinct records from the sql_distinct table. Also, we are using order by clause on the id column as follows.
select distinct id, name from sql_distinct order by id;
- The below example shows the use of a single column with sql select statement. In the below example, we are using only the id column.
Select distinct id from sql_distinct;
- In the below example we are using two column names with an order by clause with sql select distinct statement. We are using the id, name column as follows.
Select distinct id, name from sql_distinct order by id, name;
- The below example shows a statement with the where condition. We are using where condition on id and name column by using sql select distinct statement. It will return only single values from the table.
Select distinct id from sql_distinct where id = 103 and name = 'PQR' order by id;
- The below example shows with all the columns from the table are as follows.
Select distinct id, name from sql_distinct;
select distinct will eliminates all the duplicate records from the table. It is used with select statement in any of the RDBMS database systems. Select distinct is retrieving all unique records from the table. We can use SQL select distinct keywords on a specified column.
This is a guide to Select Distinct. Here we discuss the definition, Introduction, and examples along with code implementation and output. You may also have a look at the following articles to learn more –