Development questions

Explain the difference between GROUP BY, GROUP BY ROLLUP, and GROUP BY CUBE in SOQL.

DevelopmentHardDeloittePwCSalesforce
  • GROUP BY groups rows by field values and gives one row per group.
  • GROUP BY ROLLUP(f1, f2) adds subtotal rows for each level plus a grand total. Use GROUPING(field) to detect subtotal rows.
  • GROUP BY CUBE(f1, f2) adds subtotals for all combinations of the fields (more rows than ROLLUP).
soql
SELECT LeadSource, Rating, COUNT(Id) FROM Lead GROUP BY ROLLUP(LeadSource, Rating)
SELECT Type, BillingCountry, COUNT(Id) FROM Account GROUP BY CUBE(Type, BillingCountry)
soqlaggregategroup-byrollupcube