SQL Keywords

SQL ALIASES

What are SQL aliases?

SQL aliases give a temporary name to a column or table within a single query for readability and reuse.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL ALIASES: Supported in PostgreSQL, MySQL, SQL Server, Oracle, SQLite, Snowflake, Redshift, BigQuery, and all ANSI-compliant databases.

SQL ALIASES Full Explanation

SQL aliases let you assign an alternate identifier to a column or table that lasts only for the duration of the statement that creates it. They improve query readability, shorten long object names, and make complex joins and derived columns easier to reference. Aliases are defined with the AS keyword (optional in most dialects) immediately after the column or table expression.Column aliases- Exist only in the result set or for later clauses like ORDER BY, GROUP BY, or HAVING (dialect dependent).- Cannot be used in the same SELECT list expression that defines them.- Support quoted identifiers for spaces or reserved words.Table aliases- Replace the original table or subquery name for the rest of the statement.- Are required when the same table is joined more than once.- Accept optional column-list aliasing in some systems (e.g., PostgreSQL, Oracle).Aliases are parsed, not stored, so they never affect the underlying schema. They are part of the SQL-92 standard and are widely supported across relational databases.

SQL ALIASES Syntax

-- Column alias
SELECT column_expression AS alias_name
FROM table_name;

-- Table alias
SELECT t.column1, t.column2
FROM long_table_name AS t;

-- Alias without AS (allowed by most dialects)
SELECT column_expression alias_name
FROM table_name t;

SQL ALIASES Parameters

Example Queries Using SQL ALIASES

-- 1. Column alias for calculated field
SELECT SUM(total_amount) AS revenue_this_month
FROM orders
WHERE order_date >= DATE_TRUNC('month', CURRENT_DATE);

-- 2. Table alias in a self join
SELECT p1.id, p2.id AS referral_id
FROM people p1
JOIN people p2 ON p2.referrer_id = p1.id;

-- 3. Alias inside subquery
SELECT u.user_id, sq.order_count
FROM users u
LEFT JOIN (
  SELECT user_id, COUNT(*) AS order_count
  FROM orders
  GROUP BY user_id
) sq ON sq.user_id = u.user_id;

Expected Output Using SQL ALIASES

  • Each query returns a result set whose column headers match the defined aliases
  • The underlying tables remain unchanged

Use Cases with SQL ALIASES

  • Simplify long or cryptic table names in complex joins
  • Provide meaningful headers for aggregated or calculated columns
  • Disambiguate columns when joining a table to itself
  • Improve readability in subqueries and common table expressions

Common Mistakes with SQL ALIASES

  • Referencing a column alias in the WHERE clause instead of in HAVING or a nested subquery
  • Forgetting to quote aliases that contain spaces or start with a digit
  • Reusing the same alias for different tables in the same query
  • Assuming the alias persists outside the query or into another session

Related Topics

AS keyword, SELECT, JOIN, CTE, VIEW, WITH clause, GROUP BY, HAVING

First Introduced In

SQL-92 standard

Frequently Asked Questions

What is the difference between column and table aliases?

Column aliases rename the output column in the result set, while table aliases rename the source table or subquery for easier reference inside the query.

Is the AS keyword mandatory?

In most databases, AS is optional. Writing `SELECT col alias` works, but `SELECT col AS alias` is clearer and avoids accidental syntax errors.

Why does my column alias not work in WHERE?

SQL evaluates the WHERE clause before the SELECT list, so the alias does not yet exist. Move the condition to HAVING or reference the original expression.

Can I reuse the same table alias in a subquery?

Yes, each query block has its own namespace. An alias in an inner subquery does not conflict with one in the outer query.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!