Can I ORDER BY an Alias Column (MSSQL)?

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Column Sql
Click For Summary

Discussion Overview

The discussion revolves around the use of aliasing in SQL queries, specifically whether one can use an alias in the ORDER BY clause of a query in Microsoft SQL Server (MSSQL). Participants explore the implications of alias computation timing in relation to query execution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires whether it is possible to use an alias (e.g., 'x') in the ORDER BY clause instead of the original column name.
  • Another participant asserts that it is indeed possible to use the alias in the ORDER BY clause, providing an example query that works with the Northwinds database.
  • A different participant mentions having trouble downloading MSSQL2012 and suggests various workarounds for testing SQL queries, including using SQL Server 2014 or alternative database systems like MySQL.
  • One participant reflects on the computation order of aliases, suggesting that aliases are computed after the query runs, while the ORDER BY clause is processed afterward, which may affect the ability to use aliases.
  • Another participant notes rare instances where sorting on an alias did not work and mentions that they resolved such issues by ordering on the original expression instead.

Areas of Agreement / Disagreement

There is no clear consensus on the reliability of using aliases in the ORDER BY clause, as some participants provide examples supporting its use while others express uncertainty based on personal experiences with specific queries.

Contextual Notes

Participants discuss the internal workings of database engines regarding alias computation and ordering, but there are unresolved questions about the conditions under which alias sorting may fail.

WWGD
Science Advisor
Homework Helper
Messages
7,798
Reaction score
13,096
Hi All,
I am doing a query in which I am ALIASING a column name, say 'column name' AS x
Can I , at the end of the query ORDER BY x, or must I ORDER BY the given initial 'column name'?
(Still having trouble downloading free copy of MSSQL2012, so I cannot test the query).
I know this has to see with when the Alias is 'computed' in the query, but I am having trouble still.

Thanks.
 
Technology news on Phys.org
WWGD said:
Hi All,
I am doing a query in which I am ALIASING a column name, say 'column name' AS x
Can I , at the end of the query ORDER BY x, or must I ORDER BY the given initial 'column name'?
(Still having trouble downloading free copy of MSSQL2012, so I cannot test the query).
I know this has to see with when the Alias is 'computed' in the query, but I am having trouble still.

Thanks.
You can use ORDER BY x.
The following query on the Northwinds database works:
SQL:
SELECT CompanyName as expr2, ContactName, Left(Country,2) AS Expr1
FROM Customers
ORDER BY expr2,expr1;
 
  • Like
Likes   Reactions: WWGD
WWGD said:
Still having trouble downloading free copy of MSSQL2012, so I cannot test the query.
Can't remember if it was you or someone else, but possible solutions/work-arounds include (in increasing order of certainty):
  1. SQL Server 2014
  2. A cheap web host that runs SQL Server and run SQL Server Management Studio locally (although if you can't get SQL Server to work itself you might have problems)
  3. A cheap web host that runs SQL Server and provides an on-line management tool to run in your browser - this is guaranteed to work
  4. Install XAMPP and use MySql not MS SQL of course - fine for standard dialect (although watch out for CONCAT, that's usually the first thing to catch you out)
  5. Use an even cheaper web host to run MySQL (they all do this in their standard package) and provides an on-line management tool (usually PhpMyAdmin) to run in your browser - this is guaranteed to work
  6. Use sqlfiddle e.g. http://sqlfiddle.com/#!6/f892f/8 - this is also guaranteed to work and once you have set a reusable test schema is quicker to try for real than asking a question here (not that it is not a pleasure to answer your questions here of course...)
A couple of solutions there that are guaranteed to work with various degrees of compromise, but anything is better than just guessing whether your code will work when you are learning.
 
  • Like
Likes   Reactions: WWGD
Samy_A said:
You can use ORDER BY x.
The following query on the Northwinds database works:
SQL:
SELECT CompanyName as expr2, ContactName, Left(Country,2) AS Expr1
FROM Customers
ORDER BY expr2,expr1;
Thanks, Samy_A . I guess this has to see with the order in which using ALIAS for a table is computed, i.e., it is computed after running the query, while , e.g., order by is computed/processed after the query is run, after the columns have already been aliased/renamed, right?
 
WWGD said:
Thanks, Samy_A . I guess this has to see with the order in which using ALIAS for a table is computed, i.e., it is computed after running the query, while , e.g., order by is computed/processed after the query is run, after the columns have already been aliased/renamed, right?
I don't know much about the internal working of these database engines. But yes, it looks like that, or at least the database engine has a map of the fields and the aliases ready before running the query and doing the ordering.
I have had rare instances with queries where sorting on the alias didn't work. As it was easily solved by just ordering on the expression itself, I never bothered to find out under what precise condition it doesn't work.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
Replies
7
Views
3K
Replies
11
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
4
Views
4K