Question: Let us say that I have this table

```
-----------------
|  id   |   word |
|  5    |   aab  |
|  6    |   sea  |
|  7    |   blue |
|  8    | color  |
|  9    |  nn    |
|  10   |  pp    |
|  11   |  oo    |
|  12   |  qq    |

```

a person visited the page where the `id` is `8`

what I want to display is two results before and after this `id`. those two results are alphabetically ordered so in the command I should get : 

```
aab - blue - color - nn - oo 

```

How can I do that in mysql query? I am looking for the best efficient code. How many querys do I need to run? How can I get the results before my id alphabitically and get only two from each side. Please help.

Answer: 
You can do this with `limit` and `union all`:

```
select word
from ((select *
       from t
       where word<= (select word from t where id = 8)
       order by word desc
       limit 3
      ) union all
      (select *
       from t
       where word> (select word from t where id = 8)
       order by word
       limit 2
      )
     ) t
order by word;

```
You should create a sorted version of that table. Then, when given an ID, you can just do arithmetic on the IDs.

```
/* How to create the sorted table */
create table sorted_words 
(new_id mediumint not null auto_increment, primary key (new_id))
as select id as old_id, word from words order by word asc;

/* Query for getting two above and two under */
select id, word from sorted where id between 6 and 10

```

Working fiddle: <http://sqlfiddle.com/#!2/c6734/1>