đˇī¸ Aliases in SQL are temporary names assigned to columns or tables within a query. They improve the readability of query results, simplify complex SQL statements, and make long table or column names easier to reference. Aliases exist only during the execution of a query and do not permanently change the names of database objects.
đ What are Aliases?
An alias is an alternative name given to a column or table using the AS keyword or by simply placing the alias after the original name. Aliases are commonly used in reports, joins, calculations, and aggregate queries.
Information
đ Why Use Aliases?
- đ Make column headings more meaningful.
- đ Simplify long table names.
- đ Improve query readability.
- đ Make complex SQL queries easier to write and maintain.
- đ Provide names for calculated columns.
đ Sample Table
Assume the following Students table:
| StudentID | Name | Department | Marks |
|---|---|---|---|
| 101 | Alice | Computer Science | 92 |
| 102 | Bob | Mathematics | 85 |
| 103 | Charlie | Physics | 88 |
đˇī¸ Column Aliases
Column aliases provide temporary names for columns in the query result.
Column Alias Using AS
SELECT
StudentID AS ID,
Name AS Student_Name,
Department AS Course
FROM Students;The output displays the headings ID, Student_Name, and Course instead of the original column names.
Alias Without AS
In most SQL databases, the AS keyword is optional.
Alias Without AS
SELECT
StudentID ID,
Name Student_Name
FROM Students;Tip
đ§Ž Aliases for Calculated Columns
Aliases are commonly used to assign meaningful names to calculated values.
Calculated Column Alias
SELECT
Name,
Marks,
Marks + 5 AS BonusMarks
FROM Students;The calculated column appears in the result as BonusMarks.
đĸ Table Aliases
Table aliases create short names for tables, making queries shorter and easier to read. They are especially useful when joining multiple tables.
Table Alias
SELECT
s.StudentID,
s.Name,
s.Department
FROM Students AS s;Here, s is a temporary alias for the Students table.
đ Table Aliases with JOIN
Table aliases become particularly useful when multiple tables are used in the same query.
Using Aliases with JOIN
SELECT
s.Name,
d.DepartmentName
FROM Students AS s
JOIN Departments AS d
ON s.Department = d.DepartmentCode;Instead of repeatedly writing the full table names, the aliases s and d make the query shorter and easier to read.
đ Aliases with Aggregate Functions
Aggregate functions often return automatically generated column names that are difficult to read. Aliases solve this problem.
Aggregate Function Alias
SELECT
COUNT(*) AS TotalStudents,
AVG(Marks) AS AverageMarks,
MAX(Marks) AS HighestMarks
FROM Students;đ Aliases with Spaces
If an alias contains spaces, enclose it in the appropriate identifier quotes supported by your database system.
Alias with Spaces
SELECT
Name AS "Student Name",
Marks AS "Final Marks"
FROM Students;Important
đ Column Alias vs Table Alias
| Feature | Column Alias | Table Alias |
|---|---|---|
| Purpose | Renames a column in the result. | Provides a temporary name for a table. |
| Scope | Column output only. | Entire query. |
| Common Usage | Reports, calculations, aggregate functions. | Joins and complex queries. |
| Permanent Change | No. | No. |
đŧ Real-World Example
Suppose a university wants to generate a report showing student names, departments, and average scores using user-friendly column headings.
Student Report
SELECT
StudentID AS ID,
Name AS "Student Name",
Department AS Course,
Marks AS Score
FROM Students
ORDER BY Score DESC;The report displays meaningful headings that are easier for end users to understand than the original database column names.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ Aliases provide temporary names for columns and tables.
- đ They improve readability and simplify SQL queries.
- đ AS is optional in most SQL databases.
- đ Table aliases are especially useful in JOIN queries.
- đ Column aliases make reports and calculated values easier to understand.
- đ Aliases do not permanently rename database objects.