forcedotcom/sf-skills / plugins/builder/salesforce-development/skills/platform-soql-query
platform-soql-query
VerifiedUse when the user needs SOQL/SOSL authoring or optimization: natural-language-to-query generation, relationship queries, aggregates, query-plan/selectivity analysis, and performance or safety improvements. TRIGGER when the user writes, optimizes, or debugs SOQL/SOSL, touches .soql files, or asks about relationship queries, aggregates, or query performance. DO NOT TRIGGER for bulk data operations (use platform-data-manage), Apex DML logic (use platform-apex-generate), or report/dashboard queries.
| File | Kind | Size |
|---|---|---|
| README.md | markdown | 1.7 KB |
| SKILL.md | skill md | 6.3 KB |
| assets/aggregate-queries.soql | other | 9.4 KB |
| assets/basic-queries.soql | other | 7.4 KB |
| assets/bulkified-query-pattern.cls | other | 10.7 KB |
| assets/optimization-patterns.soql | other | 10.9 KB |
| assets/relationship-queries.soql | other | 7.5 KB |
| assets/selector-class.cls | other | 8.4 KB |
| references/anti-patterns.md | markdown | 8.7 KB |
| references/cli-commands.md | markdown | 7.2 KB |
| references/field-coverage-rules.md | markdown | 14.4 KB |
| references/query-optimization.md | markdown | 3.2 KB |
| references/selector-patterns.md | markdown | 13.7 KB |
| references/soql-reference.md | markdown | 6.4 KB |
| references/soql-syntax-reference.md | markdown | 4.5 KB |
| scripts/post-tool-validate.py | script | 11.9 KB |
references/soql-syntax-reference.md
View on GitHub<!-- Parent: platform-soql-query/SKILL.md -->
# SOQL Syntax Reference
## Basic Query Structure
```sql
SELECT field1, field2, ...
FROM ObjectName
WHERE condition1 AND condition2
ORDER BY field1 ASC/DESC
LIMIT number
OFFSET number
```
### Field Selection
```sql
-- Specific fields (recommended)
SELECT Id, Name, Industry FROM Account
-- All fields (avoid in Apex - use only in Developer Console)
SELECT FIELDS(ALL) FROM Account LIMIT 200
-- Standard fields only
SELECT FIELDS(STANDARD) FROM Account
```
### WHERE Clause Operators
| Operator | Example | Notes |
|----------|---------|-------|
| `=` | `Name = 'Acme'` | Exact match |
| `!=` | `Status != 'Closed'` | Not equal |
| `<`, `>`, `<=`, `>=` | `Amount > 1000` | Comparison |
| `LIKE` | `Name LIKE 'Acme%'` | Wildcard match |
| `IN` | `Status IN ('New', 'Open')` | Multiple values |
| `NOT IN` | `Type NOT IN ('Other')` | Exclude values |
| `INCLUDES` | `Interests__c INCLUDES ('Golf')` | Multi-select picklist |
| `EXCLUDES` | `Interests__c EXCLUDES ('Golf')` | Multi-select exclude |
### Date Literals
| Literal | Meaning |
|---------|---------|
| `TODAY` | Current day |
| `YESTERDAY` | Previous day |
| `THIS_WEEK` | Current week (Sun-Sat) |
| `LAST_WEEK` | Previous week |
| `THIS_MONTH` | Current month |
| `LAST_MONTH` | Previous month |
| `THIS_QUARTER` | Current quarter |
| `THIS_YEAR` | Current year |
| `LAST_N_DAYS:n` | Last n days |
| `NEXT_N_DAYS:n` | Next n days |
```sql
-- Created in last 30 days
SELECT Id FROM Account WHERE CreatedDate = LAST_N_DAYS:30
-- Modified this month
SELECT Id FROM Contact WHERE LastModifiedDate = THIS_MONTH
```
---
## Relationship Queries
### Child-to-Parent (Dot Notation)
```sql
-- Access parent fields
SELECT Id, Name, Account.Name, Account.Industry
FROM Contact
WHERE Account.AnnualRevenue > 1000000
-- Up to 5 levels
SELECT Id, Contact.Account.Owner.Manager.Name
FROM Case
```
### Parent-to-Child (Subquery)
```sql
-- Get parent with related children
SELECT Id, Name,
(SELECT Id, FirstName, LastName FROM Contacts),
(SELECT Id, Name, Amount FROM Opportunities WHERE StageName = 'Closed Won')
FROM Account
WHERE Industry = 'Technology'
```
### Standard Relationship Names
| Object | Relationship Name | Example |
|--------|-------------------|---------|
| Account → Contacts | `Contacts` | `(SELECT Id FROM Contacts)` |
| Account → Opportunities | `Opportunities` | `(SELECT Id FROM Opportunities)` |
| Account → Cases | `Cases` | `(SELECT Id FROM Cases)` |
| Contact → Cases | `Cases` | `(SELECT Id FROM Cases)` |
| Opportunity → OpportunityLineItems | `OpportunityLineItems` | `(SELECT Id FROM OpportunityLineItems)` |
### Custom Object Relationships
```sql
-- Custom relationship: add __r suffix
SELECT Id, Name, Custom_Object__r.Name
FROM Another_Object__c
-- Child relationship: add __r suffix
SELECT Id, (SELECT Id FROM Custom_Children__r)
FROM Parent_Object__c
```
---
## Aggregate Queries
### Basic Aggregates
```sql
-- Count all records
SELECT COUNT() FROM Account
-- Count with alias
SELECT COUNT(Id) cnt FROM Account
-- Sum, Average, Min, Max
SELECT SUM(Amount), AVG(Amount), MIN(Amount), MAX(Amount)
FROM Opportunity
WHERE StageName = 'Closed Won'
```
### GROUP BY
```sql
-- Count by field
SELECT Industry, COUNT(Id)
FROM Account
GROUP BY Industry
-- Multiple groupings
SELECT StageName, CALENDAR_YEAR(CloseDate), COUNT(Id)
FROM Opportunity
GROUP BY StageName, CALENDAR_YEAR(CloseDate)
```
### HAVING Clause
```sql
-- Filter aggregated results
SELECT Industry, COUNT(Id) cnt
FROM Account
GROUP BY Industry
HAVING COUNT(Id) > 10
```
### GROUP BY ROLLUP
```sql
-- Subtotals
SELECT LeadSource, Rating, COUNT(Id)
FROM Lead
GROUP BY ROLLUP(LeadSource, Rating)
```
---
## Advanced Features
### Polymorphic Relationships (What)
```sql
-- Query polymorphic fields
SELECT Id, What.Name, What.Type
FROM Task
WHERE What.Type IN ('Account', 'Opportunity')
-- TYPEOF for conditional fields
SELECT
TYPEOF What
WHEN Account THEN Name, Phone
WHEN Opportunity THEN Name, Amount
END
FROM Task
```
### Semi-Joins and Anti-Joins
```sql
-- Semi-join: Records that HAVE related records
SELECT Id, Name FROM Account
WHERE Id IN (SELECT AccountId FROM Contact)
-- Anti-join: Records that DON'T HAVE related records
SELECT Id, Name FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Opportunity)
```
### Format and Currency
```sql
-- Format currency/date in results
SELECT FORMAT(Amount), FORMAT(CloseDate) FROM Opportunity
-- Convert to user's currency
SELECT Id, convertCurrency(Amount) FROM Opportunity
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |