Skip to main content

Command Palette

Search for a command to run...

🌟 Understanding SQL Aliases: A Beginner-Friendly Guide with Examples

Updated
4 min readView as Markdown

.

🧑‍🏫 Introduction to SQL Aliases

When you run SQL queries, sometimes the column or table names are too long, not user-friendly, or unclear in the result set. That’s where aliases come in.

An alias in SQL is a temporary name that you give to a column or a table. It helps in:

  • Making your query results more readable

  • Renaming calculated columns

  • Shortening long table names, especially in JOINs

👉 Think of an alias as a nickname you give for a short time — just while the query is running.


🔹 Why Use Aliases?

SituationAlias Makes It Easier
Long or complex column namesRename for readability
Calculated valuesAssign meaningful names
Multiple tables in a joinShort aliases save time
Display reports or dashboardsClean, user-friendly output

🛠️ Let’s Build It Step-by-Step

We’ll use two simple tables:

  • Products: List of items

  • Orders: Sales made for those items


🧱 Step 1: Create the Products Table

CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(50),
    Price DECIMAL(10, 2)
);

➕ Step 2: Insert Sample Data

INSERT INTO Products (ProductID, ProductName, Price)
VALUES 
(1, 'Pen', 10.00),
(2, 'Pencil', 5.00),
(3, 'Eraser', 3.00);

Our Products table now looks like this:

ProductIDProductNamePrice
1Pen10.00
2Pencil5.00
3Eraser3.00

🏷️ Step 3: Column Aliases

Let’s say you want to show ProductName as just Name, and Price as Cost. You can do:

SELECT ProductName AS Name, Price AS Cost
FROM Products;

✅ Output:

NameCost
Pen10.00
Pencil5.00
Eraser3.00

💡 Tip: The AS keyword is optional, so you could write:

SELECT ProductName Name, Price Cost FROM Products;

But using AS makes your code clearer.


📊 Step 4: Alias for Calculated Columns

Let’s say you want to double the price and show it as a new column.

SELECT ProductName, Price * 2 AS DoublePrice
FROM Products;

✅ Output:

ProductNameDoublePrice
Pen20.00
Pencil10.00
Eraser6.00

📁 Step 5: Create a Second Table - Orders

Now let’s create a related table called Orders that references Products.

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    ProductID INT,
    Quantity INT,
    FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

➕ Insert Orders Data

sqlCopyEditINSERT INTO Orders (OrderID, ProductID, Quantity)
VALUES 
(101, 1, 2),  -- Pen
(102, 2, 5),  -- Pencil
(103, 3, 1);  -- Eraser

🔗 Step 6: Table Aliases with JOINs

Now suppose you want to display the order details along with product names. Using table aliases makes your query shorter and cleaner.

SELECT o.OrderID, p.ProductName, o.Quantity, p.Price AS UnitPrice
FROM Orders AS o
JOIN Products AS p ON o.ProductID = p.ProductID;

Here:

  • o is an alias for Orders

  • p is an alias for Products

  • p.Price AS UnitPrice gives the column a friendly name

✅ Output:

OrderIDProductNameQuantityUnitPrice
101Pen210.00
102Pencil55.00
103Eraser13.00

✅ Summary: What You Learned

FeatureDescriptionExample
Column AliasRename columns in outputPrice AS Cost
Table AliasRename table temporarilyProducts AS p
Calculated AliasAssign names to formulas/calculationsPrice * 2 AS DoublePrice
AS keywordOptional but improves claritySELECT Name AS "Customer Name"

More from this blog

Amit singh's blog

235 posts