Ira SQL ProIra SQL Pro

SQL Column and Table Aliases

beginnerbasics
2 min read

What are Aliases?

Aliases give a temporary name to a column or table within a query. They make output more readable and are essential when writing JOINs or subqueries.

Syntax

Column alias:

SELECT column_name AS alias_name FROM table_name;

Table alias:

SELECT t.column_name FROM table_name AS t;

The AS keyword is optional but recommended for clarity.

When to Use

  • Renaming columns for a cleaner result set
  • Shortening long table names in multi-table queries
  • Naming computed expressions
  • Disambiguating columns with the same name from different tables

Key Points

  1. AS is OptionalSELECT name n FROM students works, but SELECT name AS n is clearer.
  2. Quoted Aliases — Use double quotes for aliases with spaces or special characters: AS "Student Name".
  3. Scope — Column aliases are visible in ORDER BY but not in WHERE or HAVING (in standard SQL).
  4. Table Aliases in JOINs — Table aliases prevent ambiguity: s.first_name vs c.name.
  5. Self Joins — Aliases are required when joining a table to itself.

Guided Practice

Solve the challenge below. Use hints when stuck and check your answer for instant feedback.

Practice challengeGuided learning mode

SQL Column and Table Aliases Challenge

Write a query that solve this task: rename the output column for clarity.

Expected result

Result columns are labeled student_name and contact_email.

Hidden checks

  • Returned rows and values
  • Output columns and result shape
  • Final database state after the query runs

Lesson guidance

What are Aliases?

Initializing database...Each run starts from fresh sample data.

More Examples

Table alias in a JOIN

Use short aliases when joining students and enrollments.

Initializing database...Each run starts from fresh sample data.

Frequently Asked Questions

Is the AS keyword required for aliases?
No, AS is optional. SELECT name n FROM students works, but including AS improves readability.
Can I use a column alias in the WHERE clause?
No. In PostgreSQL, column aliases defined in SELECT are not available in WHERE because WHERE is evaluated first.

Related Topics