Stop 02 · Basic SELECT QueriesTopics · 4/8

Column Aliases with AS

Part of the SQL Roadmap

In short

Column names in databases are often cryptic, like amt or cust_nm, and calculated columns get ugly default names. AS lets you give any output column a clear, human name.

AS gives a column or expression a readable name in the output, such as SELECT price * quantity AS order_value.

The jaadu presses handwritten paper labels onto a row of identical steel tiffin boxes on an office pantry counter.

Think of it like this

Labels on tiffin boxes

Four steel tiffins look the same in the office fridge. Stick a label on each, "Priya's rajma" or "Arjun's poha", and nobody grabs the wrong lunch. The food inside doesn't change. An alias is that sticker: it renames the column in the result, not in the table.

Watch it happen

Result headers before and after aliases

cust_nm
0
amt
1
?column?
2

1/5Without aliases, headers are raw names. PostgreSQL even names a calculation ?column?.

The big ideas

What you need to know

Renames output only

Aliases change the header in the result. The table's real column names stay the same.

Quote names with spaces

AS "Total Amount" needs double quotes in PostgreSQL. MySQL also accepts backticks. snake_case avoids quoting entirely.

ORDER BY can use it

ORDER BY gst_rs works because sorting happens after SELECT.

WHERE can't use it

WHERE runs before SELECT, so the alias doesn't exist yet. Repeat the expression instead.

See it in code

Walk through the code

sql
1SELECT2  cust_nm          AS customer_name,3  amt              AS amount_rs,4  amt * 0.18       AS gst_rs,5  amt * 1.18       AS "Total With GST"6FROM bills7WHERE amt * 0.18 > 1008ORDER BY gst_rs DESC;

1/4Cryptic column names get clear headers.

Aliases make the output readable. AS is optional (amt amount_rs also works), but writing it is clearer.

Save yourself the pain

Common mistakes

Oops

WHERE gst_rs > 100 using an alias from SELECT.

Do this instead

Repeat the expression: WHERE amt * 0.18 > 100. Or wrap the query in a subquery later on.

Oops

Writing AS 'Total' with single quotes in PostgreSQL.

Do this instead

Single quotes are for text values. Use "Total" or, better, total with no quotes.

Your mission

Make a report readable

~15 min

Quick check

Why does WHERE total_rs > 500 fail when total_rs is a SELECT alias?

Go deeper

  1. 1Alias every calculated column you write from now on.
  2. 2Learn when your database needs quotes around aliases with spaces.
  3. 3Try table aliases too, like FROM customers AS c.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge