[elementor-template id="20878"]
[elementor-template id="21795"]

SQL Aliases – The Essential Guide to Enhancing Your SQL Code Readability and Efficiency

Written by Jagdeesh | 3 min read

Deep dive into SQL aliases, what they are, why you need them, and how to use them effectively.

You will deep dive into one of the most convenient yet underappreciated aspects of SQL – Aliases. If you’ve ever been overwhelmed by complicated SQL queries or wished for a simpler, cleaner approach to your SQL coding, this blog post is for you.

What are SQL Aliases?

SQL aliases are temporary names assigned to a table or column for the duration of a particular SQL query. They make SQL queries more readable, especially when dealing with complex joins and subqueries, and can save you a significant amount of typing in larger queries.

Why Use SQL Aliases?

There are three key reasons to use SQL aliases:

1) Readability: They make complex queries easier to read and understand.

2) Efficiency: They save time by reducing the amount of typing, especially with long or complicated table and column names.

3) Disambiguation: They help in cases where multiple tables have columns with the same name.

Alias Syntax

There are two primary contexts where aliases come into play: column aliases and table aliases.

1) Column Aliases

Column aliases allow you to rename a column in the result set of a query. Here is the general syntax

sql
SELECT column_name AS alias_name
FROM table_name;

Let’s use an example. Suppose you have a Employees table

output
| ID | Name  | Salary |
|----|-------|--------|
| 1  | Mike  | 5000   |
| 2  | Sarah | 7000   |
| 3  | John  | 6000   |

Here is how to assign an alias to the Name column

sql
SELECT Name AS EmployeeName
FROM Employees;

This query will return the following result set

output
EmployeeName
------------
Mike
Sarah
John

2) Table Aliases

Table aliases allow you to rename a table for the duration of a specific query. The syntax is as follows

sql
SELECT column_name(s)
FROM table_name AS alias_name;

To illustrate, let’s say you have another table, Departments

output
| DeptID | DeptName  |
|--------|-----------|
| 1      | Marketing |
| 2      | Sales     |
| 3      | HR        |

Let’s alias the Employees and Departments tables in a join

sql
SELECT E.Name, D.DeptName
FROM Employees AS E
JOIN Departments AS D
ON E.ID = D.DeptID;

Output:

output
Name    DeptName
----------------
Mike    Marketing
Sarah   Sales
John    HR

3) Aliases in Complex Query

This complex query is made more readable by using table aliases. The result will be the total sales amount for each department.

Input:

sql
SELECT D.DeptName, SUM(S.SalesAmount) AS TotalSales
FROM (
    SELECT E.ID, E.Name, SA.SalesAmount
    FROM Employees AS E
    JOIN Sales AS SA
    ON E.ID = SA.EmpID
) AS S
JOIN Departments AS D
ON S.ID = D.DeptID
GROUP BY D.DeptName;

Output:

output
DeptName    TotalSales
----------------------
Marketing   30000
Sales       50000
HR          40000

Conclusion

SQL aliases are a powerful tool in your SQL toolkit. They can make your code cleaner, easier to understand, and more efficient. So, next time you find yourself struggling with a complicated SQL query, remember – SQL aliases are here to help.

Free Course
Master Core Python β€” Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL β€” Hands-On
Join 5,000+ students at edu.darkorange-mallard-189514.hostingersite.com
Explore Course
Scroll to Top