Export ZOQL Clauses
Learn about the ZOQL clauses such as order by, group by, having, and limit, which are used to sort, group, filter, and restrict query results.
Export ZOQL supports the order by , group by , and having clauses.
Order By
Use the order by clause to sort results in ascending (ASC) or descending (DESC) in the order by clause.
By default, the ZOQL sorts results in ascending order if you do not specify ASC or DESC in the order by clause.
You can specify multiple fields in the order by clause.
For example, the following two statements will return results in ascending order:
The following example demonstrates how to use multiple fields with order by :
Group By
You can use the group by clause to combine rows that have the same values in particular columns. For example:
In the results of the above query, each row will have a different value of Subscription.Id .
You can specify up to 5 fields in the group by clause. For example:
When you use the group by clause, each field in the select statement must be aggregated or must appear in the group by clause. For example:
In the above example, the Account.Id field is aggregated by the count() function, so Account.Id does not need to appear in the group by clause.
If a field is not aggregated or does not appear in the group by clause, you will receive the following error when you run the query: “One or more columns in the SELECT statement does not appear in the GROUP BY statement.”
Having
Use the having clause to return only rows where aggregate values meet the conditions that you specify.
For example, you can use a having clause as a filter on a group by result.
Limit
Use the limit clause to restrict the results returned by a query.
You can use the limit clause to return the first five rows of a query:
You can use the limit clause to return the 100 rows of a result after the fiftieth row. In this example, the query will return rows 50 through 149.