home Homebuild Toolsbug_report Errorsmenu_book Guideslightbulb Tipssmart_toy Promptsextension Extensionsfolder_open Resourcesinfo About
search
error

MALFORMED_QUERY: Query parsing error: unexpected token: 'AS' (or similar keyword)

What does this error mean?

MALFORMED_QUERY is the API-level error code returned when the Salesforce query engine cannot parse your SOQL string. The message identifies the token — a keyword, symbol, or value — that it encountered but didn't expect at that position in the query. This error comes from the REST API, SOAP API, or Apex Database.query() calls, and is distinct from the System.QueryException thrown in synchronous Apex.

SOQL is not SQL. Many SQL constructs — AS aliases, JOIN, CAST, subqueries in SELECT, UNION — are not supported in SOQL and will produce this error.

Common Causes

1. Using SQL syntax not supported in SOQL

SOQL does not support AS for aliasing, JOIN between unrelated objects, CAST(), UNION, or arbitrary subqueries in the SELECT clause.

2. Misplaced or unclosed parentheses

Complex WHERE clauses with nested AND/OR groupings that have unbalanced parentheses confuse the SOQL parser, producing an unexpected token at the mismatched position.

3. Invalid operator for field type

Using LIKE on a non-text field, or INCLUDES on a standard picklist (only multi-select picklists support INCLUDES).

How to Fix It

Solution 1: Replace unsupported SQL syntax with SOQL equivalents

SOQL
-- ❌ SQL syntax — not valid in SOQL
SELECT Id AS AccountId FROM Account
SELECT a.Id, c.Name FROM Account a JOIN Contact c ON c.AccountId = a.Id

-- ✅ SOQL equivalent — relationship query
SELECT Id, (SELECT Id, Name FROM Contacts)
FROM Account
WHERE Id IN :accountIds

Solution 2: Validate your query in Workbench or Developer Console

Before embedding a query in code, test it in Workbench → SOQL Query or the Developer Console → Query Editor. Both give immediate parse errors with line and character positions, making it easy to pinpoint the offending token.

lightbulb

Pro Tip: The Salesforce SOQL documentation lists every supported clause and function. When porting SQL from another system, check each construct against the SOQL reference at developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta before running it.