SOQL GROUP BY: Accounts by Industry
Aggregate SOQL that breaks accounts down by Industry, with COUNT, SUM, and the AggregateResult alias rules.
Updated 2026-09-09
Aggregate SOQL gives you a report-style breakdown without building a report: totals per Industry, returned as AggregateResult rows.
Accounts sit at the top of most data models, so filters here usually cascade into contact, opportunity, and case reporting.
The query
total is an alias. Without it, the column comes back as expr0, which is a common source of confusion when you consume the result in Apex.
SELECT Industry, COUNT(Id) total
FROM Account
GROUP BY Industry
ORDER BY COUNT(Id) DESCAdd a time window
SELECT Industry, COUNT(Id) total
FROM Account
WHERE CreatedDate = THIS_QUARTER
GROUP BY Industry
ORDER BY COUNT(Id) DESCAggregate functions available
COUNT(field)/COUNT(Id)- number of rows in the groupCOUNT_DISTINCT(field)- unique valuesSUM(field),AVG(field),MIN(field),MAX(field)on numeric and date fieldsGROUP BY ROLLUP(field)to add a grand-total row
Common mistakes to avoid
- Use the API name
Account, not the UI label - the two differ on several standard objects. - Wrap literal text values in single quotes; date literals such as
LAST_N_DAYS:30must stay unquoted. - Run the query in a sandbox first if it feeds a bulk update - a
WHEREclause that is one character off can select the whole table. - Every non-aggregate field in the SELECT list must appear in
GROUP BY. - Aggregate queries return
AggregateResultobjects, not sObjects - you cannot dot-walk into them the same way.
Run it faster with TurboKit
Aggregates are the fastest way to sanity-check a data model before you trust a dashboard built on it.
TurboKit's AI SOQL builder lets you describe the result you want in plain English, returns the query, and exports the rows to CSV or Excel in the same panel - without leaving the Salesforce tab you are already on.
Frequently asked questions
- What is AggregateResult?
- It is the generic result type returned by any grouped or aggregate SOQL query. Fields are read by alias, and unaliased aggregate columns are named
expr0,expr1, and so on. - Is there a row limit on GROUP BY queries?
- Yes - grouped queries return at most 2,000 groups by default in Apex contexts. Filter more tightly, or use a report or bulk export if you need every group.
- Can I group Account by a formula field?
- Only if the formula is deterministic and groupable. Text formulas usually work; formulas referencing other objects often do not, and the query returns an error rather than a partial result.
More from the SOQL Library
- SOQL Query to Count Accounts in SalesforceCopy-paste SOQL to count accounts in Salesforce, with variants for filtered counts, COUNT(Id) vs COUNT(), and grouped totals.
- SOQL GROUP BY: Campaigns by TypeAggregate SOQL that breaks campaigns down by Type, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Cases by StatusAggregate SOQL that breaks cases down by Status, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Contacts by LeadSourceAggregate SOQL that breaks contacts down by LeadSource, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Leads by LeadSourceAggregate SOQL that breaks leads down by LeadSource, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Opportunities by StageNameAggregate SOQL that breaks opportunities down by StageName, with COUNT, SUM, and the AggregateResult alias rules.
Related reading
- Admin How-ToHow to Write Salesforce Queries Without Knowing SOQL (AI SOQL)Describe the records you want in plain English and get a runnable SOQL query back - how AI SOQL builders work, what to check before you run the result, and where they still fall short.
- GlossaryWhat Is an AI SOQL Builder in Salesforce?An AI SOQL builder turns a plain-English description of the records you want into a runnable SOQL query, using the schema of the org you are working in so the