# SOQL Formatter and Query Checker

> Formats SOQL queries into a readable layout (keywords uppercased, one clause per line, subqueries indented) without changing their meaning, and flags patterns that may be non-selective. The checker uses heuristics: real selectivity depends on your org's data and indexes. Checked on 2026-11-17.

Source: [SOQL and SOSL Reference: SOQL](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql.htm)
Interactive formatter: https://sfdxtalk.com/tools/soql-formatter/

## Checker rules

### LIKE with a leading wildcard, such as LIKE '%abc'

Severity: warning. Flags: A LIKE filter whose pattern starts with %.

An index is ordered by the start of a value, so a pattern that starts with a wildcard generally cannot use it. The filter may be non-selective and the query may scan many records. If you need to find text anywhere in a field, SOSL is built for that.

### Negative operator in WHERE: !=, <>, NOT IN, NOT LIKE or EXCLUDES

Severity: warning. Flags: A negative filter operator in the WHERE clause. Comparing with null (!= null) is not flagged.

Negative filters match everything except a few values, so they generally cannot narrow the data through an index and may be non-selective. Prefer a positive filter, for example IN with the values you want, when you can.

### No WHERE clause

Severity: warning. Flags: The main query has no WHERE clause.

Without a filter the query reads every record of the object (limited by LIMIT, if present). On a large object it may be slow, and in Apex it can reach the limit on records retrieved.

### No LIMIT

Severity: info. Flags: The main query has no LIMIT and is not an aggregate query.

If you only need a bounded number of records, say so with LIMIT. Without it, a query can return every matching record, and Apex stops a transaction that retrieves too many.

### ORDER BY without LIMIT

Severity: info. Flags: The main query sorts but has no LIMIT.

Sorting a large result costs more than returning it unsorted. If you only need the top or bottom records, add LIMIT so the sort is worth it.

### SELECT of more than 50 fields

Severity: info. Flags: More than 50 top-level items in the SELECT list. The threshold is our own choice, not a Salesforce limit.

Every selected field is held in memory for every returned record, so wide queries use more heap. Select only the fields the code reads.

The 50-field threshold is our own choice, not a Salesforce limit.

## Formatting examples

### Parent-child subquery and a semi-join

A child subquery in SELECT and a semi-join subquery in WHERE each get their own indented block.

Before:

```soql
select Id,Name,(select Id,LastName from Contacts where Email!=null order by LastName limit 5) from Account where Industry='Technology' and Id in (select AccountId from Opportunity where StageName='Closed Won') order by Name limit 100
```

After:

```soql
SELECT
  Id,
  Name,
  (
    SELECT
      Id,
      LastName
    FROM Contacts
    WHERE Email != NULL
    ORDER BY LastName
    LIMIT 5
  )
FROM Account
WHERE Industry = 'Technology'
  AND Id IN (
    SELECT
      AccountId
    FROM Opportunity
    WHERE StageName = 'Closed Won'
  )
ORDER BY Name
LIMIT 100
```

### Bind variables and date literals

Bind variables, LAST_N_DAYS:n literals and string escapes are kept exactly as written.

Before:

```soql
SELECT Id, Subject FROM Task WHERE OwnerId = :ownerId AND CreatedDate = LAST_N_DAYS:30 AND (Subject LIKE '%O\'Brien%' OR Status IN :statuses) AND ActivityDate >= 2026-01-01
```

After:

```soql
SELECT
  Id,
  Subject
FROM Task
WHERE OwnerId = :ownerId
  AND CreatedDate = LAST_N_DAYS:30
  AND (Subject LIKE '%O\'Brien%' OR Status IN :statuses)
  AND ActivityDate >= 2026-01-01
```

### TYPEOF and a query tail

TYPEOF branches are indented, and SECURITY_ENFORCED, GROUP BY, ORDER BY, LIMIT, OFFSET and FOR VIEW stay on their own lines.

Before:

```soql
SELECT TYPEOF What WHEN Account THEN Phone, NumberOfEmployees WHEN Opportunity THEN Amount, CloseDate ELSE Name END, Subject FROM Event WHERE IsAllDayEvent = true WITH SECURITY_ENFORCED ORDER BY Subject DESC NULLS LAST LIMIT 20 OFFSET 40 FOR VIEW
```

After:

```soql
SELECT
  TYPEOF What
    WHEN Account THEN Phone, NumberOfEmployees
    WHEN Opportunity THEN Amount, CloseDate
    ELSE Name
  END,
  Subject
FROM Event
WHERE IsAllDayEvent = TRUE
WITH SECURITY_ENFORCED
ORDER BY Subject DESC NULLS LAST
LIMIT 20
OFFSET 40
FOR VIEW
```

### Aggregate query

GROUP BY and HAVING with aggregate functions.

Before:

```soql
select StageName, COUNT(Id) total, SUM(Amount) revenue from Opportunity where CloseDate = THIS_YEAR group by StageName having COUNT(Id) > 5 order by SUM(Amount) desc
```

After:

```soql
SELECT
  StageName,
  COUNT(Id) total,
  SUM(Amount) revenue
FROM Opportunity
WHERE CloseDate = THIS_YEAR
GROUP BY StageName
HAVING COUNT(Id) > 5
ORDER BY SUM(Amount) DESC
```
