Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use aliased field to use in ORDER BY clause in MySQL?

Can we use aliased field name in ORDER BY clause?

For example:

SELECT id, name AS firstname
FROM users
ORDER BY firstname


Is it possible? When I tried this it errored out.

like image 688
Rinto George Avatar asked Feb 03 '12 05:02

Rinto George


1 Answers

An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column.

Check here.

like image 73
xdazz Avatar answered Oct 13 '22 06:10

xdazz