sql
  1. sql-as-clause

SQL Clauses - AS Clause

The SQL AS clause is used to rename a table or column with an alias name. This is particularly useful when working with long or complex table or column names.

Syntax

The syntax for using AS clause to rename a table or column is as follows:

SELECT column_name AS alias_name
FROM table_name

Example

Suppose we have a table named "employees" with columns "first_name" and "last_name". We can use the AS clause to rename the columns to "First" and "Last", respectively, as follows:

SELECT first_name AS First, last_name AS Last
FROM employees

Output

The output of the above query will be a table with two columns named "First" and "Last".

Explanation

The AS clause can be used to assign an alias name to a column or table. This alias can then be referred to in subsequent SQL statements to reference the renamed column or table.

Use

The AS clause is used to create more readable and concise SQL statements. It is particularly helpful when dealing with tables or columns with long or complicated names. Additionally, it can be used to hide sensitive or confidential information by renaming columns with an alias.

Important Points

  • The AS clause can be used for both tables and columns.
  • The alias name assigned using the AS clause can be used in subsequent SQL statements.
  • The AS keyword is optional; the same effect can be achieved using only a space between the original name and the alias name.
  • The alias name cannot be used to reference the original table or column.

Summary

In summary, the AS clause is a simple yet powerful tool for renaming tables or columns in SQL statements. It makes SQL statements more readable and concise, and can also be used to hide sensitive information. When used correctly, it can greatly improve the efficiency and effectiveness of SQL statements.

Published on: