Suppose the table is:
Data
| id | val |
|---|---|
| 0 | 4 |
| 1 | 7 |
| 2 | 2 |
| 3 | 1 |
| 4 | 9 |
consider the query:
SET @r:=0;
SELECT val, @r:=@r+1 as row_num
FROM Data
ORDER BY val;
Now, I have read that ORDER BY executed after SELECT then this query should give output as below:
| val | row_num |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 4 | 1 |
| 7 | 2 |
| 9 | 5 |
BUT the query gives output as below:
| val | row_num |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 4 | 3 |
| 7 | 4 |
| 9 | 5 |
It is like ORDER BY executed first and then the SELECT executed. How the query is executing actually?