HOME

TheInfoList



OR:

An alias is a feature of SQL that is supported by most, if not all, relational database management systems (RDBMSs). Aliases provide
database administrator Database administrators (DBAs) use specialized software to store and organize data. The role may include capacity planning, installation, configuration, database design, migration, performance monitoring, security, troubleshooting, as well as ba ...
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 can be used as an obfuscation technique to protect the real names of database fields. In SQL, you can alias tables and
columns A column or pillar in architecture and structural engineering is a structural element that transmits, through compression, the weight of the structure above to other structural elements below. In other words, a column is a compression membe ...
. A table alias is also called a correlation name. A programmer can use an alias to temporarily assign another name to a table or column for the duration of a 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 Salias_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: 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 set An SQL result set is a set of rows from a database, as well as metadata about the query such as the column names, and the types and sizes of each column. Depending on the database system, the number of rows in the result set may or may not be kno ...
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". 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 and Presto, 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 and UNNEST.https://prestodb.io/docs/0.248/sql/select.html#unnest SELECT — Presto 0.248 Documentation 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

{{SQL SQL Articles with example SQL code