blob: b27be4fb5f81b565f9107350df2a592f2bd8b929 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="aliases">
<title>Overview of Impala Aliases</title>
<titlealts audience="PDF"><navtitle>Aliases</navtitle></titlealts>
<prolog>
<metadata>
<data name="Category" value="Impala"/>
<data name="Category" value="SQL"/>
<data name="Category" value="Data Analysts"/>
<data name="Category" value="Developers"/>
<data name="Category" value="Querying"/>
<data name="Category" value="Tables"/>
<data name="Category" value="Schemas"/>
</metadata>
</prolog>
<conbody>
<p>
When you write the names of tables, columns, or column expressions in a query, you can assign an alias at the
same time. Then you can specify the alias rather than the original name when making other references to the
table or column in the same statement. You typically specify aliases that are shorter, easier to remember, or
both than the original names. The aliases are printed in the query header, making them useful for
self-documenting output.
</p>
<p>
To set up an alias, add the <codeph>AS <varname>alias</varname></codeph> clause immediately after any table,
column, or expression name in the <codeph>SELECT</codeph> list or <codeph>FROM</codeph> list of a query. The
<codeph>AS</codeph> keyword is optional; you can also specify the alias immediately after the original name.
</p>
<codeblock>-- Make the column headers of the result set easier to understand.
SELECT c1 AS name, c2 AS address, c3 AS phone FROM table_with_terse_columns;
SELECT SUM(ss_xyz_dollars_net) AS total_sales FROM table_with_cryptic_columns;
-- The alias can be a quoted string for extra readability.
SELECT c1 AS "Employee ID", c2 AS "Date of hire" FROM t1;
-- The AS keyword is optional.
SELECT c1 "Employee ID", c2 "Date of hire" FROM t1;
-- The table aliases assigned in the FROM clause can be used both earlier
-- in the query (the SELECT list) and later (the WHERE clause).
SELECT one.name, two.address, three.phone
FROM census one, building_directory two, phonebook three
WHERE one.id = two.id and two.id = three.id;
-- The aliases c1 and c2 let the query handle columns with the same names from 2 joined tables.
-- The aliases t1 and t2 let the query abbreviate references to long or cryptically named tables.
SELECT t1.column_n AS c1, t2.column_n AS c2 FROM long_name_table AS t1, very_long_name_table2 AS t2
WHERE c1 = c2;
SELECT t1.column_n c1, t2.column_n c2 FROM table1 t1, table2 t2
WHERE c1 = c2;
</codeblock>
<p rev="IMPALA-6415 IMPALA-5191">
From Impala 3.0, the alias substitution logic has changed.
</p>
<p conref="../shared/impala_common.xml#common/column_aliases"/>
<p>
To use an alias name that matches one of the Impala reserved keywords (listed in
<xref href="impala_reserved_words.xml#reserved_words"/>), surround the identifier with either single or
double quotation marks, or <codeph>``</codeph> characters (backticks).
</p>
<p>
<ph conref="../shared/impala_common.xml#common/aliases_vs_identifiers"/>
</p>
<p conref="../shared/impala_common.xml#common/complex_types_blurb"/>
<p rev="2.3.0">
Queries involving the complex types (<codeph>ARRAY</codeph>,
<codeph>STRUCT</codeph>, and <codeph>MAP</codeph>), typically make
extensive use of table aliases. These queries involve join clauses
where the complex type column is treated as a joined table.
To construct two-part or three-part qualified names for the
complex column elements in the <codeph>FROM</codeph> list,
sometimes it is syntactically required to construct a table
alias for the complex column where it is referenced in the join clause.
See <xref href="impala_complex_types.xml#complex_types"/> for details and examples.
</p>
<p>
<b>Alternatives:</b>
</p>
<p conref="../shared/impala_common.xml#common/views_vs_identifiers"/>
</conbody>
</concept>