SQL Aliases
In SQL, an alias is a temporary name assigned to a table or a column in a query. It allows you to rename a table or a column with a more meaningful or shorter name, which can make your SQL queries easier to read and understand.
For example, in a query like:
|
1 2 |
SELECT first_name AS fname, last_name AS lname FROM employees; |
Here, first_name and last_name are column names from the employees table. By using the AS keyword, we’re giving them aliases fname and lname respectively. This can be helpful when you have long or unclear column names, or when you want to join tables and need to differentiate columns with the same name.
You can also use aliases for tables when joining multiple tables in a query:
|
1 2 3 |
SELECT o.order_id, c.customer_name FROM orders AS o JOIN customers AS c ON o.customer_id = c.customer_id; |
Here, o and c are aliases for the orders and customers tables respectively. It makes the query more concise and readable
