An alias is a feature of SQL that is supported by most, if not all, relational database management systems (RDBMSs). Aliases provide users with the ability to reduce the amount of code required for a query, and to make queries simpler to understand. In addition, aliasing is required when doing self joins (i.e. joining a table with itself.)

In SQL, you can alias tables and columns. A table alias is called a correlation name, according to the SQL standard.[1] A programmer can use an alias to temporarily assign another name to a table or column for the duration of the current 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 SELECT * FROM table_name [AS] alias_name. 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:

Department Table
DepartmentID DepartmentName
31 Sales
33 Engineering
34 Clerical
35 Marketing

Using a table alias:

 SELECT D.DepartmentName FROM Department AS D

We can also write the same query like this (Note that the AS clause is omitted this time):

 SELECT D.DepartmentName FROM Department D

A column alias is similar:

 SELECT d.DepartmentId AS Id, d.DepartmentName AS Name FROM Department d

In the returned result sets, 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".

Also, if only one table is being selected and the query is not using table joins, it is permissible to omit the table name or table alias from the column name in the SELECT statement. Example as follows:

 SELECT DepartmentId AS Id, DepartmentName AS Name FROM Department d

Some systems, such as Postgres[2] and Presto,[3] support specifying column aliases together with table aliases. E.g.

 SELECT D.Id FROM Department AS D(Id)

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[4] and UNNEST.[5] As an example, one may conveniently test the above SQL statements without creating an actual Departments table by using expressions such as

WITH Department(DepartmentId, DepartmentName) AS (VALUES (1, 'HR'), (2, 'IT'))
SELECT DepartmentId AS Id, DepartmentName AS Name FROM Department d;

References

edit
  1. ^ ANSI Standard SQL – Foundation Document – Date: 2010-10-14
  2. ^ PostgreSQL: Documentation: 13: 7.2. Table Expressions
  3. ^ https://prestodb.io/docs/0.248/sql/select.html SELECT — Presto 0.248 Documentation
  4. ^ https://prestodb.io/docs/0.248/sql/values.html#examples VALUES — Presto 0.248 Documentation
  5. ^ https://prestodb.io/docs/0.248/sql/select.html#unnest SELECT — Presto 0.248 Documentation

📚 Artikel Terkait di Wikipedia

Transact-SQL

Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to the SQL (Structured Query Language) used to interact with relational databases

Alias

another object Alias (SQL), a feature of SQL Aliasing (computing), where a data location can be accessed through different symbolic names Alias Systems Corporation

PostgreSQL

renamed PostgreSQL to reflect its support for SQL. After a review in 2007, the development team decided to keep the name PostgreSQL and the alias Postgres.

SQLite

Although it is a lightweight embedded database, SQLite implements most of the SQL standard and the relational model, including transactions and ACID guarantees

Join (SQL)

A join clause in the Structured Query Language (SQL) combines columns from one or more tables into a new table. The operation corresponds to a join operation

Select (SQL)

or SQL expressions to be returned by the query. This is approximately the relational algebra projection operation. AS optionally provides an alias for

List of SQL reserved words

reserved. SQL SQL syntax List of relational database management systems Page listing all reserved words in the SQL standard, from SQL-92 to SQL:2016: Standard

Merge (SQL)

INTO works similarly. It also supports REPLACE INTO as an alias for compatibility with MySQL. Firebird supports MERGE INTO though fails to throw an error