Can I ORDER BY an Alias Column (MSSQL)?

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Column Sql
AI Thread Summary
In SQL queries, it is possible to use an alias in the ORDER BY clause. For example, when aliasing a column as 'x', you can order by 'x' instead of the original column name. This functionality is confirmed with a working example from the Northwinds database. The discussion also touches on the timing of when aliases are computed in relation to the execution of the query, indicating that aliases are processed after the query runs, allowing for their use in ordering. There are suggestions for alternatives to test SQL queries without having access to MSSQL2012, including using web hosts with SQL Server, XAMPP for MySQL, or online tools like SQL Fiddle. Some users note that while ordering by an alias generally works, there have been rare cases where it did not, suggesting that ordering directly by the original expression can be a reliable workaround.
WWGD
Science Advisor
Homework Helper
Messages
7,700
Reaction score
12,726
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 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 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top