VMware ESXi and vSphere Cluster Management
SQL Aliases: Rename Columns, Expressions, and Tables
Learn how SQL aliases rename result-set headings, label calculated expressions, and shorten table references using clear, portable query patterns.
An alias is a temporary alternative name assigned inside a SQL query. You can give a selected column, calculated expression, or table a clearer name without changing the database itself.
A query returns a result set: the rows and columns produced by the statement. A column alias changes the label shown for a result-set column. It does not rename the physical column, change the table definition, or modify stored data.
Why use SQL aliases?
- Create reader-friendly report headings.
- Give calculated fields meaningful names instead of unclear database-generated headings.
- Use shorter names for tables, especially in joins and self-joins.
- Make qualified column references easier to read.
Basic column alias syntax
The usual form places the AS keyword after the selected column or expression and before the alias:
SELECT column_name AS alias_name
FROM table_name;
For example, this query changes the output heading from amount to customer_amount:
SELECT amount AS customer_amount
FROM Customers;
The source column is still called amount. Only the heading in this query's result set is called customer_amount.
Aliasing customer-name columns
Suppose a Customers table contains columns named fName and lName. Database-oriented names may be useful to developers, but report readers may prefer First Name and Last Name.
SELECT
fName AS "First Name",
lName AS "Last Name"
FROM Customers;
The values are not changed. If fName contains Amina, the result still contains Amina; its heading is simply First Name.
Sample source data
| No | lName | fName | CITY | COUNTRY | AMOUNT |
|---|---|---|---|---|---|
| 1 | Patel | Amina | London | UK | 1800.00 |
| 2 | Garcia | Mateo | Madrid | Spain | 2250.00 |
| 3 | Chen | Lin | Toronto | Canada | NULL |
Column alias result
| First Name | Last Name |
|---|---|
| Amina | Patel |
| Mateo | Garcia |
| Lin | Chen |
The NULL amount remains NULL when selected under an alias. An alias renames a heading; it does not replace missing values.
Single-word and multi-word aliases
A simple alias such as first_name is usually an ordinary identifier and needs no delimiters:
SELECT fName AS first_name,
lName AS last_name
FROM Customers;
An alias containing spaces, punctuation, or other special characters needs an identifier delimiter. Identifier delimiters are database-specific quoting characters used to tell the database that text is an identifier rather than separate SQL tokens.
| Database family | Preferred identifier delimiter | Example alias with spaces | Notes |
|---|---|---|---|
| Standard SQL and PostgreSQL-style systems | Double quotes | AS "First Name" | Double quotes delimit an identifier. |
| SQL Server | Square brackets, or double quotes when quoted identifiers are enabled | AS [First Name] | Square brackets are commonly used in SQL Server queries. |
| MySQL | Backticks | AS `First Name` | Double-quote behavior can depend on SQL mode. |
Use the delimiter required by your database product. For maximum portability, prefer a simple alias such as first_name instead of a label containing spaces.
Aliases for calculated expressions
An expression is a calculated or constructed value, such as arithmetic, a function call, string concatenation, a conditional expression, an aggregate, or a formatted value. Expressions should usually have aliases because their automatic headings can be long or unclear.
Arithmetic expressions
SELECT
fName,
amount * 1.10 AS adjusted_amount
FROM Customers;
Here, adjusted_amount labels the calculated value. The expression adds ten percent for the result only; it does not update the stored amount column.
A display-style label can also be used when the database's identifier syntax supports it:
SELECT amount * 1.10 AS "Adjusted Amount"
FROM Customers;
Concatenated names
Concatenation syntax differs between database products. Some systems use ||, while others commonly use CONCAT. The alias syntax remains the same:
SELECT CONCAT(fName, ' ', lName) AS full_name
FROM Customers;
Conditional values
SELECT
fName,
CASE
WHEN amount IS NULL THEN 'No amount'
WHEN amount >= 2000 THEN 'High value'
ELSE 'Standard value'
END AS customer_category
FROM Customers;
Aggregate results
SELECT
COUNTRY AS country_name,
COUNT(*) AS customer_count,
SUM(amount) AS total_amount
FROM Customers
GROUP BY COUNTRY;
Aliases make report columns such as customer_count and total_amount understandable without inspecting the expressions that produced them.
Formatted values
SELECT
amount,
ROUND(amount, 2) AS rounded_amount
FROM Customers;
Functions for formatting dates, numbers, or text vary by database, but the selected expression can be followed by AS alias_name in the same way.
Is the AS keyword optional?
Many database products allow this shorter form:
SELECT amount customer_amount
FROM Customers;
It is equivalent to:
SELECT amount AS customer_amount
FROM Customers;
Use AS where supported because it clearly separates the expression from its alias. This is especially helpful when a query contains several expressions.
Column-alias support and table-alias support are separate dialect issues. Many systems accept FROM Customers AS c, but some database products or compatibility modes require FROM Customers c and do not accept AS for table aliases. Check the syntax for the database you are using.
Table aliases
A table alias is a short temporary name assigned to a table within a query. It is commonly used to create a qualified column name, which is a column reference prefixed by a table name or table alias.
SELECT
c.fName AS first_name,
c.lName AS last_name
FROM Customers AS c;
In this example, c is the table alias and c.fName is a qualified column name. After the alias is declared, use c consistently for qualifying columns in that query scope.
Why table aliases matter in joins
Joins often combine tables that contain columns with the same name, such as customer_id. Table aliases identify which table supplies each column:
SELECT
c.customer_id,
c.fName,
o.order_id
FROM Customers AS c
JOIN Orders AS o
ON o.customer_id = c.customer_id;
Aliases also make long table names shorter. In a self-join, where the same table appears twice, each occurrence needs a different alias:
SELECT
employee.employee_id,
employee.manager_id,
manager.employee_id AS manager_record_id
FROM Employees AS employee
JOIN Employees AS manager
ON manager.employee_id = employee.manager_id;
Column aliases versus table aliases
| Alias type | Example | What it names | Typical use |
|---|---|---|---|
| Column alias | amount AS adjusted_amount | A selected column or expression | Result headings and later ordering |
| Table alias | Customers AS c | A table reference | Qualified columns, joins, and self-joins |
Alias scope and query-clause behavior
Alias scope means the part of a query in which an alias can be referenced. A column alias is reliably available as a result-set label and is commonly available to ORDER BY:
SELECT
fName,
amount * 1.10 AS adjusted_amount
FROM Customers
ORDER BY adjusted_amount DESC;
Aliases in WHERE, GROUP BY, and HAVING are less portable. SQL is logically processed in stages, and filtering generally occurs before the SELECT list creates its output aliases. Some database products provide extensions, but others reject the alias.
For example, this may fail:
SELECT
amount * 1.10 AS adjusted_amount
FROM Customers
WHERE adjusted_amount > 2000;
The portable solutions are to repeat the expression or create a query level in which the alias already exists.
Repeat the expression
SELECT
amount * 1.10 AS adjusted_amount
FROM Customers
WHERE amount * 1.10 > 2000;
Use a derived table
A derived table is a subquery in the FROM clause. The inner query creates named output columns, and the outer query can use those names:
SELECT
first_name,
adjusted_amount
FROM (
SELECT
fName AS first_name,
amount * 1.10 AS adjusted_amount
FROM Customers
) AS customer_amounts
WHERE adjusted_amount > 2000;
The same idea can be implemented with a common table expression when your database supports CTEs:
WITH customer_amounts AS (
SELECT
fName AS first_name,
amount * 1.10 AS adjusted_amount
FROM Customers
)
SELECT first_name, adjusted_amount
FROM customer_amounts
WHERE adjusted_amount > 2000;
Table aliases generally replace the original table name for qualification after declaration. If you declare Customers AS c, write c.fName rather than mixing the alias with the original table name.
Aliases versus schema changes
A query-time alias is temporary. It lasts only for the query statement or the result consumed by an application. It does not:
- Rename a physical column.
- Rename a table in the database schema.
- Alter a table definition.
- Change stored rows or values.
A permanent rename is a database-definition operation, commonly performed with a product-specific ALTER TABLE command or another schema-management command. Use a permanent rename only when the database design itself should change. Use an alias when you only need a clearer name in one query or report.
Readable alias naming conventions
- Choose meaningful, concise names such as
adjusted_amountorcustomer_count. - Use a consistent style, such as lowercase snake case for technical output names.
- Use display labels such as
"Adjusted Amount"only when a human-facing heading requires spaces. - Avoid ambiguous names such as
valuewhen the expression has a more precise meaning. - Avoid reserved words. A reserved word is a word with special meaning in SQL, such as a keyword that controls query syntax.
- Do not depend on unusual quoting if the query must run on multiple database products.
- Use table aliases consistently throughout joins and qualified references.
Common alias problems
A multi-word alias causes a syntax error
The alias contains spaces but was not delimited for the active SQL dialect. Use the correct identifier delimiter, or choose first_name instead of First Name.
Quotes make an alias behave like text
Single quotes normally identify string literals, not aliases. Replace them with the database's identifier delimiter, or use a simple unquoted alias.
A SELECT alias is rejected in WHERE
The database may evaluate WHERE before the SELECT alias is established. Repeat the expression, use a derived table, or use a CTE and filter at the outer query level.
A joined column is ambiguous
More than one table exposes the same column name. Assign table aliases and qualify the reference, for example c.customer_id.
The query fails after introducing a table alias
The query may still use the original table name in a qualified reference. Once the alias is assigned, use the declared alias consistently where the dialect requires it.
An alias conflicts with SQL syntax
The chosen name may be a reserved word or contain special characters. Pick a non-reserved name, or delimit the alias using the database-specific identifier syntax.
Exam-relevant notes
SELECT expression AS alias_nameassigns a column alias.- A column alias changes result-set metadata, not the schema or stored data.
ASis commonly optional for aliases, but it improves clarity.- Table aliases shorten and qualify references, especially in joins and self-joins.
ORDER BYcommonly accepts a SELECT-list alias.- Do not assume a SELECT-list alias works in
WHERE,GROUP BY, orHAVINGacross all SQL dialects. - Use a derived table or CTE when another query level must filter or process a named expression.
Practice checklist
- Rename
fNameandlNamewith readable output headings. - Give
amount * 1.10the aliasadjusted_amount. - Assign
Customersthe table aliascand selectc.fName. - Sort by
adjusted_amountin descending order. - Filter an aliased calculated value through a derived table rather than relying on a WHERE alias extension.