Skip to main content


WHERE, ORDER BY and LIMIT are three of the most commonly used techniques in SOQL. In this post we'll dive into them one at a time and look at some examples.


When you need to filter records, you can use a WHERE clause to define which records should be returned. You can use the AND, OR and NOT operators to define your filter. If you need to use multiple filters you can use parentheses to control how the filters are applied.

Let's look at some examples:

This query will return all Opportunities with an Amount greater than $50,000.

This query will return all Leads with a Lead Source of Web that were created This Week. The THIS_WEEK keyword is an example of a Date Literal and makes filtering on date and datetime fields much simpler.

This query will return all Accounts in CA, NY or FL based on Billing State. The IN keyword allows you to filter for records that have any of the values you define. Using NOT IN is a common technique and it has the opposite affect - returning records that do not have any of these values.


Sorting your results is especially important when viewing or exporting data. SOQL allows you to sort by using an ORDER BY clause. You have the ability to sort many fields ascending or descending and to specify if null values should be shown at the beginning or end of your result.

Let's look at some examples:

This query will return all Accounts in alphabetical order from A to Z (ascending).

This query will return all Contacts by Account Name ascending, then First Name ascending. Notice that the ASC keyword was left off. This works because ascending is the default sort order.

This query will return all Opportunities by Amount from high to low (descending) with null values at the end.


It's common to query and only need to return a certain number of results. I also find myself adding LIMIT 10 to the end of my queries when I'm testing them out just to see if I'm getting the results I'm expecting.

Let's look at some examples using what we've learned:

This query will return the top 5 Opportunities by Amount.

This query will return the first 10 Accounts in alphabetical order that are in CA, NY or FL based on Billing State.

This query will return the last 10 Leads created. The format(CreatedDate) will localize the datetime field, making it easier to read. It will localize other data types like number, date and currency too! More info is in the docs.

Continued Learning

SOQL can be tough to grasp for those that haven't used it before. I know I was confused by it at first, but it just takes practice. I recommend tweaking the queries above until you understand what they're doing.

If you've never used SOQL before and are unsure of the benefits, I wrote a whole other article on why you should use SOQL. Check it out if you're on the fence.

Here are some helpful resources for getting started with SOQL: 


Popular posts from this blog

Joins in SOQL

If you are familiar with SQL, the biggest gotcha when writing SOQL is how joins are done. You'll see the term  Sub-Query  used instead of  JOIN  because of how queries are done on the Salesforce platform. You can still do joins; it's just done a little differently than you're used to. Sub-Query (Outer Join) In SQL, a join is when rows are selected from multiple tables and joined together on common columns. Thinking about your tables like a Venn Diagram is a great analogy for this. Let's say you want to query all Accounts and also include their related Opportunities. This is an example of an Outer Join because it selects all Accounts and will include their related Opportunities via the sub-query. If an Account does not have an Opportunity, it will still be returned here. An Outer Join is similar to selecting one whole circle in a Venn Diagram, including the area that overlaps with the other circle. Sub-Query (Inner Join) An inner join can seem a little m

Salesforce Lighting Migration: Plan for Success

In April 2018, I led the Salesforce Lightning Migration at Postmates . This is how I would do it over again if I could. Define Goals The goals of a Lightning Migration are: Enable Lightning Experience Configure Lightning Experience Document all Salesforce processes Create training and support materials Lightning Experience is adopted Easy enough right? Let's go though the plan to see how we accomplish each one. Enable Lightning Experience Just turn it on right? Well, kinda. Although it may be controversial, I recommend turning it on and hiding the option from all users. This can be done by removing the Lightning Experience User permission from your custom profiles.  And yes, turn it on in production .  Why? You're going to turn it on anyway. Salesforce has declared this as the path forward, so you either turn it on now or wait until they flip the switch for you. Plus, turning this on ahead of time will make deploying from a sandbox much  e

Modify Salesforce Data from the Command Line

When doing Salesforce data loads, how often are you putting the same value in an entire column? I find this happening when I need to: Move all accounts from one user to another Assign a set of leads to a specific user Set a field to a specific value Let's use the example from my other post: Salesforce Data Loads from the Command Line - Move all accounts owned by one user to another user. Magic Let's break down each piece of this: soql This is an alias I set for sfdx force:data:soql:query --query . It saves me from having to type this out each time and let's me query from the command line by just typing soql  and my query. This is a popular StackExchange post  that talks about how to set an alias. cat This command outputs the contents of a file. It is common to use this command with other commands by piping the output into another command. In our example, we are piping the output into the cut command. This post gives more examples of how to use t