Using union and order by clause in mysql
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In MySQL, UNION combines result sets from multiple SELECT statements, and ORDER BY can then sort the combined output. The main rule that trips people up is that the final ORDER BY applies to the unioned result, not independently to each branch unless you explicitly isolate those branches with subqueries.
Basic UNION plus final ORDER BY
The normal pattern is:
This means:
- combine both result sets
- remove duplicates because
UNIONis distinct by default - sort the final combined result by
name
If you want duplicates preserved, use UNION ALL:
The columns must line up
Every SELECT inside a UNION must produce the same number of columns with compatible types.
Valid example:
Invalid example:
That fails because the branch column counts differ.
Use aliases for clean ordering
When the final ordering column comes from an expression, assign an alias in the first branch and order by that alias.
This is usually clearer than relying on column positions.
If you need branch-level ordering, wrap it
A common misunderstanding is expecting an ORDER BY inside each SELECT to control the final union output. That usually does not do what people want unless it is tied to a subquery with LIMIT or another reason the intermediate ordering matters.
Example:
Here the inner ORDER BY clauses matter because they determine which top 5 rows from each branch survive into the final union.
Use a wrapper query when the final result gets more complex
Wrapping the union in an outer query can make further ordering and filtering easier:
This is especially useful when the union is part of a larger reporting query.
Performance notes
UNION removes duplicates, so it does extra work compared with UNION ALL. If duplicate elimination is unnecessary, prefer UNION ALL for performance and clarity.
Practical guidance:
- use
UNION ALLunless you specifically need deduplication - sort only once at the final level when possible
- index the columns used in branch filters, even though final union sorting may still require extra work
Common Pitfalls
The most common mistake is assuming each branch's ORDER BY controls the final union order. Another is using UNION when UNION ALL was intended, which adds unnecessary deduplication work. Developers also often order by a column name that is not exposed consistently across the union result. Using positional ordering such as ORDER BY 2 can also make maintenance harder when the selected columns change. Finally, many queries become harder to reason about simply because the union is not wrapped in an outer query when later filtering or ordering logic would benefit from that structure.
Summary
- '
ORDER BYafter aUNIONsorts the final combined result.' - Use
UNION ALLwhen you do not need duplicate elimination. - Make sure every union branch returns matching column counts and compatible types.
- Use aliases for readable final ordering.
- Wrap branches in subqueries if branch-level ordering must matter before the union.
- Wrap the whole union in an outer query when later filtering or sorting becomes more complex.

