> For the complete documentation index, see [llms.txt](https://courses.parottasalna.com/database-engineering/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://courses.parottasalna.com/database-engineering/querying-data/column-aliases.md).

# Column Aliases

> 📌 Note: This tutorial uses the dvdrental database. If you dont have the dvdrental, please go through this <https://courses.parottasalna.com/database-engineering/sample-databases/dvd-rental-database>

### What are Column Aliases ?

A column alias allows you to assign a column or an expression in the select list of a `SELECT` statement a temporary name. The column alias exists temporarily during the execution of the query.

The following illustrates the syntax of using a column alias

```sql
SELECT column_name AS alias_name
FROM table_name;
```

In this syntax, the `column_name` is assigned an alias `alias_name`. The `AS` keyword is optional so you can omit it like this,

```sql
SELECT column_name alias_name
FROM table_name;
```

The main purpose of column aliases is to make the headings of the output of a query more meaningful.

### 1. Assigning a column alias to a column example <a href="#id-1-assigning-a-column-alias-to-a-column-example" id="id-1-assigning-a-column-alias-to-a-column-example"></a>

The following query returns the first names and last names of first 5 customers from the `customer` table

```sql
SELECT first_name, last_name FROM customer LIMIT 5;
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FBsnrIGEpmodBf0uP6b0R%2Fimage.png?alt=media&amp;token=1f0cdefb-2641-4b6b-811e-c5b06f6151bd" alt=""><figcaption></figcaption></figure>

If you want to rename the `last_name` heading, you can assign it a new name using a column alias like this

```sql
SELECT first_name, last_name AS surname FROM customer LIMIT 5;
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FQRZQ5fYRyafjSx4XOHR9%2Fimage.png?alt=media&amp;token=1f96bf22-8339-46ba-aca2-0706094c2c97" alt=""><figcaption></figcaption></figure>

This query assigned the `surname` as the alias of the `last_name` column

Or you can make it shorter by removing the `AS` keyword as follows,

```sql
SELECT first_name, last_name surname FROM customer LIMIT 5;
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FfPcxpqwsvNLt6BwOqykk%2Fimage.png?alt=media&amp;token=86c4942f-9465-4843-9a72-fb01eb623a8c" alt=""><figcaption></figcaption></figure>

### 2. Assigning a column alias to an expression example <a href="#id-2-assigning-a-column-alias-to-an-expression-example" id="id-2-assigning-a-column-alias-to-an-expression-example"></a>

The following query returns the full names of first 5 customers. It constructs the full name by concatenating the first name, space, and the last name

```sql
SELECT
   first_name || ' ' || last_name
FROM
   customer LIMIT 5;
```

Note that in PostgreSQL, you use the `||` as the concatenating operator that concatenates one or more strings into a single string.

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2F59vQpsZbfGiqlz9oN3WV%2Fimage.png?alt=media&amp;token=bde58bd0-05d5-49ae-b2b1-549f8d145323" alt=""><figcaption></figcaption></figure>

As you can see clearly from the output, the heading of the column is not meaningful `?column?` . To fix this, you can assign the expression `first_name || ' ' || last_name` a column alias e.g., `full_name`

```sql
SELECT
    first_name || ' ' || last_name AS full_name
FROM
    customer LIMIT 5;
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2Fy5lZ2ASMUfWD6r2HH48p%2Fimage.png?alt=media&amp;token=718908ae-a97c-40bc-b7b7-841858877b89" alt=""><figcaption></figcaption></figure>

### 3. Column aliases that contain spaces <a href="#id-3-column-aliases-that-contain-spaces" id="id-3-column-aliases-that-contain-spaces"></a>

If a column alias contains one or more spaces, you need to surround it with double quotes like this

```sql
column_name AS "column alias"
```

For example,

```sql
SELECT
    first_name || ' ' || last_name "full name"
FROM
    customer LIMIT 5;
```

<figure><img src="https://2781136461-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FK4KaCc7tzh0tRXwFV2A5%2Fuploads%2FGHWYUbaGeXi0mfHFsDzJ%2Fimage.png?alt=media&amp;token=aceb4f40-81aa-4de9-bb26-35a0e67b13ef" alt=""><figcaption></figcaption></figure>
