
Querying Soql
Write correct Salesforce SOQL aggregate, GROUP BY, and filter queries when integrating CRM data into a solo-built app or agent workflow.
Overview
querying-soql is an agent skill for the Build phase that supplies SOQL aggregate, GROUP BY, and filter patterns for Salesforce CRM integrations.
Install
npx skills add https://github.com/forcedotcom/sf-skills --skill querying-soqlWhat is this skill?
- COUNT(), COUNT(field), COUNT_DISTINCT() patterns with null-safe filters
- SUM, AVG, MIN, MAX aggregates on Opportunity-style amount fields
- GROUP BY examples for rollups by stage, industry, or calendar year
- Combined multi-aggregate SELECT blocks for closed-won style reporting
- Documents COUNT, COUNT(field), COUNT_DISTINCT, SUM, AVG, MIN, and MAX aggregate functions
- Includes grouped aggregate examples with CALENDAR_YEAR on CloseDate
Adoption & trust: 749 installs on skills.sh; 513 GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
You need rollup and grouped CRM metrics from Salesforce but keep writing invalid or non-aggregate SOQL for dashboards and sync jobs.
Who is it for?
Indie devs and agent workflows that read Salesforce via API, CLI, or embedded queries in Apex-light integrations.
Skip if: Builders not on Salesforce who only need generic SQL against Postgres or warehouses.
When should I use this skill?
Drafting or reviewing Salesforce SOQL that uses aggregates, filters, or GROUP BY for integrations or reports.
What do I get? / Deliverables
You get ready-to-adapt SOQL blocks for counts, sums, averages, and grouped breakdowns aligned with standard Opportunity and Account patterns.
- SOQL query strings for counts and financial aggregates
- GROUP BY query templates for dimensional breakdowns
- Filtered aggregate queries ready for CLI or REST query endpoints
Recommended Skills
Journey fit
SOQL querying is done while wiring Salesforce as a system of record during product build, not during idea research. Queries are the core integration surface between your app and Salesforce objects, reports, and automations.
How it compares
Reference skill for SOQL syntax and aggregates, not a Salesforce MCP server or full metadata deployment toolkit.
Common Questions / FAQ
Who is querying-soql for?
Solo builders and small teams integrating with Salesforce orgs who want vetted aggregate and GROUP BY SOQL without re-reading Salesforce docs each time.
When should I use querying-soql?
In the Build integrations subphase while designing reports, sync queries, or agent-generated SOQL for Accounts, Contacts, and Opportunities.
Is querying-soql safe to install?
Queries themselves do not execute from the skill file; confirm repo trust via the Security Audits panel on this Prism page before install.
SKILL.md
READMESKILL.md - Querying Soql
/** * AGGREGATE QUERY PATTERNS * * SOQL aggregate functions: * - COUNT(), COUNT(field), COUNT_DISTINCT(field) * - SUM(field), AVG(field), MIN(field), MAX(field) * * Use with GROUP BY for grouping results. */ // ═══════════════════════════════════════════════════════════════════════════ // BASIC COUNT // ═══════════════════════════════════════════════════════════════════════════ -- Count all records SELECT COUNT() FROM Account -- Count with filter SELECT COUNT() FROM Contact WHERE Email != null -- Count with field alias SELECT COUNT(Id) total FROM Account -- Count distinct values SELECT COUNT_DISTINCT(Industry) FROM Account // ═══════════════════════════════════════════════════════════════════════════ // SUM, AVG, MIN, MAX // ═══════════════════════════════════════════════════════════════════════════ -- Sum of amounts SELECT SUM(Amount) totalAmount FROM Opportunity WHERE IsClosed = true -- Average amount SELECT AVG(Amount) avgAmount FROM Opportunity WHERE StageName = 'Closed Won' -- Min and Max SELECT MIN(Amount) smallest, MAX(Amount) largest FROM Opportunity WHERE IsClosed = true -- Combined aggregates SELECT COUNT(Id) totalOpps, SUM(Amount) totalAmount, AVG(Amount) avgAmount, MIN(Amount) minAmount, MAX(Amount) maxAmount FROM Opportunity WHERE StageName = 'Closed Won' AND CALENDAR_YEAR(CloseDate) = 2024 // ═══════════════════════════════════════════════════════════════════════════ // GROUP BY // ═══════════════════════════════════════════════════════════════════════════ -- Count by single field SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry -- Count by multiple fields SELECT Industry, Type, COUNT(Id) FROM Account GROUP BY Industry, Type -- Sum by category SELECT StageName, SUM(Amount) FROM Opportunity GROUP BY StageName -- Average by owner SELECT OwnerId, AVG(Amount) FROM Opportunity GROUP BY OwnerId -- Aggregate by date part SELECT CALENDAR_MONTH(CloseDate), SUM(Amount) FROM Opportunity WHERE CALENDAR_YEAR(CloseDate) = 2024 GROUP BY CALENDAR_MONTH(CloseDate) ORDER BY CALENDAR_MONTH(CloseDate) -- Year and Month grouping SELECT CALENDAR_YEAR(CloseDate) yr, CALENDAR_MONTH(CloseDate) mn, SUM(Amount) FROM Opportunity GROUP BY CALENDAR_YEAR(CloseDate), CALENDAR_MONTH(CloseDate) ORDER BY CALENDAR_YEAR(CloseDate), CALENDAR_MONTH(CloseDate) // ═══════════════════════════════════════════════════════════════════════════ // HAVING CLAUSE // ═══════════════════════════════════════════════════════════════════════════ -- Filter groups by aggregate result SELECT Industry, COUNT(Id) cnt FROM Account GROUP BY Industry HAVING COUNT(Id) > 10 -- Multiple HAVING conditions SELECT OwnerId, SUM(Amount) total, COUNT(Id) cnt FROM Opportunity WHERE StageName = 'Closed Won' GROUP BY OwnerId HAVING SUM(Amount) > 100000 AND COUNT(Id) >= 5 -- HAVING with different aggregate than SELECT SELECT LeadSource, COUNT(Id) FROM Lead GROUP BY LeadSource HAVING AVG(NumberOfEmployees) > 100 // ═══════════════════════════════════════════════════════════════════════════ // GROUP BY ROLLUP (Subtotals) // ═══════════════════════════════════════════════════════════════════════════ -- Single rollup SELECT Industry, COUNT(Id) FROM Account GROUP BY ROLLUP(Industry) -- Returns: individual industries + grand total (null Industry row) -- Multiple field rollup SELECT Industry, Type, COUNT(Id) FROM Account GROUP BY ROLLUP(Industry, Type) -- Returns: Industry+Type combos, Industry subtotals, grand total -- With SUM SELECT StageName, CALENDAR_MONTH(CloseDate), SUM(Amount) FROM Opportunity WHERE CALENDAR_YEAR(CloseDate) = 2024 GROUP BY ROLLUP(StageName, CALENDAR_MONTH(CloseDate)) // ═══════════════════════════════════════════════════════════════════════════ // GROUP BY CUBE (All Combinations) // ═══════════════════════════════════════════════════════════════════════════ -- All dimension combinations SELECT Industry, Type, COUNT(Id) FROM Account GROUP BY CUBE(Industry, Type) -- Returns: Industry+Type, Industry only, Typ