| | |
SQL DISTINCT Command
DISTINCT command in SQL collects the unique or distinct records from a field
of a table. In the student table we are interested to know how many class
records are there and the DISTINCT sql command should return class once only. So
if class five is there ten times then it should return once and if class six one
record is there then class six should return once.
So using DISTINCT sql command we can avoid duplicate records in SELECT query
There is another related command sql group by which groups the data and brings the unique names. This group by command is usually used along with count, average, minimum, maximum commands. Here we will discuss sql distinct command only
DISTINCT command
will return records once only.
SELECT DISTINCT class FROM student
This is our table and we will apply DISTINCT command to this table.
| id |
name |
class |
mark |
| 1 |
John Deo |
Four |
75 |
| 2 |
Max Ruin |
Three |
85 |
| 3 |
Arnold |
Three |
55 |
| 4 |
Krish Star |
Four |
60 |
| 5 |
John Mike |
Four |
60 |
| 6 |
Alex John |
Four |
55 |
|
|
Here again the DISTINCT command in SQL
SELECT DISTINCT class FROM 'student'
The output is displayed here
As you can see only two rows are returned and they are the distinct class in the
table
| | kashif | 05-01-2009 |
|---|
| plus2net is gearttttt.....it is 1 of the best sites of da internet..i love this site for my problem solving n learning...plus2net is doing great job...best of luck.... | | Shaveen Kaushal | 18-03-2009 |
|---|
| Thank u very much | | Kamran | 13-07-2009 |
|---|
| i want to learn sql, i am the new joiner, so please if have you any basic | | Alok Patoria | 21-01-2010 |
|---|
| i found dis helpfull.bt dere should be some heierarchy for the topics. | | plooger | 01-04-2010 |
|---|
| How could this be modified to list only the classes in which a given student is NOT enrolled? | | Rayudu | 05-04-2010 |
|---|
| How could this be modified to list only the classes in which a given student is NOT enrolled? | | xyzzx | 20-04-2010 |
|---|
| it is only a introductory knowledge.pls if posible provide a detailed description | | srinivas | 04-10-2010 |
|---|
Pls. help me in getting % of records from a table..
ex: 1000 records in a table i want 20% of records..i.e 200 records....how we can do in single statement | | Bibin | 18-11-2010 |
|---|
select top 20 percent * from Table_Name
Order by Table_Column_Name | | kishore | 04-01-2011 |
|---|
| how to display each class(field) with number of student name | | jacob | 10-05-2011 |
|---|
Hi, This site is great. I appreciate your effort
over making this valuable website
Thank you | | praveen | 21-06-2011 |
|---|
sir.,with the unique or distinct value how to take the related field values....
i.e, "select * from tablename where DISTINCT phoneno=+TextBox71.Text+"; | | jeheyr | 22-11-2011 |
|---|
You should try "select DISTINCT phoneno, etc, etc, from tablename where phoneno = +TextBox71.Text+;"
:) |
|
|
|
|
|
|