Experience Salesforce
SOQL Aggregate Functions
What You’ll Learn
- SOQL Aggregate Functions
Topics
- SOQL Basics (Salesforce Object Query Language)
- How to Write SOQL in APEX
- SOQL Variable Binding in APEX
- SOQL Keywords
- Date Literals in SOQL
- SOQL Aggregate Functions
- Child to Parent Relationship
- Parent to Child – Relationship Queries in SOQL
- SOQL Multi level Relationships
- SOQL Return Type
- Salesforce Dynamic SOQL
- SOQL ’for’ Loops
SOQL Aggregate Functions
“SOQL Aggregate Functions” shows a calculated result from the query and returns AggregateResult.
Any query that includes an aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and is only used for query results.
1. Sum():
AggregateResult ar = [SELECT SUM(Max_Salary__c)
FROM Position__c WHERE Max_Salary__c > 10000
];
2. Max():
Max(): AggregateResult ar = [SELECT MAX(Max_Salary__c)
FROM Position__c
WHERE Max_Salary__c > 10000
];
3. Min():
Min(): AggregateResult ar = [SELECT MIN(Max_Salary__c)
FROM Position__c
WHERE Max_Salary__c
];
4. Count() and Count(fieldName):
Integer i = [SELECT count(ID) FROM Account];
5. Avg():
Avg(): AggregateResult ar = [SELECT AVG(Min_Salary__c)
FROM Position__c
WHERE Max_Salary__c < 5000
];
6. Count_Distinct():
Count_Distinct(): AggregateResult ar = [SELECT COUNT_DISTINCT(Name)
FROM Position__c
];
Next Topic
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.