-1

I want to fetch records from a table using the sequence provided in "IN" operator

select * from table where id in (10, 5, 30)

I want this result

ID | Name
10 | Xyz
5  | Abc
30 | Jkl

but it is actually showing this result

ID | Name
5  | Abc
10 | Xyz
30 | Jkl
Salman A
  • 262,204
  • 82
  • 430
  • 521

1 Answers1

1

IN cannot be used to determine the order of results. You need to use some kind of row value constructor:

select *
from (
    select 10 as id, 1 as sort union all
    select 5,        2         union all
    select 30,       3
) as custom_list
join your_table on custom_list.id = your_table.id
order by custom_list.sort
Salman A
  • 262,204
  • 82
  • 430
  • 521