Aggregates
SOQL MIN Function
Using MIN Function
SOQL MIN function finds the smallest field value.
Introduction to SOQL MIN Function
The SOQL MIN function is an aggregate function that helps you find the smallest value of a specified field across multiple records in Salesforce. It's particularly useful when you need to determine the minimum value in a dataset, such as the smallest opportunity amount or the earliest date in a list of records.
Syntax of the MIN Function
The basic syntax of the SOQL MIN function is as follows:
SELECT MIN(fieldName) FROM ObjectName [WHERE condition]
Here, fieldName
is the field for which you want to find the minimum value, and ObjectName
is the name of the object you're querying. You can also include a WHERE
clause to filter the records before calculating the minimum.
Using MIN Function with Examples
Let's explore some practical examples to understand how the MIN function works in SOQL queries.
In this example, the query retrieves the smallest amount from the Opportunity records. This can help sales managers quickly identify the lowest opportunity amount in their sales pipeline.
Here, the query finds the earliest CreatedDate of all cases that have been closed. This is useful for support teams to track how long their case resolution processes have been operating.
Combining MIN with Other Aggregate Functions
The MIN function can be combined with other aggregate functions like MAX, AVG, and COUNT to generate more comprehensive reports. For example:
This query provides a quick overview of the minimum, maximum, and average amounts of opportunities, offering a complete picture of the opportunities' financial metrics.
Best Practices for Using MIN Function
- Ensure the field used with MIN is of a type that supports numeric or date comparison.
- Use appropriate
WHERE
clauses to filter unnecessary records, improving query performance. - Consider the use of indexes on fields involved in
WHERE
clauses to speed up query execution.
Aggregates
- Previous
- AVG Function
- Next
- MAX Function