SQL Column and Table Aliases: AS and Qualified Names

An SQL alias gives a column expression or table reference a temporary name within a query without renaming the stored database object.

Lesson 29 of 102Beginner10 min read

An SQL alias gives a column expression or table reference a temporary name within a query without renaming the stored database object.

Queries become easier to read when calculated columns have useful labels and repeated table references have short names. Aliases also let a query distinguish two roles played by the same table, such as a customer and that customer’s manager.

This reference adds detail to the main course. You can read it independently: the small database below supplies every table and row used in the examples. Start with the question each query answers, then compare the returned values with the data rather than memorising a line of syntax.

Set up the practice database

Run this setup once in a new practice database, before the examples. It uses four customers and six sales. Ana has two sales, Ben has one, Cara has two, and Dev has none.

Sale 106 has no customer link; sale 104 has an unknown amount. These deliberate test cases expose missing information and unmatched relationships.

SQL
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name VARCHAR(40) NOT NULL,
  city VARCHAR(40),
  manager_id INTEGER
);
CREATE TABLE sales (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER,
  amount DECIMAL(10,2),
  status VARCHAR(20) NOT NULL,
  created_on DATE NOT NULL
);
INSERT INTO customers (id,name,city,manager_id) VALUES
  (1,'Ana','Leeds',NULL),(2,'Ben','Leeds',1),
  (3,'Cara','Porto',1),(4,'Dev',NULL,2);
INSERT INTO sales (id,customer_id,amount,status,created_on) VALUES
  (101,1,10,'paid','2026-01-01'),
  (102,1,30,'pending','2026-01-02'),
  (103,2,20,'paid','2026-01-02'),
  (104,3,NULL,'pending','2026-02-01'),
  (105,3,20,'paid','2026-02-02'),
  (106,NULL,5,'paid','2026-02-03');

An amount of zero would be a known value. The NULL amount here means the value is unknown. A customer with no matching sale differs from a sale whose customer_id is NULL.

The primary keys identify individual records even when amounts or cities repeat. Dates use year-month-day format. The examples specify sorting whenever order matters; insertion order alone does not guarantee display order.

How SQL aliases works

A column alias labels the value produced by one selected expression. SELECT name AS customer_name returns the existing name values under a clearer heading. SELECT amount * 2 AS doubled_amount labels a calculation. Neither changes the stored column definition. Choose a name that states the meaning and, when relevant, the unit, such as revenue_usd or days_open. Avoid labels that imply a count when the expression actually calculates a monetary sum.

A table alias identifies a table reference inside the statement. FROM customers AS c lets you qualify a column as c.name. Qualification is essential when more than one input has an id or name column. A JOIN between customers and sales exposes two ids, so SELECT id can be ambiguous. c.id and s.id state exactly which one you want. Once a table reference has an alias, use that alias consistently for its columns rather than switching between the original name and the alias.

Where an alias can be used

Aliases follow scope rules. A SELECT-list alias is generally available to ORDER BY because ordering operates on the output values. It is generally unavailable in WHERE, which determines input rows before the SELECT expressions are established. Some engines permit aliases in additional clauses, but relying on those extensions makes a query harder to port. Put a calculation in a derived table or CTE when you want the next query level to filter it by its alias.

Use meaningful short aliases rather than random letters in a complicated query. In a self join, employee and manager are clearer roles than a and b. Aliases with spaces require identifier quoting, whose punctuation varies between databases, so customer_name is more portable than Customer Name. An alias can also hide an inconvenient source column name from a report, but it does not protect against schema changes: the original expression must still refer to a real source column. Keep output names stable if application code reads them by name.

MySQL, PostgreSQL, SQL Server and SQLite

AS for column and ordinary table aliases is supported by these four systems. Alias visibility in WHERE, GROUP BY and HAVING varies, so the portable habit is to filter source expressions or use an outer query. Identifier quoting differs; simple underscore-separated aliases avoid that dependency.

Check the engine-specific syntax and version requirements in PostgreSQL select-list aliases.

Practice task and worked solution

Return sale 102 with the customer name labelled buyer and the amount labelled total. Use c and s as table aliases.

Try the task before reading its worked solution. The supplied rows make the expected answer checkable by hand.

SQL
SELECT c.name AS buyer, s.amount AS total
FROM customers AS c
JOIN sales AS s ON s.customer_id = c.id
WHERE s.id = 102;
Output
buyer | total
Ana   | 30

