The distinct keyword is used in conjunction with select keyword. It is helpful when there is a need of avoiding duplicate values present in any specific columns/table. When we use distinct keyword only the unique values are fetched.
SELECT DISTINCT column1, column2 FROM table_name
column1, column2 : Names of the fields of the table.
table_name : Table from where we want to fetch the records.
This query will return all the unique combinations of rows in the table with fields column1, column2.
NOTE: If distinct keyword is used with multiple columns, the distinct combination is displayed in the result set.
Table – Student
- To fetch unique names from the NAME field –
SELECT DISTINCT NAME FROM Student;
- To fetch a unique combination of rows from the whole table –
SELECT DISTINCT * FROM Student;
Note : Without the keyword distinct in both the above examples 6 records would have been fetched instead of 4, since in the original table there are 6 records with the duplicate values.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Difference between Having clause and Group by clause
- Distinct clause in MS SQL Server
- 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
- SQL query using COUNT and HAVING clause
- Difference between Where and Having Clause in SQL
- Difference between order by and group by clause in SQL
- Difference between From and Where Clause in SQL
Improved By : Anshika Goyal