SQL Aliases — Renaming Columns and Tables in Queries
Name Result Columns
A report should show student_name instead of name, and a join needs short, unambiguous table references.
An alias gives a table reference or result expression a name within a query. It does not rename the stored table or column.
Explain and apply
Give readable names to result columns and distinguish those names from stored column names.
Work it out
Display name under the result heading student_name, leaving the stored column unchanged.
Avoid mistakes
Alias visibility differs across clauses and engines.
Name Result Columns: the request and its result
School need
A report should show student_name instead of name, and a join needs short, unambiguous table references.
Database operation
An alias gives a table reference or result expression a name within a query. It does not rename the stored table or column.
Check the outcome
The returned headings change, but later queries must still refer to the stored name and marks columns unless they declare their own aliases.
Understand what the database does
AS introduces a result alias, while a table alias such as s lets later references identify that table instance. An alias is especially useful when joining tables that both contain roll_no. Output aliases are not generally available in WHERE because that clause does not evaluate the already projected result.
Related concepts: INNER JOIN and Choose Values with CASE.
What Is an Alias?
An alias is a temporary name assigned to a column or table in the result set. It only exists while the query is running. You can use it to simplify names or to make column headers look better in reports or dashboards.
Syntax for Column Alias
SQL · portable concepts; preserved examples identify their engine
SELECT column_name AS alias_name
FROM table_name;
Syntax for Table Alias
SQL · portable concepts; preserved examples identify their engine
SELECT t.column_name
FROM table_name AS t;
Sample Table – Students
SQL · portable concepts; preserved examples identify their engine
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Sneha Patil', '10A', 15, 'Pune');
1. Alias for a Column
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Sneha Patil', '10A', 15, 'Pune');
SQL · portable concepts; preserved examples identify their engine
Let’s display student names with a better column header:
SELECT name AS student_name, city AS hometown
FROM students;
student_name | hometown
--------------+----------
Aarav Sharma | Delhi
Diya Iyer | Chennai
Sneha Patil | Pune
2. Alias Without AS Keyword
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Sneha Patil', '10A', 15, 'Pune');
SQL · portable concepts; preserved examples identify their engine
The AS keyword is optional — you can write:
SELECT name student_name, city hometown
FROM students;
But using AS improves readability and is recommended.
3. Alias with Expressions
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Sneha Patil', '10A', 15, 'Pune');
SQL · portable concepts; preserved examples identify their engine
You can alias the result of a calculation:
SELECT name, age + 1 AS age_next_year
FROM students;
name | age_next_year
---------------+---------------
Aarav Sharma | 16
Diya Iyer | 15
Sneha Patil | 16
4. Alias for a Table
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Sneha Patil', '10A', 15, 'Pune');
SQL · portable concepts; preserved examples identify their engine
Useful when working with multiple tables or just to shorten queries:
SELECT s.name, s.city
FROM students AS s;
5. Table Alias in Joins
SQL · portable concepts; preserved examples identify their engine
Imagine you have another table with exam results:
CREATE TABLE results (
roll_no INT,
subject VARCHAR(30),
marks INT
);
INSERT INTO results VALUES
(1, 'Maths', 85),
(2, 'Maths', 90),
(3, 'Maths', 88);
Now join using aliases:
SELECT s.name, r.subject, r.marks
FROM students AS s
JOIN results AS r
ON s.roll_no = r.roll_no;
name | subject | marks
---------------+---------+-------
Aarav Sharma | Maths | 85
Diya Iyer | Maths | 90
Sneha Patil | Maths | 88
Best Practices
Query discussed in this section:
SELECT s.name, r.subject, r.marks
FROM students AS s
JOIN results AS r
ON s.roll_no = r.roll_no;
- Use meaningful alias names — avoid single letters unless in short joins.
- Prefer using AS for readability, especially in reports or shared queries.
- Use quotes around aliases if they contain spaces (e.g.,
AS "Student Name").
Important differences and mistakes
Alias visibility differs across clauses and engines. Avoid treating an alias as a permanent schema change, and keep table aliases descriptive enough to follow. Double-quoted identifier behavior varies with SQL mode; simple unquoted aliases are easiest here.
Try it with the school data
Display name under the result heading student_name, leaving the stored column unchanged.
This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
SQL · create only these practice tables in a disposable database:
CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');
Worked answer and reasoning
This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
SQL · create only these practice tables in a disposable database:
CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');
SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect
SELECT name AS student_name, marks AS exam_marks FROM practice_students ORDER BY roll_no;| student_name | exam_marks |
|---|---|
| Aarav | 85 |
| Diya | 70 |
| Sneha | 85 |
The returned headings change, but later queries must still refer to the stored name and marks columns unless they declare their own aliases.
Check your understanding
Use the idea independently
An alias gives a table reference or result expression a name within a query. It does not rename the stored table or column.
The returned headings change, but later queries must still refer to the stored name and marks columns unless they declare their own aliases.
Continue your understanding with INNER JOIN and Choose Values with CASE.
Source for the engine-specific behavior: MySQL 8.4 reference documentation.
Reaching the recap and completing the quizzes are separate milestones.