SQL Aliases
An alias is a temporary name given to a column or a table in a SQL query. Aliases are created using the AS keyword. They only exist for the duration of that query — they do not change the actual column or table name in the database.
Think of an alias as a nickname. A person named "Subramaniam" might go by "Sam" in casual conversation — the real name doesn't change, but the nickname makes communication easier.
The Reference Table
| StudentID | StudentName | Age | City | Fees |
|---|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi | 45000.00 |
| 2 | Priya Mehta | 22 | Mumbai | 52000.00 |
| 3 | Arjun Nair | 19 | Chennai | 38000.00 |
| 4 | Sneha Kapoor | 21 | Pune | 47000.00 |
Column Aliases
Column aliases rename a column in the output of a query. This is useful when a column name is long, technical, or when using calculated columns that have no name by default.
Syntax
SELECT column_name AS alias_name
FROM table_name;Example 1: Renaming Columns in the Output
SELECT StudentName AS Name, City AS Location
FROM Students;Result:
| Name | Location |
|---|---|
| Ravi Sharma | Delhi |
| Priya Mehta | Mumbai |
| Arjun Nair | Chennai |
| Sneha Kapoor | Pune |
The column headers in the result show "Name" and "Location" instead of the original "StudentName" and "City".
Example 2: Alias for a Calculated Column
When a calculation is used in SELECT, the result has no column name. An alias gives it a meaningful label.
SELECT StudentName, Fees * 1.10 AS FeesAfterIncrease
FROM Students;Result:
| StudentName | FeesAfterIncrease |
|---|---|
| Ravi Sharma | 49500.00 |
| Priya Mehta | 57200.00 |
| Arjun Nair | 41800.00 |
| Sneha Kapoor | 51700.00 |
Example 3: Alias for Aggregate Functions
SELECT COUNT(*) AS TotalStudents,
AVG(Age) AS AverageAge,
MAX(Fees) AS HighestFee
FROM Students;Result:
| TotalStudents | AverageAge | HighestFee |
|---|---|---|
| 4 | 20.5 | 52000.00 |
Aliases With Spaces in the Name
If an alias contains spaces, it must be enclosed in double quotes (or backticks in MySQL).
SELECT StudentName AS "Full Name", City AS "Home City"
FROM Students;It is generally better practice to use underscores instead of spaces in alias names: Full_Name instead of "Full Name".
Table Aliases
Aliases can also be given to entire tables. This is particularly useful in queries that involve multiple tables (JOINs), where writing the full table name repeatedly would be tedious.
Syntax
SELECT alias.column_name
FROM table_name AS alias;Example: Table Alias
SELECT s.StudentName, s.City
FROM Students AS s;Here, s is the alias for the Students table. Instead of writing Students.StudentName, it is written as s.StudentName. The AS keyword is optional for table aliases — writing FROM Students s works the same way.
When the AS Keyword Can Be Omitted
The AS keyword is optional in many databases. Both of these are valid:
-- With AS
SELECT StudentName AS Name FROM Students;
-- Without AS (works in most databases)
SELECT StudentName Name FROM Students;However, including AS is recommended for clarity and readability.
Key Points to Remember
- Aliases are temporary — they only apply to the current query result.
- Aliases do not rename the actual column or table in the database.
- Column aliases improve the readability of output, especially for calculated or aggregated columns.
- Table aliases reduce repetition in queries that reference the same table multiple times.
- Use underscores instead of spaces in alias names to avoid needing quotes.
Summary
The AS keyword is used to create aliases — temporary names for columns or tables in a query. Column aliases improve the readability of query results, especially when working with calculated values or aggregate functions. Table aliases simplify queries by providing a shorter way to reference table names. Aliases are widely used in professional SQL and are considered a best practice for writing clean, readable queries.
