Open In App

How to Use Column Alias in SELECT Statement?

Last Updated : 06 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with SQL queries, readability and clarity are crucial for efficient data analysis. Using column aliases in the SELECT statement can significantly improve the clarity of our output by providing more meaningful and user-friendly names to the columns. Aliases are especially useful when working with complex queries, calculated fields, or when the original column names are not descriptive enough.

In this article, we will explain how to use column aliases in SQL, the syntax for implementing them, best practices, and practical examples to help us understand their importance and application. By the end of this article, we’ll be able to use column aliases effectively to make our SQL queries more readable and presentable.

What is a Column Alias in SQL?

A column alias is a temporary name assigned to a column or expression in the result set of an SQL query. It is used to make column names more descriptive or user-friendly. Column aliases do not change the actual column name in the database; they only appear in the output of the query.

For example, instead of displaying a column name like ENDNAME, we can use a column alias to display it as LASTNAME in the result set. The syntax for using column aliases in SQL is straightforward. We can use the AS keyword to assign an alias to a column or simply specify the alias without the keyword.

Syntax for Table Alias:

SELECT * FROM table_name AS alias_name;

Syntax for Column Alias:

SELECT column_name AS alias_name FROM table_name;

For this guide, we’ll use a sample table named demo_table in a database called geeks.

demo_table

demo_table

Example 1: Using a Simple Column Alias

In this example, perform a column alias on the ENDNAME column. We will use LASTNAME as the alias name. The column firstname is renamed as Name and age is renamed as Age. The aliases make the result set more readable by using simple, user-friendly names.

Query:

SELECT FIRSTNAME, ENDNAME AS LASTNAME, AGE, GENDER 
FROM demo_table;

Output

Example 2: Using Aliases with Special Characters or Spaces

When using special characters or spaces in an alias name, enclose the alias in double quotes. Here, firstname is displayed as “First Name” and lastname as “Last Name” in the output. FIRSTNAME was displayed as FIRST NAME, and ENDNAME was displayed as LAST NAME.

Query:

SELECT FIRSTNAME AS "FIRST NAME", ENDNAME AS "LAST NAME", AGE, GENDER 
FROM demo_table;

Output

Conclusion

Using column aliases in SQL is a simple yet powerful way to enhance the readability and presentation of our query results. By applying meaningful aliases to our columns, we can make our SQL output more user-friendly and easier to interpret. Always follow best practices, such as using the AS keyword and enclosing aliases with spaces in double quotes, to write efficient and clean SQL queries.



Next Article
Article Tags :

Similar Reads