SQL Interview Questions | OFFSET Command Problem

Question

Sometimes you are not given a problem, but are asked to create one along with the solution. You will understand this concept through the next question.

What is the function of OFFSET command? You might know the definition but create a scenario where you have to make use of it. Also, can the same process be done with any other method?

in progress 0
Dhruv2301 4 days 2 Answers 32 views Contributor 0

Answers ( 2 )

  1. OFFSET command is used to skip rows from the results which has
    been fetched from the query.
    Suppose we have a table and we want to fetch all the rows apart
    from the first 5 rows, then we can use OFFSET command.
    An alternate method to do similar kind of operation would be to use
    the Row_Number() window function. This function assigns a unique
    row number to every record in the table starting from 1. Using this
    column we can impose the condition with the help of the ‘WHERE’ clause.

  2. OFFSET command is used to skip rows from the results.
    Best example/Scenario : lets find the third highest salary without using any sub -query.
    select * from table order by salary desc limit 1 offset 2;

    Alternate Solutions : you all may know
    select * from (select *,dense_rank() over (order by salary desc) as d_rank from table) t where t.d_rank=3;

    Good Luck 🙂

Leave an answer

Browse
Browse