SQLite Forum

ORDER BY out of order. Is this a bug or expected?
Login
```
create table t(n float);
insert into t values(-574.41),(500.00);

select printf('%8.2f',n) as n
  from t
  order by n desc;
```
gives
```
+----------+
|    n     |
+----------+
|  -574.41 |
|   500.00 |
+----------+
```

which indicates n is treated as string (the result of printf) instead of float (the column's affinity).

So, the question is:

Should `ORDER BY` see the alias of the printf that masks the original column name on the `SELECT` part, or the actual column name which would produce the correct result as indicated by changing the alias?

```
select printf('%8.2f',n) as nn
  from t
  order by n desc;
```

gives

```
+----------+
|    nn    |
+----------+
|   500.00 |
|  -574.41 |
+----------+
```

BTW, using a cast over printf to convert to float for `ORDER BY`'s sake ruins the formatting so it's no-go.