> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/developer-hub/template-configuration-and-styling/related-list-references/introduction-to-soql.md).

# Introduction to SOQL

## Basic Structure

```
SELECT field1, field2, field3
FROM ObjectName
WHERE conditions
ORDER BY field ASC|DESC
LIMIT number
```

### Important SOQL Features

* **SELECT fields**
* **WHERE filters** (text, number, date, boolean, IN, LIKE, etc.)
* **ORDER BY** (ASC/DESC, NULLS FIRST/LAST)
* **LIMIT / OFFSET**
* **Aggregate functions** (`COUNT()`, `SUM()`, `AVG()`, `MIN()`, `MAX()`)
* **GROUP BY / HAVING**
* **Parent-to-child subqueries**
* **Child-to-parent traversals**

{% hint style="info" %}
Looking for something more advanced? Explore our [running list of SOQL queries for S-Docs and S-Sign](https://sdoc.atlassian.net/wiki/spaces/SF/pages/1696661620/Useful+SOQL+Queries)!
{% endhint %}

## SOQL Examples (10 Practical Examples)

{% stepper %}
{% step %}
**Basic Query**

Return ID, name, and industry of all accounts:

```
SELECT Id, Name, Industry
FROM Account
```

{% endstep %}

{% step %}
**Filtering with WHERE**

Return name and annual revenue of accounts with greater than 1,000,000 annual revenue:

```
SELECT Name, AnnualRevenue
FROM Account
WHERE AnnualRevenue > 1000000
```

{% endstep %}

{% step %}
**Filtering Using LIKE (Wildcards)**

Fetch contact names whose email ends with '@gmail.com':

```
SELECT Name
FROM Contact
WHERE Email LIKE '%@gmail.com'
```

{% endstep %}

{% step %}
**Using IN to Match Multiple Values**

Return ID and status for cases that has 'New', 'Working', or 'Escalated' status:

```
SELECT Id, Status
FROM Case
WHERE Status IN ('New', 'Working', 'Escalated')
```

{% endstep %}

{% step %}
**Ordering Results**

Return name, close date, and amount of all opportunities, ordered by newest created first:

```
SELECT Name, CloseDate, Amount
FROM Opportunity
ORDER BY CloseDate DESC
```

{% endstep %}

{% step %}
**Parent-to-Child (Subquery)**

Returns ID, Name, and all associated OpportunityLineItems for the opportunity with ID = '006XXXXXXXXXXXX':

```
SELECT Id, Name,
    (SELECT Quantity, TotalPrice FROM OpportunityLineItems)
FROM Opportunity
WHERE Id = '006XXXXXXXXXXXX'
```

{% endstep %}

{% step %}
**Child-to-Parent (Dot Notation)**

Fetch all contact's ID, name, and their parent account's name and industry:

```
SELECT Id, Name, Account.Name, Account.Industry
FROM Contact
```

{% endstep %}

{% step %}
**Aggregate Query**

Return status and the count of cases for each status:

```
SELECT Status, COUNT(Id)
FROM Case
GROUP BY Status
```

{% endstep %}

{% step %}
**Aggregate with HAVING**

Only return status and count of cases for each status that has more than 50 cases:

```
SELECT Status, COUNT(Id)
FROM Case
GROUP BY Status
HAVING COUNT(Id) > 50
```

{% endstep %}

{% step %}
**LIMIT and OFFSET**

Skip the first 20 leads and return the next 20 leads, sorted by newest first:

```
SELECT Name
FROM Lead
ORDER BY CreatedDate DESC
LIMIT 20
OFFSET 20
```

{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.sdocs.com/developer-hub/template-configuration-and-styling/related-list-references/introduction-to-soql.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
