SQL SELECT DISTINCT Statement

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_IDFirst_NameCityAgeGrade
1AkashDelhi18A2
2BhaveshKanpur19A1
3YashDelhi20A2
4BhavnaDelhi19B1
5YatinLucknow20B1
6IshikaGhaziabad19C1
7VivekGoa20B2

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

Leave a Reply

Your email address will not be published.