How to Fix Error 1250 (42000) "Table from One of the SELECTs Cannot Be Used in Global ORDER Clause" When Using UNION in MySQL
This article explains the causes of Error 1250 in MySQL and provides solutions to fix it when using UNION.
The UNION Sorting Dilemma
When working with MySQL’s UNION
operator, sorting the combined results seems like it should be straightforward—until you encounter Error 1250. This frustrating message appears when you try to reference individual tables from your union components in the global ORDER BY
clause. It’s MySQL’s way of saying, “I can’t sort by this because it doesn’t exist in the final combined result.”
This error typically occurs because the UNION
operation creates an anonymous result set, making original table references unavailable for sorting. The database engine needs you to work with the unified structure rather than the component parts when applying global operations.
Why MySQL Rejects Table References in ORDER BY
The Nature of UNION Results
A UNION
operation produces a new, temporary result set that:
- Combines rows from multiple queries
- Eliminates duplicates (unless using
UNION ALL
) - Has its own column structure independent of source tables
The Scope Problem
When you write:
SELECT a, b FROM table1
UNION
SELECT x, y FROM table2
ORDER BY table1.b; # Error - table1 doesn't exist in this context
MySQL can’t access table1
in the ORDER BY
because the sort operation applies to the final combined result, not the intermediate steps.
Correct Approaches to Sorting UNION Results
Using Column Aliases or Positions
Reference columns by their unified names or positions:
-- Using column aliases
SELECT id, name AS employee_name FROM staff
UNION
SELECT user_id, full_name FROM contractors
ORDER BY employee_name; # Works - references output column
-- Using column positions
SELECT id, name FROM employees
UNION
SELECT uid, username FROM system_users
ORDER BY 2; # Sorts by second column
Creating a Derived Table
Wrap your union in a subquery to sort the final results:
SELECT * FROM (
SELECT product_id, product_name FROM current_products
UNION
SELECT item_id, description FROM archived_items
) AS combined_products
ORDER BY product_name;
Advanced Sorting Techniques
Sorting by Non-Selected Columns
Include sorting columns in your SELECT even if you don’t need them displayed:
SELECT name, department, salary FROM (
SELECT name, department, salary, 1 AS source FROM employees
UNION
SELECT full_name, dept, compensation, 2 FROM contractors
) AS all_workers
ORDER BY source, salary DESC;
Conditional Sorting with CASE
Implement complex sorting logic that considers the union source:
SELECT id, name, type FROM (
SELECT emp_id AS id, emp_name AS name, 'employee' AS type FROM staff
UNION
SELECT cont_id, cont_name, 'contractor' FROM contractors
) AS personnel
ORDER BY
CASE WHEN type = 'employee' THEN 0 ELSE 1 END,
name;
Common Pitfalls and Solutions
Attempting to Sort Individual UNION Components
This doesn’t work as expected:
(SELECT a, b FROM table1 ORDER BY b)
UNION
(SELECT x, y FROM table2 ORDER BY y); # ORDER BY is ignored here
Instead, apply sorting at the end:
SELECT a AS col1, b AS col2 FROM table1
UNION
SELECT x, y FROM table2
ORDER BY col2;
Mixing Different Column Names
Ensure consistent aliases when columns represent the same concept:
-- Problematic
SELECT user_id, login_name FROM admins
UNION
SELECT id, username FROM users
ORDER BY login_name; # Fails for user records
-- Fixed version
SELECT user_id AS id, login_name AS username FROM admins
UNION
SELECT id, username FROM users
ORDER BY username;
Performance Considerations
Indexing for UNION Sorts
For large result sets, ensure your sort columns are properly indexed in the source tables:
-- Add indexes if frequently sorting by these columns
CREATE INDEX idx_name ON employees(name);
CREATE INDEX idx_full_name ON contractors(full_name);
Limiting Sorted Results
Apply LIMIT
after sorting to improve performance:
SELECT id, name FROM (
SELECT emp_id AS id, emp_name AS name FROM employees
UNION
SELECT cont_id, cont_name FROM contractors
) AS combined
ORDER BY name
LIMIT 100;
Best Practices for UNION Sorting
- Always use column aliases to create consistent names across all
SELECT
statements - Document your union structures so others understand the result schema
- Test sort operations with small datasets before applying to production data
- Consider using views for frequently used unions with standard sorting
- Prefer positional references only for simple, stable queries
Conclusion
Error 1250 serves as an important reminder that UNION
operations create new, independent result sets in MySQL. When sorting these combined results, we must work with the unified structure rather than the original table references. By using consistent column aliases, derived tables, and positional references, we can implement effective sorting while avoiding this common error.
Remember that the key to successful union sorting lies in understanding the scope of each operation. The UNION
happens first, creating an anonymous result set, and then the ORDER BY
operates on that result. Keeping this execution order in mind will help you structure your queries correctly and achieve the desired sorted output without runtime errors.