SOQL Query to Count Opportunities in Salesforce
Copy-paste SOQL to count opportunities in Salesforce, with variants for filtered counts, COUNT(Id) vs COUNT(), and grouped totals.
Updated 2026-09-09
You need a number, not a list: how many opportunities exist in this org, and how many of them match a filter. COUNT() answers both without pulling a single record into memory.
Opportunity queries feed pipeline reviews, so they nearly always combine a stage filter with a close-date window.
The query: count all opportunities
COUNT() returns a single row with the total. It is the cheapest way to size an object before you export or mass-update it.
SELECT COUNT() FROM OpportunityCount with a filter
Add a WHERE clause to scope the count. This is the query to run before any data operation, so you know exactly how many rows you are about to touch.
SELECT COUNT() FROM Opportunity
WHERE CreatedDate = LAST_N_DAYS:30Count opportunities grouped by StageName
COUNT(Id) plus GROUP BY gives you a breakdown in one call. Note the alias - aggregate columns are returned as expr0 unless you name them.
SELECT StageName, COUNT(Id) total
FROM Opportunity
GROUP BY StageName
ORDER BY COUNT(Id) DESCCommon mistakes to avoid
- Use the API name
Opportunity, 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. COUNT()takes no field and returns one row;COUNT(Id)is the aggregate form you need whenever you alsoGROUP BY.- Counts respect sharing when run as a non-admin user, so two people can legitimately see different totals.
Run it faster with TurboKit
Counting is usually step one of a bigger job - export, cleanup, or a mass update on those opportunities.
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 the difference between COUNT() and COUNT(Id) on Opportunity?
COUNT()returns a single scalar total and cannot be combined with other selected fields.COUNT(Id)is an aggregate function, so it can sit next toGROUP BYcolumns - use it whenever you want counts broken down by StageName.- Does a COUNT() query on Opportunity hit governor limits?
COUNT()does not return rows, so it does not consume the 50,000-row query limit in Apex the way a fullSELECTwould. It still counts as one SOQL query against the per-transaction query limit.- Why is my opportunity count different from the report total?
- Reports apply their own filters, record-type scoping, and sharing rules. A SOQL count run as an admin sees every record the admin can access, which is frequently a larger set than a filtered report.
More from the SOQL Library
- SOQL GROUP BY: Opportunities by StageNameAggregate SOQL that breaks opportunities down by StageName, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Accounts by IndustryAggregate SOQL that breaks accounts down by Industry, with COUNT, SUM, and the AggregateResult alias rules.
- 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.
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