Add a column to the query which can sub identify the data to sort on that.
In the below example I use a Common Table Expression with the selects you showed which places them in specific groups in the CTE, and then do a union
off of both of those groups into AllStudents
.
The final select will then sort AllStudents
by the SortIndex
column first and then by the name
such as:
WITH Juveniles as
(
Select 1 as [SortIndex], id,name,age From Student
Where age < 15
),
AStudents as
(
Select 2 as [SortIndex], id,name,age From Student
Where Name like "%a%"
),
AllStudents as
(
select * from Juveniles
union
select * from AStudents
)
select * from AllStudents
sort by [SortIndex], name;
To summarize, it will get all the students which will be sorted by group first, and subsorted by the name within the group after that.