Using UNION and ORDER BY in MySQL: A Solution for Group-wise Sorting

Nov 27, 2025 · Programming · 10 views · 7.8

Keywords: MySQL | UNION | ORDER BY | Group-wise Sorting | Pseudo-column

Abstract: This article explores the challenge of combining UNION and ORDER BY in MySQL queries to achieve group-wise sorting. By analyzing real-world search scenarios, we propose a solution using a pseudo-column (Rank) to ensure independent sorting within each UNION subquery. The paper details the working mechanism of the pseudo-column, distinguishes between UNION and UNION ALL, and provides comprehensive code examples for implementing exact search, within 5 km search, and 5-15 km search with group-wise ordering. Additionally, performance optimization and common error handling are discussed, offering practical guidance for developers.

Problem Background and Requirements Analysis

In web development, it is often necessary to retrieve different types of data from a database and merge and sort them based on specific criteria. For example, in a geolocation search application, users may want to display exact match results, results within 5 km, and results within 5-15 km simultaneously. While MySQL's UNION operator easily combines results from multiple SELECT queries, adding an ORDER BY clause directly after UNION sorts all results globally, failing to maintain independent ordering within each subquery's results.

Core Solution: Application of Pseudo-Column (Rank)

To address this issue, we introduce a pseudo-column (commonly named Rank) that assigns a unique priority value to each subquery. By sorting first by Rank and then by other fields (e.g., ID or add_date), group-wise sorting can be achieved. Below is a complete example based on the user scenario:

SELECT *
FROM (
    SELECT 1 AS Rank, id, add_date, name, distance
    FROM locations
    WHERE name = '<search_place>'
    UNION ALL
    SELECT 2 AS Rank, id, add_date, name, distance
    FROM locations
    WHERE distance < 5
    UNION ALL
    SELECT 3 AS Rank, id, add_date, name, distance
    FROM locations
    WHERE distance BETWEEN 5 AND 15
) AS combined_results
ORDER BY Rank, add_date DESC;

In this query:

Technical Details and Optimization Suggestions

Using UNION ALL instead of UNION can improve query performance, as UNION ALL does not remove duplicate rows, avoiding additional deduplication overhead. In most search scenarios, the result sets of subqueries are mutually exclusive, making UNION ALL safe and efficient.

If more complex sorting logic is needed for each subquery, ORDER BY can be used inside the subqueries, but note: directly using ORDER BY in UNION subqueries may not yield expected results unless combined with LIMIT. Therefore, it is recommended to handle sorting uniformly in the outer query.

Common Issues and Error Handling

In practice, developers might encounter the following issues:

Conclusion

By introducing a pseudo-column Rank, we successfully resolve the group-wise sorting issue when combining UNION and ORDER BY in MySQL. This method is not only applicable to geolocation searches but can be extended to any scenario requiring merged result sets with maintained individual sort orders. Developers should adjust Rank values and sorting fields based on specific needs to achieve optimal user experience.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.