drop table if exists t; create table t (c1 int, c2 int); insert into t values (2, 3), (3, 3), (1, 1), (1, 2), (2, 2), (1, 3), (1,4), (1,5), (null, 2), (null, 3); select * from t order by c1, c2; c1 c2 NULL 2 NULL 3 1 1 1 2 1 3 1 4 1 5 2 2 2 3 3 3 select * from t order by c1 desc, c2; c1 c2 3 3 2 2 2 3 1 1 1 2 1 3 1 4 1 5 NULL 2 NULL 3 select * from t order by c1 asc, c2 desc; c1 c2 NULL 3 NULL 2 1 5 1 4 1 3 1 2 1 1 2 3 2 2 3 3 select * from t order by c2 desc, c1 desc; c1 c2 1 5 1 4 3 3 2 3 1 3 NULL 3 2 2 1 2 NULL 2 1 1 select * from t order by c1 desc, c2 desc; c1 c2 3 3 2 3 2 2 1 5 1 4 1 3 1 2 1 1 NULL 3 NULL 2 select * from t order by 3 desc, c1; Error 1054: Unknown column '3' in 'order clause' select c1 as a from t order by a; a NULL NULL 1 1 1 1 1 2 2 3 select c1 as a from t order by c1; a NULL NULL 1 1 1 1 1 2 2 3 select c1 as a from t order by c2, c1; a 1 NULL 1 2 NULL 1 2 3 1 1 select c1 as a from t order by max(c1), c1; a 2 select c1 as a from t order by c2, max(c2), c1; a 2 select c1 from t group by c1 order by c1; c1 NULL 1 2 3 select c1 from t group by c1 order by count(c1), c1; c1 NULL 3 2 1 select c1 from t group by c1 order by count(c1) desc, c1; c1 1 2 3 NULL select c1 from t group by c1 order by max(c1), c1; c1 NULL 1 2 3 select c1 from t group by c1 order by max(c1) desc, c1; c1 3 2 1 NULL select c1 from t group by c1 order by c2, c1; c1 1 NULL 2 3 select c1 from t group by c1 order by c2 desc, c1; c1 2 3 NULL 1 select c1 from t group by c1 order by max(c2), c1; c1 NULL 2 3 1 select c1 from t group by c1 order by max(c2) desc, c1; c1 1 NULL 2 3 select * from t group by c2 having c2 > 1 order by c2 desc, c1; c1 c2 1 5 1 4 2 3 1 2 select c1 from t group by c2 having c2 > 1 order by c2 desc, c1; c1 1 1 2 1 select * from t group by c2 having c2 > 1 order by c1 desc, c2; c1 c2 2 3 1 2 1 4 1 5 select c2 from t group by c2 having c2 > 1 order by c1 desc, c2; c2 3 2 4 5 select * from t having c2 > 1 order by c1 desc, c2; c1 c2 3 3 2 2 2 3 1 2 1 3 1 4 1 5 NULL 2 NULL 3 select c2 from t having c2 > 1 order by c1 desc, c2; c2 3 2 3 2 3 4 5 2 3 select c2 from t having count(c2) > 4 order by c1 desc, c2; c2 3 select * from t group by c2 having c2 > 1 order by c1 desc, c2 desc; c1 c2 2 3 1 5 1 4 1 2 select * from t group by c2 having c2 > 1 order by max(c1), c2; c1 c2 1 4 1 5 1 2 2 3 select c2 from t group by c2 having c2 > 1 order by max(c1), c2; c2 4 5 2 3 select * from t group by c2 having c2 > 1 order by max(c2), c1; c1 c2 1 2 2 3 1 4 1 5 select * from t group by c2 having c2 > 1 order by 2 desc, c1; c1 c2 1 5 1 4 2 3 1 2 select * from t group by c2 having c2 > 1 order by max(c2), c2 desc; c1 c2 1 2 2 3 1 4 1 5 select * from t group by c2 having c2 > 1 order by 2 desc, max(c2), c1; c1 c2 1 5 1 4 2 3 1 2 drop table if exists t; create table t (c1 int, c2 int, c3 int); insert into t SET c1=1, c2=2, c3 = 3; insert into t SET c1=2, c2=3, c3 = 1; insert into t SET c1=3, c2=1, c3 = 2; select c1 as c2 from t order by c2 + c3, c1; c2 3 2 1 select c1 as a from t order by c2 + c3, c1; a 3 2 1 select c1 as a from t order by a + c3, c1; a 2 1 3 select c1 as a from t order by c1 + c3, c1; a 2 1 3 select c1 as c2 from t order by c2 + 1, c1; c2 3 1 2 select c1 as c2, c2 from t order by c2, c1; Error 1052: Column 'c2' in field list is ambiguous select c1 as c2, c2 from t order by c2 + 1, c1; c2 c2 3 1 1 2 2 3