The distinct keyword is used in conjunction with select keyword. It is helpful when there is need of avoiding the 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: from where we want to fetch This query will return all the unique combination 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.
- To fetch unique names from the NAME field
SELECT DISTINCT NAME FROM Student;
NAME Ram RAMESH SUJIT SURESH
- To fetch unique combination of rows from the whole table
SELECT DISTINCT * FROM Student;
ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18 3 SUJIT ROHTAK XXXXXXXXXX 20 4 SURESH Delhi XXXXXXXXXX 18
Please 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.
- SQL | WHERE Clause
- SQL | ON Clause
- Having vs Where Clause?
- SQL | Except Clause
- SQL | WITH clause
- SQL | USING Clause
- PHP | MySQL WHERE Clause
- SQL | Union Clause
- SQL | SELECT TOP Clause
- SQL | Sub queries in From Clause
- SQL | Intersect & Except clause
- SQL | With Ties Clause
- SQL | LIMIT Clause
- SQL | INTERSECT Clause
- MySQL | PARTITION BY Clause