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
- AS is Optional —
SELECT name n FROM studentsworks, butSELECT name AS nis clearer. - Quoted Aliases — Use double quotes for aliases with spaces or special characters:
AS "Student Name". - Scope — Column aliases are visible in
ORDER BYbut not inWHEREorHAVING(in standard SQL). - Table Aliases in JOINs — Table aliases prevent ambiguity:
s.first_namevsc.name. - 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.