SQL SELECT alias

In SQL, an alias is a temporary name given to a column or table in a query. Aliases are often used to make column names more readable or to provide a shorter name for a table. Aliases can also be used for calculations or to join columns from different tables.

To assign an alias to a column, you can use the AS keyword or simply provide the alias directly after the column name. Here’s an example:

SELECT column_name AS alias_name
FROM table_name;

Alternatively:

SELECT column_name alias_name
FROM table_name;

Here, column_name represents the actual name of the column in the table, and alias_name is the desired alias name for that column.

You can also assign aliases to tables in a similar way when performing joins or subqueries:

SELECT t1.column_name, t2.column_name
FROM table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.id;

In this example, table1 and table2 are assigned aliases t1 and t2 respectively.

Aliases can be useful when you need to perform calculations or when you have long column or table names that you want to make more concise. They can also make your SQL queries easier to read and understand.

#SQL #alias