In this tutorial, we will learn how to use the DISTINCT Statement in SQL Query with an example.

The SQL DISTINCT statement returns unique data from the database table.
syntax of the Distinct
statement
SELECT DISTINCT column1, column2, ... FROM table_name;
Database Example
Consider the student
table having the following records:
Student_ID | First_Name | City | Age | Grade |
---|---|---|---|---|
1 | Akash | Delhi | 18 | A2 |
2 | Bhavesh | Kanpur | 19 | A1 |
3 | Yash | Delhi | 20 | A2 |
4 | Bhavna | Delhi | 19 | B1 |
5 | Yatin | Lucknow | 20 | B1 |
6 | Ishika | Ghaziabad | 19 | C1 |
7 | Vivek | Goa | 20 | B2 |
First, we run the SELECT query on the City column in the above table, which returns the duplicate City records.
SELECT `City` FROM student;
Output:-
City |
---|
Delhi |
Kanpur |
Delhi |
Delhi |
Lucknow |
Ghaziabad |
Goa |
Now, we run the above Query with DISTINCT
Keyword, It will return only unique city records.
SELECT DISTINCT `City` FROM student;
Output:-
City |
---|
Delhi |
Kanpur |
Lucknow |
Ghaziabad |
Goa |
Be First to Comment