•Only the last SELECT  statement can use an INTO  clause. However, the entire UNION  result is written to the INTO  output destination. 

As of MySQL 8.0.20, these two UNION  variants containing INTO  are deprecated and you should expect support for them to be removed in a future version of MySQL: 

•In the trailing query block of a query expression, use of INTO  before FROM  produces a warning. Example: 

... UNION SELECT * INTO OUTFILE ’ file_name ’ FROM table_name ; 

•In a parenthesized trailing block of a query expression, use of INTO  (regardless of its position relative to FROM ) produces a warning. Example: 

... UNION (SELECT * INTO OUTFILE ’ file_name ’ FROM table_name ); 

Those variants are deprecated because they are confusing, as if they collect information from the named table rather than the entire query expression (the UNION ). 

UNION  queries with an aggregate function in an ORDER BY  clause are rejected with an ER_AGGREGATE_ORDER_FOR_UNION  error. Example: 

## UNION Handing in MySQL 8.0 Compared to MySQL 5.7 

SELECT 1 AS foo UNION SELECT 2 ORDER BY MAX(1); 

In MySQL 8.0, the parser rules for SELECT  and UNION  were refactored to be more consistent (the same SELECT  syntax applies uniformly in each such context) and reduce duplication. Compared to MySQL 5.7, several user-visible effects resulted from this work, which may require rewriting of certain statements: 

• NATURAL JOIN  permits an optional INNER  keyword ( NATURAL INNER JOIN ), in compliance with standard SQL. 

•Right-deep joins without parentheses are permitted (for example, ... JOIN ... JOIN ... ON ... ON ), in compliance with standard SQL. 

• STRAIGHT_JOIN  now permits a USING  clause, similar to other inner joins. 

•The parser accepts parentheses around query expressions. For example, (SELECT ... UNION SELECT ...)  is permitted. See also Section 13.2.10.4, “Parenthesized Query Expressions” . 

•The parser better conforms to the documented permitted placement of the SQL_CACHE  and SQL_NO_CACHE  query modifiers. 

•Left-hand nesting of unions, previously permitted only in subqueries, is now permitted in top-level statements. For example, this statement is now accepted as valid: 

(SELECT 1 UNION SELECT 1) UNION SELECT 1; 

•Locking clauses ( FOR UPDATE , LOCK IN SHARE MODE ) are allowed only in non- UNION  queries. This means that parentheses must be used for SELECT  statements containing locking clauses. This statement is no longer accepted as valid: 

SELECT 1 FOR UPDATE UNION SELECT 1 FOR UPDATE; 

Instead, write the statement like this: 

(SELECT 1 FOR UPDATE) UNION (SELECT 1 FOR UPDATE); 

