There are few question like this, but they are not constructed in a simple way that make the answer accessible to more generic cases. These question were built for specific cases.
Suppose we have a table
id letter
----------
1 a
2 a
3 b
4 b
5 a
how do we get this result ?
1 a 2
3 b 2
5 a 1