# 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**

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

{% endstep %}

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

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

{% endstep %}

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

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

{% endstep %}

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

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

{% endstep %}

{% step %}
**Ordering Results**

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

{% endstep %}

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

Returns Opportunities with all associated OpportunityLineItems:

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

{% endstep %}

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

Fetch Contact info and their parent Account name:

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

{% endstep %}

{% step %}
**Aggregate Query**

Total number of Cases per Status:

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

{% endstep %}

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

Only return statuses with more than 50 cases:

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

{% endstep %}

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

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

{% endstep %}
{% endstepper %}


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
