The difference in behavior is because UNION
is a Reserved Word in MySQL. It can't be used as an identifier unless it is escaped.
In MySQL, identifiers can be escaped by enclosing them in backtick characters.
The first statement works, because the token following the keyword FROM is interpreted as an identifier, because it's enclosed in backtick characters.
The second statement is throwing syntax error, because the token is interpreted as a reserved word UNION
and that appears in a spot where MySQL isn't expecting it (where MySQL doesn't allow it.)
Excerpt from the MySQL Reference manual:
backticks
Identifiers within MySQL SQL statements must be quoted using the backtick character (`) if they contain special characters or reserved words. For example, to refer to a table named FOO#BAR or a column named SELECT, you would specify the identifiers as `FOO#BAR` and `SELECT`. Since the backticks provide an extra level of safety, they are used extensively in program-generated SQL statements, where the identifier names might not be known in advance.
Many other database systems use double quotation marks (") around such special names. For portability, you can enable ANSI_QUOTES mode in MySQL and use double quotation marks instead of backticks to qualify identifier names.