SOQL GROUP BY: Users by ProfileId

Aggregate SOQL that breaks users down by ProfileId, 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 ProfileId, returned as AggregateResult rows.

User queries are the fastest way to audit licences, dormant logins, and profile assignments without opening Setup.

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.

Users per ProfileId
SELECT ProfileId, COUNT(Id) total
FROM User
GROUP BY ProfileId
ORDER BY COUNT(Id) DESC

Add a time window

This quarter only
SELECT ProfileId, COUNT(Id) total
FROM User
WHERE CreatedDate = THIS_QUARTER
GROUP BY ProfileId
ORDER BY COUNT(Id) DESC

Aggregate functions available

  • COUNT(field) / COUNT(Id) - number of rows in the group
  • COUNT_DISTINCT(field) - unique values
  • SUM(field), AVG(field), MIN(field), MAX(field) on numeric and date fields
  • GROUP BY ROLLUP(field) to add a grand-total row

Common mistakes to avoid

  • Use the API name User, 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:30 must stay unquoted.
  • Run the query in a sandbox first if it feeds a bulk update - a WHERE clause 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 AggregateResult objects, 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 User 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

Related reading