SQL - Aliases

SQL aliases are temporary names assigned to columns or tables within a query to enhance readability and simplify complex statements.

Column Aliases

Column aliases rename columns in the result set, making the output more descriptive:

SELECT first_name AS "First_Name", last_name AS "Last_Name"

FROM employees;

They are also useful for labelling calculated fields:

SELECT salary * 12 AS "Annual Salary"

FROM employees;

Table Aliases

Table aliases, particularly in joins, make queries simpler by abbreviating table names:

SELECT e.first_name, d.department_name

FROM employees AS e

JOIN departments AS dept ON e.department_id = dept.department_id;

Benefits:

Readability: Makes complex queries easier to understand.

Simplification: Reduces typing and avoids repeating long names.

Clarity: Provides meaningful names for derived columns or expressions.

Aliases are temporary and only valid for the duration of the query. They don’t affect the database schema but are essential for writing cleaner, more efficient SQL code.