SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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
Related Tutorial
Group by
SQL Maximum value
SQL Average
SQL Rename table
PHP MySQL functions
Here again the DISTINCT command in SQL

SELECT DISTINCT class FROM 'student'

The output is displayed here
class
Four
Three
As you can see only two rows are returned and they are the distinct class in the table




kashif05-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 Kaushal18-03-2009
Thank u very much
Kamran13-07-2009
i want to learn sql, i am the new joiner, so please if have you any basic
Alok Patoria21-01-2010
i found dis helpfull.bt dere should be some heierarchy for the topics.
plooger01-04-2010
How could this be modified to list only the classes in which a given student is NOT enrolled?
Rayudu05-04-2010
How could this be modified to list only the classes in which a given student is NOT enrolled?
xyzzx20-04-2010
it is only a introductory knowledge.pls if posible provide a detailed description
srinivas04-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
Bibin18-11-2010
select top 20 percent * from Table_Name
Order by Table_Column_Name
kishore04-01-2011
how to display each class(field) with number of student name
jacob10-05-2011
Hi, This site is great. I appreciate your effort
over making this valuable website
Thank you
praveen21-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+";
jeheyr22-11-2011
You should try "select DISTINCT phoneno, etc, etc, from tablename where phoneno = +TextBox71.Text+;"
:)
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
SQL Tutorial List
SQL Commands
SQL Sections
Date & Time
Join Table
String
Math