Syntax error: Unexpected identifier
BigQuery says "Unexpected identifier" when a word turns up where a statement had to start. The position in the message is the word it choked on, which is usually one token after the real mistake.
Syntax error: Unexpected identifier "sselect" at [1:1]
The fix
Look one token to the left of the position in the message. In almost every case the real problem is there: a missing comma, a missing semicolon, or a keyword spelled wrong so that the parser read it as an alias.
What BigQuery means
A word appeared where a statement had to start.
BigQuery has a separate message, Expected end of input but got identifier "x", for a word
that turns up in the middle of an otherwise complete statement. Which of the two you get tells
you where the parser was when it gave up:
| Message | Where the parser was |
|---|---|
Unexpected identifier "x" at [L:C] | at the start of a statement, waiting for SELECT, WITH, INSERT … |
Expected end of input but got identifier "x" | it had already parsed a whole statement and found more text after it |
The second one is more common. [L:C] points at the first token the parser could not use. The
actual mistake is normally one token earlier.
Where it comes from
A typo in the leading keyword.
sselect 1
Pasted code that is not SQL. Any language whose first word is an identifier lands here:
const x = 1;
Syntax error: Unexpected identifier "const" at [1:1]
A pasted Markdown code fence gives the same error with an empty name, because the three
backticks are read as an empty quoted identifier. import is a special case with its own message, because IMPORT is a real GoogleSQL
keyword. See Expected keyword MODULE or keyword PROTO.
A missing comma in the select list. BigQuery read a b as a AS b, then found a third
name with nothing to attach it to:
SELECT a b c FROM (SELECT 1 a, 2 b, 3 c)
Syntax error: Expected end of input but got identifier "c" at [1:12]
A misspelled keyword. WEHRE became an alias for the table, and then x had no home:
WITH t AS (SELECT 1 x) SELECT * FROM t WEHRE x = 1
Syntax error: Expected end of input but got identifier "x" at [1:46]
Note where the position lands. The mistake is at column 40, the message says 46.
A missing semicolon between two statements.
SELECT 1
SELECT 2
Syntax error: Expected end of input but got keyword SELECT at [2:1]
Things that are fine
- A trailing comma before
FROM.SELECT 1, 2, FROM tis valid in BigQuery. - A
#standardSQLline at the top. It parses. --and#and/* */comments. All three work.//does not, and givesSyntax error: Unexpected "/".
Things that are not fine
- A reserved word as a bare alias.
SELECT 1 AS ordergivesSyntax error: Unexpected keyword ORDER. Backtick it or rename it. - A dash in an identifier outside the first path part.
SELECT 1 AS my-colgivesSyntax error: Expected end of input but got "-". - Smart quotes from a word processor.
Syntax error: Illegal input character "\342".
In Querylab.io
Querylab.io parses in the browser as you type, so these are marked before anything is sent to BigQuery, and the message names what is missing rather than the token after it. The missing comma above is reported as “Expected comma between SELECT items”.