writing SQL queries to get neighbour rows
I have come across some problems writing queries for getting neighbour rows and have no idea what to do about it.

This is the scenario:

There is an entity A which has 1000 entity B, each entity B has 1000 entity C, each entity C has 1000 entity D. At first we have a query to get all entity Ds inside entity A. Then we implement some logic so that we can navigate from a particular D to the next D or previous D, but this logic is not written as SQL queries.

If I want to implement the navigation logic as SQL queries, how can I go to the next D in my select query? Suppose the original query is as simple as 

    select * from D 
      inner join C where C.ID = D.FK
      inner join B where B.ID = C.FK


writing SQL queries to get neighbour rows
===

I have come across some problems writing queries for getting neighbour rows and have no idea what to do about it.

This is the scenario:

There is an entity A which has 1000 entity B, each entity B has 1000 entity C, each entity C has 1000 entity D. At first we have a query to get all entity Ds inside entity A. Then we implement some logic so that we can navigate from a particular D to the next D or previous D, but this logic is not written as SQL queries.

If I want to implement the navigation logic as SQL queries, how can I go to the next D in my select query? Suppose the original query is as simple as 

    select * from D 
      inner join C where C.ID = D.FK
      inner join B where B.ID = C.FK

