Overview

Distinct Statement is used to find out the distinct values in any of the columns.

Syntax

Syntax for Distinct Statement is

SELECT DISTINCT column_name FROM table_name;

Example

Let us get a clear picture with the help of an example

Consider table hostel_list containing the list of hostels

+----+-------------------+-----------+----------+
| id | name | occupancy | capacity |
+----+-------------------+-----------+----------+
|  1 | ASHOK BHAVAN | 2 | 1090 |
|  2 | KAILASH BHAVAN | 2 | 990 |
|  3 | RUDRAKSH BHAVAN | 2 | 990 |
|  4 | KAVERI BHAVAN | 1 | 690 |
|  5 | BHAGIRATHI BHAVAN | 4 | 2140 |
|  6 | NARMADA BHAVAN | 6 | 3240 |
|  7 | GANGA BHAVAN | 1 | 780 |
|  8 | YAMUNA BHAVAN | 8 | 4580 |
+----+-------------------+-----------+----------+

And suppose you want to fetch what distinct type of occupancy hostels have, Than you can run the following SQL Query

SELECT DISTINCT occupancy FROM hostel_list;
+-----------+
| occupancy |
+-----------+
|         2 |
|         1 |
|         4 |
|         6 |
|         8 |
+-----------+


This article is contributed by Ankur Rastogi on Tue Mar 22 2016 22:01:24 GMT+0530 (IST)

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.