AS
The AS
keyword creates aliases for columns, expressions, tables, or functions in a query. Aliases provide meaningful names for the query results and are essential for mapping query results to Java objects in the View response.
Syntax
Column or expression alias:
<column_or_expression> AS <alias>
Table alias:
<table_name> AS <alias>
Elements
column_or_expression
-
A column name, function, or expression that you want to alias.
table_name
-
The name of a table that you want to alias.
alias
-
A new name to assign to the column, expression, or table. This name will be used in the query result and should match field names in your Java response types.
Features
- Result mapping
-
Maps columns or expressions in the query result to fields in your Java response types, ensuring proper deserialization.
- Column renaming
-
Renames columns in the result set to match the required field names or to make them more meaningful.
- Structuring results
-
When used with composite expressions, enables creation of nested objects and structured data in the result.
- Table references
-
Creates shorter or more meaningful names for tables, especially useful in joins and complex queries.
Examples
Basic column aliases
SELECT name AS customerName FROM customers
SELECT address.city AS customerCity FROM customers
SELECT
id,
name AS customerName,
email AS contactEmail,
status
FROM customers
Aliases for special elements
SELECT * AS products FROM products
SELECT :requestId AS requestId, name FROM customers
SELECT next_page_token() AS nextPageToken FROM products
Aliases for nested structures
SELECT (name, email) AS contactInfo FROM customers
SELECT (
name AS fullName,
email AS emailAddress,
phone AS phoneNumber
) AS contactDetails
FROM customers
SELECT
id,
(name, email) AS contactInfo,
(address.street, address.city, address.zipCode) AS location
FROM customers
SELECT
category,
(
collect(name) AS productNames,
collect(price) AS pricePoints
) AS categoryData
FROM products
GROUP BY category
Table aliases
SELECT c.name, o.id
FROM customers AS c
JOIN orders AS o ON o.customerId = c.id
SELECT
c.name,
o.id AS orderId,
p.name AS productName
FROM customers AS c
JOIN orders AS o ON o.customerId = c.id
JOIN products AS p ON p.id = o.productId
Java type mapping
The alias names in your query must match the field names in your Java response types. For example:
SELECT
id,
name AS customerName,
(address.street, address.city) AS location
FROM customers
Should match a Java type like:
public record CustomerResponse(
String id,
String customerName,
Location location
) {}
public record Location(
String street,
String city
) {}
For collection results:
SELECT category, collect(name) AS productNames
FROM products
GROUP BY category
Maps to:
public record CategorySummary(
String category,
List<String> productNames
) {}
Alias requirements and constraints
Notes
-
Aliases defined in the SELECT clause can’t be referenced in WHERE clauses
-
Table aliases can be referenced in SELECT, WHERE, and ON clauses
-
Aliases are primarily for result mapping and don’t affect the underlying data
-
For complex nested structures, ensure that your Java classes have matching structure
-
The mapping between SQL aliases and Java fields is case-sensitive
Related features
-
SELECT clause - Uses aliases for result mapping
-
FROM clause - Can include table aliases
-
JOIN clause - Often uses table aliases
-
Result mapping - How query results map to Java types