+ Start a Discussion
Vijaya Kumar RegantiVijaya Kumar Reganti 

What is use of Order By clause in SOQL

Hi Friends,

 

I have a query as follows.

 

[SELECT id,Name,Body_of_topic__c,Subject_Line__c FROM Topics__c Order By Sort_Order__c]

 


where  Sort_Order__c field is a number field. So please tell me the result of this query and the importance of the Order By clause. What happens if Sort_Order__c is 1,2,3  etc.

 

Thanks,

 

Vijay

Best Answer chosen by Admin (Salesforce Developers) 
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Vijay,

 

This is so simple, ORDER BY is nothing but sorting the list of records based on a field value may be its a number/string.

it willorder those records Numerically/Alphabetically in Ascending order by default.

 

You can also specify in which order it should be rendered. For example, ASC or DESC order of records. By, default it will take ASC for Ascending order.

 

So, for your query you are ordering by the number field Sort_Order__c. So, It will hold the list of records in ascending order numerically based on the field value.

 

Hope so this helps you...!

 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Vijay,

 

This is so simple, ORDER BY is nothing but sorting the list of records based on a field value may be its a number/string.

it willorder those records Numerically/Alphabetically in Ascending order by default.

 

You can also specify in which order it should be rendered. For example, ASC or DESC order of records. By, default it will take ASC for Ascending order.

 

So, for your query you are ordering by the number field Sort_Order__c. So, It will hold the list of records in ascending order numerically based on the field value.

 

Hope so this helps you...!

 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

This was selected as the best answer
ericmonteericmonte
You might want to read this:
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_orderby.htm

Order By basically sorts yours SOQL in a specific order. and return it into that way. You can do it ASC - Ascending or DESC (Descending).

A good use for SOQL order is when you have a list and you want to return it in a page and limit it to a specific size. So if you do a soql with order and limit it to 20 the first 20 based on the order will be displayed.

Hope this helps