Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Distinct

Author  Topic 

shaggy
Posting Yak Master

248 Posts

Posted - 2009-11-24 : 06:55:26
Hi,

should i need to include order by asc clause for column which i used in select list with distinct keyword.

Eg:
select distinct col1 from tablename order by col1

kindly advice

Please reply any web site for reference

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-24 : 07:23:57
In relation to your given example - no.

In my simple words:
The distinct is used to build the record set.
The order by is used to order the already builded record set.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-11-24 : 07:59:40
If you are a beginner you can follow w3schools website which will help you a lot in learning the basics..
Link : http://www.w3schools.com/sql/default.asp

If you are strong in basics you can learn a lot in msdn website. This site is the dictionary for MS SQL Server..
Link : http://msdn.microsoft.com/en-us/sqlserver/default.aspx

Wish you ALL THE BEST!!

Balaji.K
Go to Top of Page

shaggy
Posting Yak Master

248 Posts

Posted - 2009-11-24 : 08:13:40
Thanks webfred
when we use distinct automatically the recordset is sorted according to the distinct column list
iam looking for any documents whether the above said is exact
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-11-24 : 08:20:00
Hi

If we distinct col the records are automatically sorted.

For Desc

SELECT DISTINCT(OBJECT_ID) FROM SYS.OBJECTS ORDER BY OBJECT_ID desc


-------------------------
R...
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-24 : 08:32:23
quote:
Originally posted by shaggy

Thanks webfred
when we use distinct automatically the recordset is sorted according to the distinct column list
iam looking for any documents whether the above said is exact



In my simple words:
It is a side effect from the work to have distinct data...

Don't know where it is to find in a document.
But I NEVER would assume that it always will happen like expected.
I always would write my ORDER BY if I want a sorted output.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -