SOQL formatter and query checker

Winter '27 API 68.0 Checked against the official docs on Source: SOQL and SOSL Reference: SOQL

Quick answer

Paste a SOQL query to get a readable, consistently formatted version that means exactly the same thing, plus hints about patterns that may be non-selective, such as a leading wildcard, a negative operator or a missing WHERE or LIMIT. The hints are heuristics: only your org's data and indexes decide real selectivity.

Format and check a SOQL query

The formatter needs JavaScript. The explanation, rules and worked examples below work without it.

Formatted query

The formatted query appears here.

Checker hints

Paste a SOQL query to format and check it.

    How SOQL formatting conventions work

    SOQL ignores line breaks and extra spaces, so formatting is purely for people. The most common convention, and the one this tool applies, is simple: write keywords in uppercase, start each clause (SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET) on its own line, and indent anything that belongs to a clause.

    • Fields: one per line makes diffs in version control show exactly which field was added. The compact layout keeps them on one line.
    • Conditions: each top-level AND or OR starts a new line, so a long filter reads like a list. Parenthesised groups stay together.
    • Subqueries: a parent-child subquery in SELECT or a semi-join in WHERE is indented as its own query inside parentheses. TYPEOF branches are indented under TYPEOF.
    • Things that must not change: string literals (including escaped quotes), bind variables such as :ids, date and datetime literals, and literals such as LAST_N_DAYS:5 are copied exactly. Field and object names keep the case you typed.

    The formatter is token based rather than a find-and-replace on keywords, so a word inside a string, or an object named Order, is never mistaken for a keyword. It also never reorders, adds or removes tokens. If you paste something that is not a valid query, it still prints the tokens it found, so check the result before you run it.

    What the query checker looks for

    The checker reads only the text of the query. It flags patterns where the query alone is enough to say that the query may be slow or non-selective. It cannot see how many records match or which fields are indexed in your org, so every finding is a heuristic to investigate, not a verdict. Most of the cost of a query shows up against the limits in the governor limits reference.

    • LIKE with a leading wildcard, such as LIKE '%abc' 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. SOQL run time limit

    • Negative operator in WHERE: !=, <>, NOT IN, NOT LIKE or EXCLUDES 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. SOQL run time limit

    • No WHERE clause 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. Records retrieved limit

    • No LIMIT 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. Records retrieved limit

    • ORDER BY without LIMIT 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. Records retrieved limit

    • SELECT of more than 50 fields 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. Heap size limit

    Rules the checker deliberately skips: a function applied to a filtered field, formula fields, and the number of matching records, because none of those can be proven from the query text. Filters on indexed fields with selective values are fine, and a query flagged here can still be the right query. The 50-field threshold is our own rule of thumb, not a Salesforce limit.

    Worked examples

    Each result below is generated at build time by the same formatter the tool runs, with the default options.

    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

    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

    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

    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

    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

    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

    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

    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

    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

    A query the checker has hints for

    SELECT Id, Name FROM Account WHERE Name LIKE '%corp' AND Type != 'Prospect' ORDER BY Name
    • LIKE with a leading wildcard, such as LIKE '%abc' warning · matched: LIKE '%corp'

      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. Related limit

    • Negative operator in WHERE: !=, <>, NOT IN, NOT LIKE or EXCLUDES warning · matched: !=

      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. Related limit

    • No LIMIT info · matched: no LIMIT

      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. Related limit

    • ORDER BY without LIMIT info · matched: ORDER BY

      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. Related limit

    Where to go next

    A tidy query is easier to review, but the limits decide whether it runs. See the SOQL query count limit, the records retrieved limit and the heap size limit, or use the full governor limits reference.

    Frequently asked questions

    What does a SOQL formatter do?

    It rewrites a query so it is easy to read: keywords in uppercase, each clause such as FROM, WHERE and ORDER BY on its own line, fields and conditions listed one per line, and subqueries indented. It changes only whitespace and the case of keywords, so the query returns the same records.

    Does formatting change what my query returns?

    No. The formatter splits the query into tokens and prints the same tokens again with different spacing and keyword case. String literals, bind variables, date literals and field names are copied as you wrote them. SOQL keywords are not case-sensitive, so uppercasing them does not change the meaning.

    Can this tool tell me if my SOQL query is selective?

    No. Whether a filter is selective depends on how many records match in your org and on which fields are indexed, and a query on its own does not contain that information. The checker only flags patterns that commonly cause trouble, such as a leading wildcard or a negative operator, and labels them as heuristics.

    Why does the checker flag a leading wildcard or a negative operator?

    A pattern such as LIKE '%abc' does not start with a fixed value, so an index generally cannot be used to find matches. Negative operators such as !=, NOT IN, NOT LIKE and EXCLUDES match everything except a few values, so they generally cannot narrow the data through an index either. In both cases the filter may be non-selective, so test the query against realistic data.

    Does the formatter handle subqueries, TYPEOF and bind variables?

    Yes. Parent-child subqueries in SELECT and semi-join subqueries in WHERE are indented as their own blocks, TYPEOF branches are indented under TYPEOF, and bind variables such as :accountIds, date literals such as LAST_N_DAYS:5 and quoted strings with escapes stay exactly as written.

    How many SOQL queries and records can an Apex transaction use?

    A synchronous Apex transaction can issue 100 SOQL queries and an asynchronous one 200, and a transaction can retrieve 50,000 records in total with SOQL. Each parent-child relationship subquery counts as an extra query. The governor limits reference on this site lists these and the other limits.

    Is my query sent to a server?

    No. Formatting and checking run in your browser, and the query you paste is not sent to SFDX Talk or anyone else.

    More tools

    SFDX Talk is unofficial documentation. It is not affiliated with, endorsed by, or sponsored by Salesforce, Inc. Salesforce changes its platform every release, so confirm against the official page before relying on a value.