Alias (SQL): Difference between revisions

Content deleted Content added
Yobot (talk | contribs)
m top: WP:CHECKWIKI error fixes using AWB (11904)
hyperlink
 
(19 intermediate revisions by 13 users not shown)
Line 1:
{{Short description|Feature of SQL}}
{{refimprove|date=October 2013}}
An '''alias''' is a feature of [[SQL]] that is supported by most, if not all, [[RDBMS|relational database management systems (RDBMSs)]]. Aliases provide [[database administrator]]s, as well as other database users, with the ability to reduce the amount of code required for a query, and to make queries simpler to understand. In addition, aliasing canis berequired usedwhen asdoing anself [[Obfuscationjoins (software)|obfuscationi.e. technique]]joining toa protecttable the real names of databasewith fieldsitself.)
 
In SQL, you can alias tables and [[Column (database)|columns]]. A table alias is also called a '''correlation name''', according to the SQL standard.<ref>ANSI Standard SQL - Foundation Document - Date: 2010-10-14</ref> A programmer can use an alias to temporarily assign another name to a table or column for the duration of athe current [[Select (SQL)|SELECT query]]. Assigning an alias does not actually rename the column or table. This is often useful when either tables or their columns have very long or complex names. An alias name could be anything, but usually it is kept short. For example, it might be common to use a table alias such as "pi" for a table named "price_information".
 
The general syntax of an alias is <codesyntaxhighlight lang="sql" inline>SELECT * FROM table_name [AS] alias_name</codesyntaxhighlight>. Note that the AS keyword is completely optional and is usually kept for readability purposes. Here is some sample data that the queries below will be referencing:
 
{| class="wikitable" style="text-align:center; float:left; margin-left:5px"
Line 22 ⟶ 23:
Using a table alias:
 
<sourcesyntaxhighlight lang="sql">
SELECT D.DepartmentName FROM Department AS D
</syntaxhighlight>
</source>
 
We can also write the same query like this (Note that the AS clause is omitted this time):
 
<sourcesyntaxhighlight lang="sql">
SELECT D.DepartmentName FROM Department D
</syntaxhighlight>
</source>
 
A column alias is similar:
 
<sourcesyntaxhighlight lang="sql">
SELECT d.DepartmentId AS Id, d.DepartmentName AS Name FROM Department d
</syntaxhighlight>
</source>
 
In the returned [[result set]]s, the data shown above would be returned, with the only exception being "DepartmentID" would show up as "Id", and "DepartmentName" would show up as "Name".
Line 42 ⟶ 43:
Also, if only one table is being selected and the query is not using [[Join (SQL)|table joins]], it is permissible to omit the table name or table alias from the column name in the SELECT statement. Example as follows:
 
<sourcesyntaxhighlight lang="sql">
SELECT DepartmentId AS Id, DepartmentName AS Name FROM Department d
</syntaxhighlight>
</source>
 
Some systems, such as Postgres<ref>[https://www.postgresql.org/docs/13/queries-table-expressions.html#QUERIES-FROM PostgreSQL: Documentation: 13: 7.2. Table Expressions]</ref> and Presto,<ref>https://prestodb.io/docs/0.248/sql/select.html SELECT — Presto 0.248 Documentation</ref> support specifying column aliases together with table aliases. E.g.
{{SQL}}
 
<syntaxhighlight lang="sql">
SELECT D.Id FROM Department AS D(Id)
</syntaxhighlight>
 
would produce the same result set as before. In this syntax it is permissible to omit aliases for some column names. In the example, an alias was provided for DepartmentId, but omitted for DepartmentName. Columns with unspecified aliases will be left unaliased. This syntax is often used with expressions that do not produce useful table and column names, such as VALUES<ref>https://prestodb.io/docs/0.248/sql/values.html#examples VALUES — Presto 0.248 Documentation</ref> and UNNEST.<ref>https://prestodb.io/docs/0.248/sql/select.html#unnest SELECT — Presto 0.248 Documentation</ref> As an example, one may conveniently test the above SQL statements without creating an actual Departments table by using expressions such as
 
<syntaxhighlight lang="sql">
WITH Department(DepartmentId, DepartmentName) AS (VALUES (1, 'HR'), (2, 'IT'))
SELECT DepartmentId AS Id, DepartmentName AS Name FROM Department d;
</syntaxhighlight>
 
==References==
{{Reflist}}
 
{{SQL}}
 
[[Category:SQL]]
[[Category:Articles with example SQL code]]