image3

How to Fix “ERROR 1250 (42000): Table ‘…’ from one of The SELECTs Cannot be Used in Global ORDER Clause” When Using UNION in MySQL

If we face “ERROR 1250 (42000): Table ‘…’ from one of the SELECTs cannot be used in global ORDER clause” then this means that we are are using “ORDER BY” clause with tables name. This does not work in MySQL.

(SELECT EmployeeName FROM Employees)
UNION
(SELECT CompanyName FROM Companies)
ORDER BY Companies.CompanyName ASC;

In order to solve this problem we have 2 solutions. Either we can remove alias or table name. from the query,

(SELECT EmployeeName FROM Employees) 
UNION 
(SELECT CompanyName FROM Companies) ORDER BY Companies.CompanyName ASC;

or we can use alias upfront in the query and then we can use the reference of that alias after order by.

(SELECT EmployeeName e FROM Employees) 
UNION 
(SELECT CompanyName FROM Companies) ORDER BY e ASC;

This is how we can fix “ERROR 1250 (42000): Table ‘…’ from one of The SELECTs Cannot be Used in Global ORDER Clause” When Using UNION in MySQL.