Questions tagged [alias]

Within a query, tables or subqueries may be aliased for readability and so that the same table can be referenced more than once in the query with different join conditions.

Tables and nested subqueries may be aliased within a SQL query, helping with readability, and allowing a table to be referenced more than once with different join conditions. An example of a query with sub-query and table aliases below.

select a.foo
      ,b.bar
      ,b.BarCount
  from (select b.bar
              ,count (*) as BarCount
          from BarTable b
          join OtherTable o
            on b.OtherTableID = o.OtherTableID
         group by b.bar) b
  join Foobar a
    on a.bar = b.bar

Table Foobar is aliased as a, and the nested subquery has an alias b. A nested subquery has a separate name-space for aliases as can be seen in the use of the alias b in both the parent and child queries to note different things. Note that this is possible, but not necessarily recommended as it can be confusing to people reading the code. It may also be desirable to use more meaningful aliases.

SQL statements may have an optional AS statement for aliasing, e.g.

select [...]
  from Foo       as a
  join Bar       as b
    on a.BarID = b.BarID

Aliases are necessary if a table is to be included more than once with different join conditions in the same query. A common example of this is a self-join such as the query below.

select par.BusinessUnitName    as ParentBusinessUnit
      ,chl.BusinessUnitName    as ChileBusinessUnit
  from BusinessUnit par
  join BusinessUnit chl
    on chl.ParentBusinessUnitID = par.BusinessUnitID

In this case the table BusinessUnit cannot be specified for both sides of the join without disambiguating which side a given expression is referring to. Aliases allow this to be specified.

84 questions
52
votes
4 answers

Using column alias in a WHERE clause doesn't work

Given a table users with two fields: id and email. select id, email as electronic_mail from ( select id, email from users ) t where electronic_mail = '' Postgres complains that: ERROR: column "electronic_mail" does not…
Victor
  • 657
  • 1
  • 7
  • 11
31
votes
2 answers

Subqueries' aliases same as main queries' aliases

I have an SQL query whose aliases are the same as some of its subquery's aliases. For example: select * from ROOM r where ... ( select * from ROAD r where ... ) This works fine, as the…
IcySnow
  • 517
  • 3
  • 6
  • 7
16
votes
3 answers

Why are queries parsed in such a way that disallows the use of column aliases in most clauses?

While trying to write a query, I found out (the hard way) that SQL Server parses WHEREs in a query long before parsing the SELECTs when executing a query. The MSDN docs say that the general logical parsing order is such that SELECT is parsed nearly…
Shauna
  • 413
  • 1
  • 5
  • 10
8
votes
3 answers

Is using SUM() twice suboptimal?

I know I have to write SUM twice, if I wish to use it in a HAVING clause (or use a derived table otherwise): SELECT id, sum(hours) AS totalhours FROM mytable GROUP BY id HAVING sum(hours) > 50; My question now is, whether or not this is…
8
votes
1 answer

How to create alias in SQL Server 2022

As per https://learn.microsoft.com/en-us/sql/relational-databases/native-client/sql-server-native-client?view=sql-server-ver16: The SQL Server Native Client (often abbreviated SNAC) has been removed from SQL Server 2022 (16.x) and SQL Server…
Tom Pažourek
  • 553
  • 7
  • 19
7
votes
1 answer

Besides attaching the debugger, is there any other differences between "(local)\Instance" and "MyLocalName\Instance"?

When using SQL Server Management Studio for SQL Server 2008 R2 connecting to a local SQL Server 2008 R2 instance (version 10.50.2500), I know that if I am using the instance name (local)\sql2008 it will fail to attach the debugger to any queries I…
Scott Chamberlain
  • 1,045
  • 1
  • 9
  • 25
7
votes
3 answers

Display column title as in the query without 'AS' statement

I query data from multiple tables using JOIN statements like this : SELECT u.id,u.name,d.id,d.name FROM user u RIGHT JOIN dog d ON... ; When I get the answer, in psql for example, I've got this kind of rows title : | id | name | id | name …
Francois
  • 179
  • 1
  • 3
7
votes
4 answers

Use column alias in GROUP BY

I have a query where I chain two columns. Why does the alias not work in the GROUP BY clause but in ORDER BY clause, it does work? How can I write the select correctly? SELECT KOS_VER_ID AS "Vertrag" , WHR_ISO_3_CODE AS "Waehrung" ,…
Konrad
  • 71
  • 1
  • 1
  • 2
7
votes
2 answers

Why do so many queries have aliases?

I've spent a lot of time searching for, researching and implementing various complex MySQL queries over the last several years. Many of them use lots of aliases for tables, even when none are necessary. Why is this? I've seen a little discussion…
Josh Hudnall
  • 173
  • 6
6
votes
2 answers

How to select all columns plus one alias column in MySQL

It is a simple question, but i could not find the answer. I want to select all the columns in my table with a SELECT * statement, but i also need to alias a UUID column this way: BIN_TO_UUID(ID) as ID. I need something like this: SELECT…
Michael Wallace
  • 253
  • 1
  • 2
  • 9
6
votes
1 answer

When are COLUMN aliases in FROM clauses needed?

FROM provides column_alias, the SQL spec calls these clauses. This is what the postgres docs say about them, A substitute name for the FROM item containing the alias. An alias is used for brevity or to eliminate ambiguity for…
Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
6
votes
1 answer

Column alias in Postgres FROM clause?

I'm reading through the Postgres documentation, the page on SELECT statements, and I ran across an aspect of aliases that I have never encountered. In the section on FROM clauses, subheading alias, there is a sentence stating: If an alias is…
Wildcard
  • 587
  • 8
  • 18
6
votes
3 answers

How and when do I use an alias in my SQL statement

I have a query in which I do some calculation and then I give that column an alias. In the next column I would like to use the result of that calculation in an IF statement. MySQL did not recognise the alias when I used it as a condition but instead…
Manny265
  • 325
  • 2
  • 7
  • 15
5
votes
3 answers

MySQL reuse select aliases

I currently have a query where I'm doing two subqueries to get X, Y data: SELECT t.series AS week, ( ... ) X, ( ..., AND ... ) Y, ROUND(( ... ) * 100) / ( ..., AND ... ), 2) Z FROM series_tmp t Y is kind of subset of X, since I apply just…
5
votes
3 answers

Getting SQL server to recognise a date column

I am trying to get a count of IDS on a orders table for yesterday (not hardcode) where the date in the table corresponds to when the order was placed. My table looks like this orders ( order_id INT PRIMARY KEY , user_id INT , date_created DATE ,…
RustyRyan
  • 89
  • 7
1
2 3 4 5 6