DevLearningTools

LEARN · SQL

SQL Aliases

An alias gives a column or table a temporary name for one query. Column aliases were covered with AS earlier, this lesson adds table aliases, essential once joins are introduced.

An alias is a temporary name, scoped to one query, for a column or a table. Column aliases were already covered with AS back in the SELECT lesson, this lesson briefly recaps that and introduces table aliases, which don't matter much yet but become essential the moment a query involves more than one table.

Learning Objectives

  • Recap renaming a column's result with AS.
  • Alias a table, with and without the AS keyword.
  • Understand why table aliases matter once a query references more than one table.

Column Aliases: A Recap

Covered in full in the SELECT lesson: AS renames a column in the result only, without touching the actual table. The identifier-quoting differences for a multi-word alias (backticks, double quotes, or brackets depending on the database) were covered there too.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
SELECT salary AS annual_salary
FROM employees;

Table Aliases

A table alias gives a table a short temporary name, e for employees here, that can then prefix column names: e.first_name instead of employees.first_name. On a single-table query this just saves typing, its real value shows up once a query joins two or more tables, covered in the next module, where column names alone can become ambiguous between tables.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server
SELECT e.first_name, e.salary
FROM employees AS e;
Oracle
SELECT e.first_name, e.salary
FROM employees e;
NOTE

Oracle is the one database here that doesn't accept AS before a table alias, only FROM employees e works, not FROM employees AS e, that second form raises a syntax error. Every other database in this course accepts the table alias with or without AS. Column aliases don't have this restriction on any of the six, AS is always optional for those.

Try It Yourself

Write a query that selects every employee's first_name and salary, using e as a table alias for employees, written in a way that works on all six databases in this course.

Answer
SELECT e.first_name, e.salary
FROM employees e;

-- Omitting AS here works on every database, including Oracle.
-- Including AS also works, except on Oracle specifically.

Frequently Asked Questions

Is a table alias required for a single-table query?

No, it's entirely optional until a query references more than one table. Many developers still use short aliases out of habit, for consistency and brevity.

Can a table alias be used in the WHERE clause of the same query?

Yes, once a table alias is declared in FROM, it can be used anywhere else in that same query, SELECT, WHERE, ORDER BY, and more.

Common Beginner Mistakes

Writing FROM employees AS e on Oracle

Oracle rejects AS before a table alias specifically, it's a syntax error there, even though AS is accepted (and common) for column aliases on every database including Oracle. Omit AS for table aliases, FROM employees e, to keep a query portable across all six.

Using a confusing or overly short alias on a complex query

A single letter like e is fine for a short query with one or two tables. On a query with many tables, a slightly longer, more descriptive alias (emp, dept) is usually easier for the next reader to follow.

Best Practices

  • Omit AS for table aliases to keep a query portable across all six databases in this course, including Oracle.
  • Keep AS for column aliases, it's accepted everywhere and makes the alias's intent explicit to a reader.
  • Pick short but recognizable table aliases, especially once a query involves more than one or two tables.

Interview Questions

Why would FROM employees AS e fail on Oracle but work fine on MySQL?

Oracle specifically disallows the AS keyword before a table alias, a long-standing quirk of its SQL parser, while every other database in this course accepts it optionally. Column aliases don't have this restriction on any of the six, only table aliases on Oracle specifically.

Write a query, portable across all six databases in this course, that selects department and salary from employees using a table alias.

SELECT d.department, d.salary FROM employees d; — omitting AS makes it valid on every database in this course, including Oracle, where including AS would fail.

Summary

This lesson recapped column aliases with AS, introduced table aliases and why they matter once joins are involved, and covered Oracle's specific restriction: no AS before a table alias, even though AS is otherwise standard and portable for column aliases.

That completes Module 2, Operators and Filtering: combining conditions with AND/OR/NOT, pattern matching with LIKE and wildcards, matching a list with IN, matching a range with BETWEEN, and naming columns and tables with aliases.

What's Next?

Module 3, SQL Functions, is next: aggregate functions in depth, string functions, numeric functions, date functions, and the CASE expression.

</>

Ready to Practice SQL?

Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.