Yandex

SQL Aliases
Renaming Columns and Tables in Queries



Introduction

Sometimes column or table names in your database aren't exactly presentation-ready — long, technical, or just unclear. This is where SQL ALIAS comes in. It lets you rename columns or tables temporarily to make your output more readable and meaningful.

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

SELECT column_name AS alias_name
FROM table_name;

Syntax for Table Alias

SELECT t.column_name
FROM table_name AS t;

Sample Table – Students

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

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

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

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

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

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

  • 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").

Summary

SQL aliases help you present your data clearly and professionally. Whether you're simplifying column names, formatting output for a report, or shortening complex queries — AS makes your SQL more expressive and human-readable.

What’s Next?

With aliases mastered, you're ready to dive into SQL Subqueries — a way to use one query inside another for advanced filtering and logic.

QUIZ

Question 1:What is the primary purpose of using an alias in SQL?

Question 2:Aliases defined using AS are permanent changes to the table structure.

Question 3:Which of the following are correct uses of SQL column aliases?

Question 4:What does this query do?
SELECT name AS student_name, marks AS score FROM students;

Question 5:Aliases can also be used for table names in SQL joins or subqueries.

Question 6:In which scenarios are table aliases especially helpful?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M