Sale 102 has customer_id 1, so it joins to Ana. The query exposes two chosen labels and no ambiguous id column. The output names differ from those in the earlier example, while the underlying table definitions and saved values are unchanged.

Rewrite the example with SELECT c.id AS customer_id,s.id AS sale_id and verify that the two identifiers have different meanings. Next attempt to filter sale_amount in the same-level WHERE and check the engine’s behaviour. Replace that reference with s.amount, or move the named expression into a derived table before filtering it. The portable versions should select the same values.

SQL Column and Table Aliases: AS and Qualified Names Syntax

SQL aliases

SELECT c.name AS customer_name,
       s.amount AS sale_amount
FROM customers AS c
JOIN sales AS s ON s.customer_id = c.id
WHERE s.id = 103;

SQL Column and Table Aliases: AS and Qualified Names Summary

  • A column alias, expression AS label, names a value in the result without renaming the stored column.
  • A table alias, FROM customers AS c, gives a table a short name used to qualify columns such as c.id.
  • Aliases are essential when two joined tables share a column name or one table appears twice.
  • ORDER BY can use a select-list alias, but WHERE generally can't, because it runs earlier.
  • Choose aliases that describe meaning and units, because reports and APIs come to depend on them.

SQL Column and Table Aliases: AS and Qualified Names Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Labelled columns from a two-table join

The aliases c and s disambiguate the two tables. The output labels describe the fields a report receives. Sale 103 belongs to customer 2, whose name is Ben. The database columns remain customers.name and sales.amount after the query finishes.

SQL
SELECT c.name AS customer_name,
       s.amount AS sale_amount
FROM customers AS c
JOIN sales AS s ON s.customer_id = c.id
WHERE s.id = 103;
Output
customer_name | sale_amount
Ben           | 20

Common SQL Column and Table Aliases: AS and Qualified Names Mistakes

Filtering on a SELECT alias in WHERE

WHERE sale_amount > 10 in the same SELECT that defines amount AS sale_amount is not portable and fails in several engines. Write WHERE s.amount > 10, or place the first SELECT inside a derived table and filter its named output from the outer query.

SQL Column and Table Aliases: AS and Qualified Names in Real Projects

Aliases as an API contract

An API endpoint can return customer_name and sale_amount regardless of terse internal column names. These aliases become part of the response contract. Review a changed alias as carefully as a changed API field, because consumers can depend on the existing name even when the values stay the same.

Treat output aliases consumed by application code as stable field names. buyer and customer_name may carry the same value, but changing between them can break a client that reads a particular property. Use explicit aliases for overlapping key names so an export never has two columns ambiguously labelled id.

SQL Column and Table Aliases: AS and Qualified Names FAQ

Does AS rename a database column?

No. A column alias only labels the output of one query; the stored column keeps its name. Renaming schema objects requires a separate schema-changing command, such as ALTER TABLE customers RENAME COLUMN name TO full_name.

Can two tables use the same alias?

Not in the same query scope, because every reference such as c.id would be ambiguous and the database rejects the query. Give each role its own alias, for example buyer and seller when the same customers table is joined twice.

Can I omit AS?

Many databases let you write SELECT name customer_name or FROM customers c, and Oracle doesn't allow AS before table aliases. Writing AS for column aliases makes the intent clearer and reduces confusion between a missing comma and an intended alias.

How do I use an alias that contains spaces?

Quote it as an identifier: AS "Customer name" in PostgreSQL, SQLite and standard SQL, with backticks in MySQL or square brackets in SQL Server. Aliases without spaces, such as customer_name, avoid quoting altogether and are easier to use in application code.

Can I use a column alias in GROUP BY?

MySQL, PostgreSQL and SQLite accept output aliases in GROUP BY, but SQL Server doesn't, and the rules differ in edge cases. For portable queries, repeat the expression in GROUP BY, or compute it in a subquery or CTE and group by the named column outside.

SQL Column and Table Aliases: AS and Qualified Names Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choicemedium

What are the output labels for sale 103 in the alias example?

SQL Column and Table Aliases: AS and Qualified Names Quiz

2 questions to check what you learned.

  1. What does name AS customer_name change?

    • The stored column name for future queries
    • The saved text in every customer name
    • The output column label for this query
  2. Why use c.id and s.id in a join?

    • To identify the intended table when both have an id column
    • To make each table id refer to the same stored value
    • To require both tables to be ordered by id before joining