function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mattias NordinMattias Nordin 

SQL: Return to 10 rows

Hi,

 

Im trying to do a list of the top 10 contact rows. Is it possible to select TOP10 or something similar?

Is there a referece document of the SQL syntax or is it standard SQL?

 

 

This is the class im experimenting with.

 

public with sharing class RepeatCon {

    List<Contact> pos = [SELECT TOP10 Id, Name FROM Contact];

    public List<Contact> getPos() {
        return pos;
    }

}

 

Note that i dont know much about APAX yet.

BulentBulent

what is the definition of top 10?

based on creation date or some other criteria?

you can limit query results to 10 and order by based on a field and additional criteria based on the where condition 

SJTECH2009SJTECH2009
[select Id,NAme from Contact Limit 10]
sfdcfoxsfdcfox

SOQL is the language that Salesforce uses. It's the Salesforce Object Query Language, found in the Force.com Web Services API documentation.

 

For a quick comparison overview, here goes:

 

SELECT [fieldList] from [Object] WHERE [Conditions] [ORDER BY Field] [LIMIT X]

 

* Field list must not be *, instead you must specify which fields you wish to return.

* You can use functions like MONTH( dateField ) in conditions, and COUNT() is a special fieldList that returns the number of items that match the conditions; do not use COUNT( * ).

* You can use sub-queries in the fieldList, SQL style (in parentheses), the Object must be a relationship name that Salesforce understands, ex: SELECT Id,Name,(SELECT Id,FirstName,LastName FROM Contacts) FROM Account. Custom relationships have __r at the end.

* Date fields in the condition area can use "date literals", such as TODAY. No parentheses, because they are literals, not functions (see documentation).

* The IN keyword works, but BETWEEN does not.

* No such thing as UNION, * JOIN, etc. Use sub-queries instead.

* Use dot notation to obtain parent information. You can go up five levels. Ex: SELECT Id,Account.Name FROM Contact.

* You can not use sub-queries inside sub-queries (you can go "down" one level).

* There is a special syntax for anti-joins, too.

 

You'll find all this, and more, in the documentation (link above). 

 

And, as always, feel free to contact us here at the community if you have problems using SOQL; that is what we are here for. Also, you can try searching on your question. Odds are, if you have a question, it has most likely already been answered before.