I interpret the question as: show names starting with Y if names starting with X are not found. This solution will be quick, as it will reduce the existing one since the discovery of 1 record
if exists(select * from table where name like 'X%')
begin
select * from table where name like 'X%'
end
else
begin
select * from table where name like 'Y%'
end
Ideally, name columns are indexed to make this work well.
source
share