Working answer (in MySQL):
If you have the following table "names":
+------+--------+
| id | name |
+------+--------+
| 1 | foo |
| 2 | bar |
| 3 | foobar |
+------+--------+
And you want to know where "foo" ranks alphabetically, then:
SELECT z.rank FROM (
SELECT t.id, t.name, @rownum := @rownum + 1 AS rank
FROM names t, (SELECT @rownum := 0) r
ORDER BY name ASC
) as z WHERE id=1;
Will produce:
+------+
| rank |
+------+
| 2 |
+------+
The changes from @potNpan's solution are the addition of as z
and the change from @rownum = @rownum + 1
to @rownum := @rownum + 1
. Now it works :)