TutorialsSQLSQL Aliases
🟢 Free Demo
SQL TutorialTopic 14 of 20

SQL Aliases

Give temporary names to columns and tables

✅ What You Will Learn

How to alias columns with AS for readable output
How to alias tables to shorten query code
When aliases are required vs optional
How aliases interact with ORDER BY and GROUP BY

An alias is a temporary name you assign to a column or table in a query. Aliases make your output more readable and your query easier to write — especially when dealing with long table names or calculated columns.

Aliases only exist for the duration of the query. They do not change the actual column or table name in the database.

📋 The orders table — aliases rename columns in the result

order_idcustomer_nameproductamount
1001Rahul SharmaLaptop45000
1002Priya VermaMobile Phone18000
1003Amit KumarHeadphones3500
1004Sneha KapoorLaptop52000
1005Vikram SinghTablet28000

Syntax

SQL SYNTAX
-- Column alias
SELECT column_name AS alias_name

-- Table alias
FROM table_name alias

Examples

Example 1Column aliases for calculated fields
SELECT customer_name AS "Customer",
       amount AS "Order Value (₹)",
       amount * 0.18 AS "GST (18%)",
       amount * 1.18 AS "Total Payable"
FROM orders;
OUTPUT
Customer     | Order Value (₹) | GST (18%) | Total Payable
-------------|-----------------|-----------|-------------
Rahul Sharma | 45000           | 8100.00   | 53100.00
💡

Use double quotes for aliases with spaces or special characters. Single-word aliases can skip the quotes.

Example 2Table aliases — essential for JOINs
-- Without alias (hard to read)
SELECT customers.customer_name, orders.product, orders.amount
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

-- With alias (clean)
SELECT c.customer_name, o.product, o.amount
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id;
💡

Table aliases make JOIN queries far more readable. Use short, meaningful aliases — c for customers, o for orders, p for products.

Example 3Alias used in ORDER BY
SELECT product,
       SUM(amount) AS total_revenue
FROM orders
GROUP BY product
ORDER BY total_revenue DESC;
💡

You can reference a column alias in ORDER BY. However, you cannot use a column alias in WHERE or HAVING — use the full expression there instead.

📌 Key Points to Remember

  • AS keyword creates an alias — it is optional but recommended for readability
  • Table aliases are especially important in JOINs
  • Column aliases can be used in ORDER BY but not in WHERE or HAVING
  • Aliases with spaces need double quotes: AS "Total Revenue"
  • Once you define a table alias, you must use it throughout the query

🏢 Real-World Application

Aliases make SQL queries readable and professional. Without table aliases, a JOIN query becomes verbose: SELECT orders.customer_name, orders.amount, customers.city FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id. With aliases: SELECT o.customer_name, o.amount, c.city FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id. In data analyst interviews, clean SQL with aliases is expected — messy queries without them suggest inexperience.

⚠️ Common Mistakes to Avoid

WRONGReferencing a column alias in WHERE
FIXWHERE is evaluated before SELECT, so column aliases are not available in WHERE. Use the original expression: WHERE amount * 0.18 > 1000 (not WHERE gst > 1000).
WRONGUsing reserved words as aliases without quoting
FIXAvoid aliases like "order" or "group" as they are SQL keywords. If you must use them, wrap in double quotes: SELECT amount AS "order".

❓ Frequently Asked Questions

What is an alias in SQL?

An alias is a temporary name assigned to a column or table using AS. Column aliases rename the output column for readability. Table aliases (FROM orders o) shorten table names in long queries.

Is AS required for aliases in SQL?

No — AS is optional in most databases. SELECT amount gst_amount works the same as SELECT amount AS gst_amount. But using AS explicitly makes queries clearer and more portable.

Can I use a column alias in WHERE?

No. WHERE is evaluated before the SELECT clause processes aliases. Use the original column expression in WHERE. You can use column aliases in ORDER BY and (in some databases) HAVING.

Can two columns have the same alias in SQL?

Technically yes but it creates confusion. Different columns with the same alias are allowed syntactically in most databases, but downstream tools may only show one of them. Always use unique aliases.

✏️ Practice Exercise

Rewrite a previous query you wrote — add meaningful aliases to all columns and use table aliases if there are JOINs.

← PreviousSubqueriesNext →CASE Statement
🎓 Level Up Faster

Learn SQL with Live Trainer Guidance

These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.