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

.

### 🧑‍🏫 **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?

| Situation | Alias Makes It Easier |
| --- | --- |
| Long or complex column names | Rename for readability |
| Calculated values | Assign meaningful names |
| Multiple tables in a join | Short aliases save time |
| Display reports or dashboards | Clean, 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

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

---

## ➕ Step 2: Insert Sample Data

```sql
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:

| ProductID | ProductName | Price |
| --- | --- | --- |
| 1 | Pen | 10.00 |
| 2 | Pencil | 5.00 |
| 3 | Eraser | 3.00 |

---

## 🏷️ Step 3: Column Aliases

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

```sql
SELECT ProductName AS Name, Price AS Cost
FROM Products;
```

### ✅ Output:

| Name | Cost |
| --- | --- |
| Pen | 10.00 |
| Pencil | 5.00 |
| Eraser | 3.00 |

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

```sql
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.

```sql
SELECT ProductName, Price * 2 AS DoublePrice
FROM Products;
```

### ✅ Output:

| ProductName | DoublePrice |
| --- | --- |
| Pen | 20.00 |
| Pencil | 10.00 |
| Eraser | 6.00 |

---

## 📁 Step 5: Create a Second Table - Orders

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

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

---

### ➕ Insert Orders Data

```sql
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.

```sql
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:

| OrderID | ProductName | Quantity | UnitPrice |
| --- | --- | --- | --- |
| 101 | Pen | 2 | 10.00 |
| 102 | Pencil | 5 | 5.00 |
| 103 | Eraser | 1 | 3.00 |

---

## ✅ Summary: What You Learned

| Feature | Description | Example |
| --- | --- | --- |
| **Column Alias** | Rename columns in output | `Price AS Cost` |
| **Table Alias** | Rename table temporarily | `Products AS p` |
| **Calculated Alias** | Assign names to formulas/calculations | `Price * 2 AS DoublePrice` |
| **AS keyword** | Optional but improves clarity | `SELECT Name AS "Customer Name"` |
