100 Days of SQL

sql

Day 16 – SQL Aliases

In SQL, an alias is a temporary name given to a table or column in a query. Aliases are commonly used to make column names more readable or to simplify complex queries.

To create an alias for a table or column in a query, you can use the AS keyword followed by the desired alias name. Here’s an example:


SELECT column1 AS alias1, column2 AS alias2 FROM table_name;

In this example, the SELECT statement returns two columns from a table called table_name, but the columns are given temporary aliases alias1 and alias2.

You can also use aliases to simplify complex queries that involve multiple tables. For example, if you want to join two tables called orders and customers, you can use aliases to refer to each table without having to type out the full table name every time. Here’s an example:


SELECT o.order_id, c.customer_name
FROM orders AS o
INNER JOIN customers AS c ON o.customer_id = c.customer_id;

In this example, the orders and customers tables are given temporary aliases o and c, respectively. This makes it easier to refer to each table in the SELECT statement and the JOIN clause.

Note that aliases are temporary and only apply to the current query. If you want to use the same alias in a different query, you’ll need to define it again.