# Write a SOQL query to get the sum, average, minimum, and maximum Opportunity Amount grouped by StageName.

**Topic:** Development · **Difficulty:** Easy · **Asked at:** Infosys, Capgemini, PwC · **Tags:** soql, scenario, aggregate, group-by

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-to-get-the-sum-average-minimum-and-maximum-opportunity-amount-grouped-by-stagename

## Answer

Group Opportunities by StageName and apply all four aggregate functions.

```soql
SELECT StageName, SUM(Amount) total, AVG(Amount) average, MIN(Amount) smallest, MAX(Amount) largest
FROM Opportunity
GROUP BY StageName
```
Each row is an `AggregateResult`; read values with `ar.get('total')` and so on.

---
Source: ForceChallenges · last updated 2026-10-19
