Search code examples
sqlsql-servert-sqlsql-order-byworkflow

Query to list all users and apply offset condition on it first then other filters


By using below query I am getting unwanted results.

select *     
from db_user 
where full_name like 'Admin%'    
order by date_created asc 
OFFSET 1 ROWS FETCH NEXT 10 ROWS ONLY;

Required Results:

  • To get list of all users and apply offset condition on it first then other filters like (having 'fullName' and orderBy 'desc').

Results Getting:

  • List of all users first with filters applied like (having 'fullName' and orderBy 'desc') and then offset applied.

How to arrange query flow to achieve the required result ?


Solution

  • Are you looking for something like this?

    select u.*     
    from (select u.*
          from db_user 
          order by id asc
          OFFSET 1 ROWS FETCH NEXT 10 ROWS ONLY
         ) u
    where full_name like 'Admin%'    
    order by date_created asc ;
    

    Use of offset without order by doesn't really make sense. But this seems to be what you are asking for.