Preloader

SQL Aliases

SQL
SQL Tutorials

SQL aliases give a temporary name to a column or a table. They make query results easier to read. They also help shorten long or complex names.

You use aliases only while running a query. They do not change the actual table or column name.

Why SQL Aliases Are Useful

Aliases help you
improve result readability
rename calculated columns
shorten long table names
write cleaner queries

Because of this, aliases appear often in real SQL queries.

Column Alias in SQL

A column alias gives a new name to a column in the result.

Basic Column SQL Alias Example

SELECT name AS student_name
FROM students;

This query shows the name column as student_name.

Column Alias Without AS

You can also write aliases without using AS.

SELECT marks total_marks
FROM students;

This works the same way and keeps the query short.

Alias with Expressions

Aliases are very useful with calculations.

SELECT name, marks + 5 AS updated_marks
FROM students;

This query adds five marks and shows the result with a clear name.

Alias with Aggregate Functions

SELECT city, AVG(marks) AS average_marks
FROM students
GROUP BY city;

Here, the alias makes the average column easier to understand.

Using Aliases in ORDER BY

You can use column aliases in the ORDER BY clause.

SELECT name, marks + 5 AS final_marks
FROM students
ORDER BY final_marks DESC;

This query sorts results using the alias.

Table Alias in SQL

A table alias gives a short name to a table. This is very helpful when working with joins.

Basic Table Alias Example

SELECT s.name, s.marks
FROM students AS s;

The alias s replaces the full table name.

Table Alias with JOIN

SELECT s.name, c.course_name
FROM students s
JOIN courses c ON s.id = c.student_id;

Here, table aliases keep the query clean and easy to follow.

Important Rules for SQL Aliases

  • Aliases exist only during query execution
  • You cannot use aliases in WHERE clause in most databases
  • Aliases work well in SELECT and ORDER BY
  • Use clear alias names for better readability

Common Mistake

-- This may not work
SELECT marks + 5 AS bonus
FROM students
WHERE bonus > 90;

Most databases do not allow aliases in WHERE. Instead, repeat the expression or use a subquery.

Summary

SQL aliases create temporary names.
They improve clarity and readability.
They help simplify complex queries.

Once you use aliases regularly, your SQL queries become much easier to understand.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *