In the query above, first we combine id and name of both employees and customers into one result set using the UNION operator. Then we sort the result set by using the ORDER BY clause. Notice that we put the SELECT statements inside the parentheses and place the ORDER BY clause as the last statement.
If you place the ORDER BY clause in each SELECT statement, it will not affect the order of the rows in the final result produced by the UNION operator.
MySQL also provides you with alternative option to sort the result set based on column position using ORDER BY clause as the following query: