Introduction
While handling the databases, sometimes we might encounter data with various duplicate records, which might cause discrepancies like excess memory usage. While carrying various operations, we may want to eliminate these entries and fetch only the unique records. But to do this by checking all the entries and then removing the duplicate entries one by one will be very inefficient, especially if there are many duplicate entries.
So to efficiently fetch only the unique records from the database, we use the DISTINCT keyword, which is used along with the SELECT keyword, which is like a command to the database to select columns from the table but unique entries.
Syntax
Let’s have a look at the syntax of the distinct keyword-
SELECT DISTINCT col1, col2, col3, …colN FROM table_name WHERE [condition]; |
Example
Let's see an example of using the DISTINCT keyword to fetch unique records from a database.
Consider We have the following table named “Students”-
Roll No. |
Name |
Age |
Marks |
UID |
1 |
Vivek Dixit |
21 |
89 |
9845678 |
2 |
Apoorv Singh |
20 |
91 |
3148965 |
3 |
Ayush Singh |
21 |
88 |
7995165 |
4 |
Prashant Yadav |
19 |
91 |
3164646 |
5 |
Gorakh Nath |
20 |
91 |
3154668 |
Using only SELECT keyword
Now, we want to fetch the marks obtained by all the students. To do this, we will use the SELECT keyword, which will show the marks-
Query-
SELECT Marks FROM Students; |
Output-
Marks |
89 |
91 |
88 |
91 |
91 |
Since we have few students who had equal marks, duplicate entries are in the result set.
Using the DISTINCT keyword
Now let's use the DISTINCT keyword along with the select keyword, then check the output-
Query-
SELECT DISTINCT Marks FROM Students; |
Output-
Marks |
89 |
91 |
88 |
We get all the unique marks obtained by the students. And all the duplicate entries have been removed from the result set.
Using the COUNT keyword
There is one more scenario when we might look for the total number of unique entries in a column rather than the unique ones themselves. In that case, we can use the count keyword. It returns the total number of entries in the result set obtained using the distinct and select keywords.
The syntax for this is as follows-
SELECT COUNT(DISTINCT col1, col2, col3, …colN) FROM table_name WHERE [condition]; |
Let’s see an example using it on the table given above-
QUERY-
SELECT COUNT(DISTINCT Marks) FROM Students; |
OUTPUT-
COUNT(DISTINCT Marks) |
3 |
Also see, Tcl Commands in SQL