Skip to content
Back to skills

2525 Soql Relationship Guide A134d99f

ASecurity

Complete reference for querying related records in Salesforce.

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
businesssql

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 2525-soql-relationship-guide_a134d99f --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 2525 Soql Relationship Guide A134d99f?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 2525 Soql Relationship Guide A134d99f
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-2525-soql-relationship-guide-a134d99f/badge)](https://www.skillsdirectory.com/skills/tools-only-2525-soql-relationship-guide-a134d99f)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
<!-- Parent: sf-data/SKILL.md -->
# SOQL Relationship Query Guide

Complete reference for querying related records in Salesforce.

## Relationship Types

### 1. Parent-to-Child (Subquery)
Returns parent records with nested child records.

```sql
SELECT Id, Name,
    (SELECT Id, FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
```

**Key Points:**
- Maximum 20 subqueries per query
- Use relationship name (plural): `Contacts`, `Opportunities`, `Cases`
- For custom objects: `Child_Object__r`

### 2. Child-to-Parent (Dot Notation)
Access parent fields from child record.

```sql
SELECT Id, Name,
    Account.Name,
    Account.Industry,
    Account.Owner.Name
FROM Contact
WHERE Account.Type = 'Customer'
```

**Key Points:**
- Maximum 5 levels deep
- Standard: `Account.Name`
- Custom: `Parent__r.Name`

### 3. Polymorphic (TYPEOF)
Handle fields that reference multiple object types.

```sql
SELECT Id, Subject,
    TYPEOF Who
        WHEN Contact THEN FirstName, LastName
        WHEN Lead THEN Company
    END,
    TYPEOF What
        WHEN Account THEN Name, Industry
        WHEN Opportunity THEN Amount
    END
FROM Task
```

**Common Polymorphic Fields:**
- `WhoId` → Contact, Lead
- `WhatId` → Account, Opportunity, Case, etc.
- `OwnerId` → User, Queue

## Relationship Names

| Child Object | Parent Field | Relationship Name |
|--------------|--------------|-------------------|
| Contact | AccountId | Account.Contacts |
| Opportunity | AccountId | Account.Opportunities |
| Case | AccountId | Account.Cases |
| Task | WhatId | Account.Tasks |
| Contact | ReportsToId | Contact.ReportsTo |

## Limits

| Limit | Value |
|-------|-------|
| Child-to-Parent depth | 5 levels |
| Subqueries per query | 20 |
| Rows per subquery | 200 (without LIMIT) |

## Best Practices

1. **Use indexed fields** in WHERE clauses
2. **Add LIMIT** to subqueries
3. **Filter early** - push conditions into subqueries
4. **Avoid N+1** - use relationship queries instead of loops

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.