Krivalar Tutorials 
Krivalar Tutorials



SQL Distinct - Select distinct query to find unique values

<< Previous - SQL Select Table

Next - SQL Order By >>






  • Select Distinct is used to eliminate the duplicate values in the table.
  • A table contains many columns in which some columns are often having duplicate values. Sometimes, if you want to list unique values, in that case, you can use the DISTINCT keyword.
  • While writing a query, this DISTINCT keyword usually combines with SELECT statement.

Syntax


SELECT DISTINCT column-name FROM table-name:
WHERE condition 1 AND condition 2 AND .......condition N ;

EXAMPLE

Following is the student table in the collegedb database.

+--------+---------+------+-----------+
| RollNo | Name    | Age  | City      |
+--------+---------+------+-----------+
|      1 | Aruna   |   18 | Chennai   |
|      2 | Varun   |   19 | Bangalore |
|      3 | Ara     |   19 | Kerala    |
|      4 | Markdin |   18 | Mumbai    |
|      5 | Kannan  |   20 | Kerala    |
|      6 | Kanika  |   18 | Chennai   |
|      7 | Jose    |   19 | Kerala    |
+--------+---------+------+-----------+


The following query will remove duplicate values from student table.



mysql> SELECT DISTINCT City FROM student;
+-----------+
| City      |
+-----------+
| Chennai   |
| Bangalore |
| Kerala    |
| Mumbai    |
+-----------+
4 rows in set (1.71 sec)


<< Previous - SQL Select Table

Next - SQL Order By >>









Searching using Binary Search Tree