Order By Schema

AVAILABLE API VERSION
API v59.0 and later

All sObjects in the schema have a corresponding RecordOrderBy type. The RecordOrderBy type combines the object API name with the _OrderBy suffix. For example:

RecordQueryAggregate for Account
1type Account {
2  groupBy: Account_GroupBy
3  orderBy: Account_OrderBy
4}

The RecordOrderBy type is introduced by the RecordQueryAggregate type.

RecordOrderBy Type 

RecordOrderBy creates one input type per sObject type. For example, Account has a corresponding Account_OrderBy type that you can use with the orderBy argument.

The Account_OrderBy type contains fields of OrderByClause type. Some fields are shown here for brevity. We recommend that you use the Altair GraphQL client for a complete view of the schema.

RecordOrderBy Type
1input Account_OrderBy {
2  AccountNumber: AggregateOrderByStringClause
3  AnnualRevenue AggregateOrderByNumberClause
4  BillingCity: AggregateOrderByStringClause
5  BillingCountry: AggregateOrderByStringClause
6  CreatedBy: User_OrderBy
7  CreatedById: NoFunctionAggregateOrderByClause
8  Id: AggregateOrderByStringClause
9  Industry: AggregateOrderByStringClause
10  Name: AggregateOrderByStringClause
11  NumberOfEmployees: AggregateOrderByNumberClause
12  OperatingHours: OperatingHours_OrderBy
13  Owner: User_OrderBy
14  OwnerId: NoFunctionAggregateOrderByClause
15  Ownership: AggregateOrderByStringClause
16  Parent: Account_OrderBy
17  ParentId: NoFunctionAggregateOrderByClause
18}

The main types include:

  • AggregateOrderByStringClause - Defines aggregate functions used by fields of these types:
    • String
    • Id
    • DateTime
    • Time
    • Date
    • Email
    • PhoneNumber
    • Url
    • Picklist
  • AggregateOrderByNumberClause - Defines aggregate functions used by fields of these types:
    • Int
    • Double
    • Long
    • Latitude
    • Longitude
    • Percent
  • NoFunctionAggregateOrderByClause - Defines how null values are returned and how sorting order are used by fields of these types:
    • Boolean
    • Reference fields like AccountId and OwnerId

AggregateOrderByStringClause Type 

The AggregateOrderByStringClause type has several enumeration types.

AggregateOrderByStringClause Type
1type AggregateOrderByStringClause {
2  function: AggregateOrderByStringFunction
3  nulls: NullsOrder
4  order: ResultsOrder
5}

Pass these types to the orderBy argument in the aggregate field.

  • function - The aggregate function to apply to the filter.
  • nulls - Orders null records either first or last.
  • order - Order the aggregate results by ascending or descending order.

AggregateOrderByStringFunction Enum 

The AggregateOrderByStringFunction enum values define aggregate functions for string fields like date, email, or Url and so on.

AggregateOrderByStringFunction Enum
1enum AggregateOrderByStringFunction {
2  COUNT
3  COUNT_DISTINCT
4  MAX
5  MIN
6}

Use these values with string fields.

  • COUNT - Returns the number of rows matching the query criteria.
  • COUNT_DISTINCT - Returns the number of distinct non-null field values matching the query criteria.
  • MAX - Returns the maximum value of a field.
  • MIN - Returns the minimum value of a field.

AggregateOrderByNumberClause Type 

The AggregateOrderByNumberClause type has several enumeration types.

AggregateOrderByNumberClause Type
1type AggregateOrderByNumberClause {
2  function: AggregateOrderByNumberFunction
3  nulls: NullsOrder
4  order: ResultsOrder
5}

Pass these types to the orderBy argument in the aggregate field.

  • function - The aggregate function to apply to the filter.
  • nulls - Orders null records either first or last.
  • order - Order the aggregate results by ascending or descending order.

NoFunctionAggregateOrderByClause Type 

The NoFunctionAggregateOrderByClause type has several enumeration types.

NoFunctionAggregateOrderByClause Type
1type NoFunctionAggregateOrderByClause {
2  nulls: NullsOrder
3  order: ResultsOrder
4}

Pass these types to the orderBy argument in the aggregate field.

  • nulls - Orders null records either first or last.
  • order - Order the aggregate results by ascending or descending order.

AggregateOrderByNumberFunction Enum 

The AggregateOrderByNumberFunction enum values define aggregate functions for number fields like double, long, percent and so on.

AggregateOrderByNumberFunction Enum
1enum AggregateOrderByNumberFunction {
2  AVG
3  COUNT
4  COUNT_DISTINCT
5  MAX
6  MIN
7  SUM
8}

Use these values with numeric fields.

  • AVG - Returns the average value of a field.
  • COUNT - Returns the number of rows matching the query criteria.
  • COUNT_DISTINCT - Returns the number of distinct non-null field values matching the query criteria.
  • MAX - Returns the maximum value of a field.
  • MIN - Returns the minimum value of a field.
  • SUM - Returns the total sum of a numeric field.

NullsOrder Enum 

The NullsOrder enumeration type specifies the order in which nulls appear in the result set. By default, null values are sorted first.

NullsOrder enumeration type
1enum NullsOrder {
2  FIRST
3  LAST
4}

Use these values in the orderBy argument.

  • FIRST - Order the null values first. This is the default.
  • LAST - Order the null values last.

ResultsOrder Enum 

The ResultsOrder enumeration type specifies whether the results are ordered in ascending or descending order. By default, the result is ordered in ascending order.

ResultsOrder enumeration type
1enum ResultsOrder {
2  ASC
3  DESC
4}

Use these values in the orderBy argument.

  • ASC - Ascending order (default)
  • DESC - Descending order

User_OrderBy Type 

Some fields like CreatedBy and Owner correspond to a _OrderBy type, which contains additional fields.

User_GroupBy Type
1type User_GroupBy {
2  AboutMe: AggregateOrderByStringClause
3  Account: Account_OrderBy
4  AccountId: NoFunctionAggregateOrderByClause
5  Contact: Contact_OrderBy
6  NumberOfFailedLogins: AggregateOrderByNumberClause
7  # Other User_OrderBy fields
8}

See Also 

Aggregate Examples

SOQL and SOSL Reference: Date Functions