Introduction to SQL SELECT DISTINCT
DISTINCT keyword in SQL is used to fetch only unique records from a database table. It is usually used in conjunction with the SELECT statement. For the uninitiated, the SELECT statement is used to fetch desired records from the datatable. Distinct keyword removes all duplicate records and fetches only unique ones. It can further be used with aggregate functions like AVG, MAX, MIN, SUM, COUNT, etc.
SELECT DISTINCT keyword is very helpful when a table contains many duplicate values and we have to fetch unique records only, for example in business scenarios where we want to know the distinct locations from where orders are being placed, how many distinct sets of customers do we have, etc.
Syntax and Parameters
The basic syntax for writing a SELECT DISTINCT statement in SQL is as follows:
SELECT DISTINCT column_name1, column_name2,...
The parameters used in the above-mentioned syntax are as follows:
- column_name1, column_name2,..: These are the columns or fields which you want to fetch in your final result set.
- table_name: This is the name of the table from which said columns or fields will be fetched.
How Does SELECT Statements with DISTINCT Keyword Work in SQL?
DISTINCT keyword in SQL filters out duplicate values from our returned results. For more simplification of its working, we may look at it in the following way:
- A SELECT DISTINCT statement first builds our overall result set with all records, i.e including duplicate values based on FROM, JOIN, WHERE, HAVING, etc statements.
- Next, it sorts the result set based on the column_name or field with which DISTINCT has been used.
- Then it performs de-duplication (i.e. removes any duplicate values) on the overall result set which was prepared in the first step.
The SELECT DISTINCT statement can be considered semantically equivalent to a GROUP BY with all returned fields mentioned in the GROUP BY clause.
Having discussed the syntax and working of SELECT DISTINCT statements, let us go ahead and try some examples to develop a great understanding of this concept.
Examples of SQL SELECT DISTINCT
In order to illustrate the working of SQL DISTINCT keyword, what could be better than creating a dummy database table. Let us create a table called “customers”.
We can use the code snippet given below for performing this task.
CREATE TABLE public.customers
customer_id integer NOT NULL,
sale_date date NOT NULL,
sale_amount numeric NOT NULL,
salesperson character varying(255),
store_state character varying(255),
order_id character varying(255)
We have successfully created the table. Now let us insert some records in it to work with.
INSERT INTO public.customers(
customer_id, sale_date, sale_amount, salesperson, store_state, order_id)
VALUES (1001,'2020-05-23',1200,'Raj K','KA','1001'),
We have successfully inserted 7 records in the table. The customer’s table after performing the above insertions looks something as follows:
SELECT * FROM customers;
Now let us try some examples using the DISTINCT keyword in conjunction with the SELECT statement.
Find the customer ids of all the unique customers who have bought or ordered something from the departmental store.
SELECT DISTINCT customer_id
In this example, we can see that the distinct keyword has fetched only the unique customer ids.
Find all the unique dates on which sales were made at the departmental store.
SELECT DISTINCT sale_date
Find all the distinct store locations where the departmental store is located.
SELECT DISTINCT store_state
ORDER BY store_state ASC;
Explanation: The thing with NULL values and the DISTINCT keyword is that DISTINCT lets the first NULL in the final result set and removes all other subsequent NULL values. This can be observed in this example also. We had two NULL values in the store_state column, but DISTINCT has made room for only one NULL in the final result set.
Find all the unique orders that were made on a particular date in the departmental store.
SELECT DISTINCT sale_date,
ORDER BY sale_date;
Find the sum of revenue collected for all the unique orders that were made on a particular date at a particular store of the departmental store.
SELECT DISTINCT sale_date, store_state,
sum(sale_amount) as total_sales
GROUP BY store_state, sale_date
ORDER BY sale_date ASC;
Find all the unique customers and the sum of total money spent by them at the departmental store.
SELECT DISTINCT customer_id,
sum(sale_amount) as total_sales
GROUP BY customer_id
ORDER BY customer_id ASC;
Find all the unique salespeople working in the departmental store.
SELECT DISTINCT salesperson
SQL 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 select distinct statements with aggregation like min, max, avg, count, etc. Below is the syntax of the select distinct statements.
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 that was used to select the specified data from a table. We can select the data as per the condition which was given in the query. We can use select with a distinct statement to retrieve unique records from the table.
- Name of column1 to the name of column N – This is the table column that was used with distinct keywords to retrieve data from a table. Suppose we have used a distinct statement with a specified column it will retrieve the distinct records from the table.
- Name of the 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 retrieved the specified row which we have defined in the where condition. Where a condition is very useful to retrieve a specific condition unique row by using a statement.
- Order by – This condition is used to fetch the records as per the specified order. The default condition of the order is ascending order. Suppose we have given the explicit condition then it will retrieve the data as per the specified order which was we have given into the query.
- At the time of using only one expression by using the distinct clause, our query will return the unique values from the expressions.
- Suppose we are defining more than one expression our distinct clause will return the unique combination of the expressions.
- We cannot ignore the null values by using the statement, 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 sql select distinct statements we can avoid the redundancy of data. This statement will be eliminating the appearance of repetitive data.
- The below example shows statement keyword is case sensitive. In the first example, we have used keywords in the uppercase letter while in the second example we have used keywords in lowercase letters in both times it will return 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 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, and 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 be returning 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;
SQL DISTINCT keyword is used mostly in the conjugation of SELECT statements to fetch only unique records from the specified table by removing duplicate values from the final result set.
This is a guide to SQL SELECT DISTINCT. Here we discuss the Introduction of SELECT DISTINCT in SQL and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –
- Top 5 Examples of SQL Server Substring
- Overview of Triggers in SQL
- Introduction to SQL Arithmetic Operators
- ANY in SQL | Examples | Syntax