- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL Alias
In SQL, an alias acts as a temporary "nickname" for a table or a column within a specific query. These names only exist for the duration of the query execution and do not change the actual names stored in your database schema.
Aliases are essential tools for developers because they transform cryptic column names into human-readable headers and prevent long, repetitive table names from making your code unmanageable, especially when dealing with complex joins or calculated fields.
Types of SQL Aliases
- Column Alias: Renames a column in the resulting output.
- Table Alias: Assigns a shorthand name to a table to simplify the query logic.
1. Column Alias
A column alias is primarily used to make the result set easier to read for end-users or application front-ends. While you can use them on standard columns, they are most useful when you are performing calculations or concatenations where the default column name might be something like COUNT(*) or expression_1.
Syntax:
SELECT column_name AS alias_name
FROM table_name;
Example:
SELECT
first_name AS "Employee Name",
salary AS "Monthly Salary",
salary * 12 AS "Annual Income"
FROM employees;
- In this example,
first_nameappears as "Employee Name" in the results. - We created a calculated column
salary * 12and gave it a descriptive alias "Annual Income".
WHERE clause. This is because SQL filters data (WHERE) before it processes the SELECT list. To filter by an aliased calculation, use the original expression or a Common Table Expression (CTE).
2. Table Alias
Table aliases are used to give a table a temporary shorthand. This is a lifesaver in large queries where you might reference the same table multiple times or when you have very long table names that make the query hard to scan.
Syntax:
SELECT column_name
FROM table_name AS alias_name;
Example:
SELECT e.first_name, e.salary
FROM employees AS e
WHERE e.salary > 3000;
- The
employeestable is assigned the aliase. Now, instead of typingemployees.first_name, we can simply usee.first_name.
u for users, o for orders, or oi for order_items. Avoid cryptic single letters like x or y if the query is complex.
Key Features of SQL Aliases
1. Column Alias Without AS
Technically, the AS keyword is optional in most SQL engines. You can simply put a space between the column name and the alias.
Example:
SELECT first_name "Employee Name", salary "Monthly Salary"
FROM employees;
AS works, it can lead to bugs. If you accidentally forget a comma between two column names, the second column name will be treated as an alias for the first one, and your query won't throw an error it will just return "wrong" data.
2. Table Alias Without AS
Similarly, you can alias tables without the AS keyword. This is very common in production code for the sake of brevity.
Example:
SELECT e.first_name, e.salary
FROM employees e
WHERE e.salary > 3000;
SQL Alias in Joins
Aliases are practically mandatory when working with JOINs. If two tables have columns with the same name (like id or created_at), using aliases tells the database exactly which table's column you want to retrieve.
Example:
SELECT
e.first_name,
e.salary,
d.department_name
FROM employees AS e
JOIN departments AS d ON e.department_id = d.department_id;
- By using
eandd, the query becomes much more compact and readable than if we wrote outemployees.department_idevery time.
SQL Alias in Aggregations
Aggregates like SUM, AVG, and COUNT don't have natural names in the output. Without an alias, the database might return a header like count(*) or ?column?. Using an alias provides context to the developer or the application reading the data.
Example:
SELECT
department_id,
COUNT(*) AS "Total Employees",
AVG(salary) AS "Average Salary"
FROM employees
GROUP BY department_id;
result['Total Employees'] rather than a system-generated index or name.
Summary
SQL aliases are powerful tools for writing clean, professional code. Column aliases clarify the intent of your data output, especially for calculated fields, while table aliases reduce redundancy and improve the clarity of complex JOIN operations. While the AS keyword is optional, using it is generally considered a best practice for readability. By mastering aliases, you'll write queries that are not only functional but also easy for other developers to understand.