SQL use column name alias without SELECTING - sql

I know I can specify a column alias like so:
SELECT stuff as mask
Is there a way I can specify a column alias without returning that column data in the result set? Essentially, I want to be able to make my query clean by doing:
SELECT doManipulationStuff(cleanCompactAlias)
Where
reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias

You can use a Common Table Expression (CTE) to create a subquery with aliases:
WITH clean_cte AS
(
SELECT reallyLong.misleading.andAnnoying.columnName1 as cleanCompactAlias1,
reallyLong.misleading.andAnnoying.columnName2 as cleanCompactAlias2
)
SELECT doManipulationStuff(cleanCompactAlias1),
doManipulationStuff(cleanCompactAlias2)
That way you can put all of your aliasing in the CTE and just forget about it when calling your functions. You still have to do the aliasing somewhere, but this at least keeps it out of your main query to make it a bit more readable.

You could use a subquery:
select doManipulationStuff(cleanCompactAlias)
from (select t.*, reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
. . .
) t

Related

Using calculation with an an aliased column in ORDER BY

As we all know, the ORDER BY clause is processed after the SELECT clause, so a column alias in the SELECT clause can be used.
However, I find that I can’t use the aliased column in a calculation in the ORDER BY clause.
WITH data AS(
SELECT *
FROM (VALUES
('apple'),
('banana'),
('cherry'),
('date')
) AS x(item)
)
SELECT item AS s
FROM data
-- ORDER BY s; -- OK
-- ORDER BY item + ''; -- OK
ORDER BY s + ''; -- Fails
I know there are alternative ways of doing this particular query, and I know that this is a trivial calculation, but I’m interested in why the column alias doesn’t work when in a calculation.
I have tested in PostgreSQL, MariaDB, SQLite and Oracle, and it works as expected. SQL Server appears to be the odd one out.
The documentation clearly states that:
The column names referenced in the ORDER BY clause must correspond to
either a column or column alias in the select list or to a column
defined in a table specified in the FROM clause without any
ambiguities. If the ORDER BY clause references a column alias from
the select list, the column alias must be used standalone, and not as
a part of some expression in ORDER BY clause:
Technically speaking, your query should work since order by clause is logically evaluated after select clause and it should have access to all expressions declared in select clause. But without looking at having access to the SQL specs I cannot comment whether it is a limitation of SQL Server or the other RDBMS implementing it as a bonus feature.
Anyway, you can use CROSS APPLY as a trick.... it is part of FROM clause so the expressions should be available in all subsequent clauses:
SELECT item
FROM t
CROSS APPLY (SELECT item + '') AS CA(item_for_sort)
ORDER BY item_for_sort
It is simply due to the way expressions are evaluated. A more illustrative example:
;WITH data AS
(
SELECT * FROM (VALUES('apple'),('banana')) AS sq(item)
)
SELECT item AS s
FROM data
ORDER BY CASE WHEN 1 = 1 THEN s END;
This returns the same Invalid column name error. The CASE expression (and the concatenation of s + '' in the simpler case) is evaluated before the alias in the select list is resolved.
One workaround for your simpler case is to append the empty string in the select list:
SELECT
item + '' AS s
...
ORDER BY s;
There are more complex ways, like using a derived table or CTE:
;WITH data AS
(
SELECT * FROM (VALUES('apple'),('banana') AS sq(item)
),
step2 AS
(
SELECT item AS s FROM data
)
SELECT s FROM step2 ORDER BY s+'';
This is just the way that SQL Server works, and I think you could say "well SQL Server is bad because of this" but SQL Server could also say "what the heck is this use case?" :-)

using subquery's column alias as a property in main query

i want to know if the main query can see the alias, here's an example:
SELECT AVG(values)
FROM(
SELECT SUM(a1) AS values
FROM tableX
)
Does the first query see the alias "values"?
Does the first query see the alias "values"?
Yes, it does. The subquery creates a derived table, and aliases act as column names in that context. However, standard SQL requires that you give an alias to the subquery.
So:
SELECT AVG(vals)
FROM(
SELECT SUM(a1) AS vals
FROM tableX
) t --> alias of the subquery
Side notes:
values is a language keyword, hence not a good choice for a column name; I renamed it to vals in the query
Your example is really contrived; the subquery always returns one row, so aggregating again in the outer query makes little sense: this is guaranteed to return the same value as that of the subquery. A more useful example would put a group by clause in the subquery, like so
SELECT AVG(vals)
FROM(
SELECT SUM(a1) AS vals
FROM tableX
GROUP BY id
) t

How can we use Multiple Queries using 'WITH' Clause

as sample code below....
WITH sampleA as (SELECT * FROM emp)
SELECT * FROM sampleA
SELECT * FROM sampleA
this alias 'sampleA' will work for only first query not for second or later..
But I want to query more with this alias only.
Can you please help me, how can I do that?
Common table Expression scope is limited to first SELECT statement. For multiple usage , use instead temporay table or table variable.

SQL alias select

I need to reorder my query to have the name in the first column, but I still need the rest of the columns populated as well.
How would you implement an alias in oracle to achieve this query?
select s.name, s.* from table as s
Thanks for the help.
There is no simple way around this, you must specify all columns in the order you would like them.
select s.name, s.column1, s.column2, ... from table as s

How to refer to a variable create in the course of executing a query in T-SQL, in the WHERE clause?

What the title says, really.
If I SELECT [statement] AS whatever, why can't I refer to the whatever column in the body of the WHERE clause? Is there some kind of a workaround? It's driving me crazy.
As far as I'm aware, you can't directly do this in SQL Server.
If you REALLY have to use your column alias in the WHERE clause, you can do this, but it seems like overkill to use a subquery just for the alias:
SELECT *
FROM
(
SELECT [YourColumn] AS YourAlias, etc...
FROM Whatever
) YourSubquery
WHERE YourAlias > 2
You're almost certainly better off just using the contents of the original column in your WHERE clause.
It has to do with the way a SELECT statement gets translated into an abstract query tree: the 'whatever' only appears in the query result projection part of the tree, which is above the filtering part of the tree, so the WHERE clause cannot understand the 'whatever'. This is not some internal implementation detail, it is a fundamental behavior of relational queries: the projection of the result occurs after the evaluation of the joins and filters.
IS really trivial to work around the 'problem' by making the hierarchy of the query explicit:
select ...
from (
select [something] as whatever
from ...
) as subquery
WHERE whatever = ...;
A common table expression can also server the same purpose:
with cte as (
select [something] as whatever
from ...)
select ... from cte
WHERE whatever = ...;
It's to do with the order of operations in the select statement. The WHERE clause is evaluated before the SELECT clause so this information isn't available. Although it is available in the ORDER BY clause as this is processed last.
As others have mentioned, a sub-query will get around this problem.