In this article, we will be discussing about distinct clause in MS SQL Server.
Introduction :
- A table has a maximum of 1000 rows constituted.
- The probability of repeated rows otherwise called as duplicates in SQL terms might occur in the table.
- In SQL Server, distinct is a term used to remove duplicates from a table.
Basic syntax :
select distinct select_list from table_name
Example :
Sample table – Student
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | Deepa | EEE |
To remove the duplicates, the query must be written as follows –
select distinct roll number, name, course from student
The output will be as follows –
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | Deepa | EEE |
As there are no duplicates, same number of rows are returned. Let us see an example having duplicates.
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | Deepa | EEE |
| 111 | Riya | CSE |
The query should be written as –
select distinct roll number, name, course from student
Output is –
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | Deepa | EEE |
There was a duplicate named Riya but by using distinct, the duplicates are removed.
Consider another example of null values.
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | – | – |
Note that the name and the course are null in case of Deepa, so the query is written as –
select distinct name, roll number, course from student
The output is as follows –
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | Null |
In case of null values, distinct removes all the other null values and restores only one null value as shown in the output.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Where clause in MS SQL Server
- Having clause in MS SQL Server
- Group by clause in MS SQL Server
- Difference between Having clause and Group by clause
- SQL | Distinct Clause
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | WHERE Clause
- SQL | SELECT TOP Clause
- SQL | Union Clause
- SQL | WITH clause
- SQL | Except Clause
- SQL | OFFSET-FETCH Clause
- Having vs Where Clause in SQL
- SQL | LIMIT Clause
- SQL | Intersect & Except clause
- SQL | USING Clause
- SQL | With Ties Clause
- SQL | Sub queries in From Clause
- SQL | ON Clause
- Combining aggregate and non-aggregate values in SQL using Joins and Over clause
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

