Aliases in SQL

In this post, we’ll go over aliases in SQL Server. The following are some key points about Aliases.

  • In SQL Server, aliases can be used to create a temporary name for columns or tables.
  • Column aliases are used to make column headings easier to read in your result set.
  • Table aliases are used to shorten your SQL for easier reading or when performing joins.
  • An alias exists only for the duration of the query.
  • The AS keyword is used to create an alias, but it is not required in all SQL Products.
  • We frequently get No Column Name as the output column name when performing arithmetic expressions, concatenating columns, or calling system or built-in functions in the select statement. We can use Alias to provide a meaningful name for it.

Examples:
SELECT first_name + ‘ ‘ + last_name
FROM tbStudents
ORDER BY  first_name;

This returns the output by combining the first_name and last_name of the student from the table. However, the column name will be No Column Name. To avoid this, we can use the alias as shown below.

SELECT first_name + ‘ ‘ + last_name AS FullName
FROM tbStudents
ORDER BY  first_name;

The following is an example of how to shorten table names by assigning alias names to them. It means that the alias will be used to represent/refer to the actual table.

SELECT * FROM tbStudentsInformation AS S
JOIN tbStudentsPayments AS P ON S.iStudentID = P.iStudentID;

The following is an example to shorten the column names in the output.

SELECT
first_name + last_name AS StudentName,
MIN(PaymentDate) FirstPaidDate,
MAX(PaymentDate) RecentPaidDate
FROM tbStudentsInformation AS S
JOIN tbStudentsPayments AS P ON S.iStudentID = P.iStudentID
GROUP BY first_name + last_name;

Please go to the index page for the complete SQL Server tutorial.

Hope you find this article helpful.

Please subscribe for more interesting updates.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s