Tutorial :Can alias in SQL be used within the same table?


I have difficulty understanding alias. Can alias in SQL be used within the same table?


In a query, you can use multiple aliases for a single table:

SELECT alias1.Name, alias2.Name  FROM table as alias1   INNER JOIN table as alias2       ON alias1.ChildId = alias2.Id  

In the code above I am aliasing table as alias1 and alias2. It is the same table, with 2 different aliases.


Not sure I understand your question completely...

A good read on aliases @ http://www.w3schools.com/sql/sql_alias.asp http://www.sqltutorial.org/sqlalias.aspx

There are 2 kinds of aliases, one for tables and one for columns. Aliaes are used as a way to make your sql code more readable. It can give meaningful names to column and table names that might be long and/or confusing.


check the w3schools breif description and examples for SQL Alias

You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.


Which alias as you referring to: 'table alias' or 'column alias'?

In the SQL-92 Standard, the vernacular 'table alias' is referred to as a correlation name. A correlation name much be unique within its scope. The actual wording is as follows:

An identifier that is a correlation name is associated with a table within a particular scope. The scope of a correlation name is either a select statement: single row, subquery, or query specification. Scopes may be nested. In different scopes, the same correlation name may be associated with different tables or with the same table.

In the SQL-92 Standard, the vernacular 'column alias' is referred to (rather wordily) as an as clause that contains a column name. There is no general condition that the same column name shall not be specified more than once in column lists (but there are context-specific restrictions e.g. a view column list). In fact, SQL's allowance of duplicate column names is often cited as a fatal flaw as regards being turly relational.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »