Skip to main content

SQL AS for Column Aliases

As your queries grow more sophisticated, the default column names in your results start to fall short. A calculated expression like price * stock_quantity gets a messy auto-generated label. A joined query returns two columns both named name, one from products and one from categories, and you cannot tell them apart. An abbreviated column name like qty makes perfect sense in the database schema but confuses everyone reading the report.

Column aliases solve all of these problems. The AS keyword lets you rename any column in your output to something clearer, more descriptive, or more user-friendly. The alias does not change anything in the database itself. It only affects how the column is labeled in the query results.

This guide covers everything you need to know about column aliases: basic renaming, aliasing expressions and calculations, handling aliases with spaces and special characters, where aliases can and cannot be used, and the common mistakes that trip up beginners. Every example uses the ShopSmart sample database with full outputs (we defined it in a previous guide here).

How Column Aliases Work

A column alias assigns a temporary name to a column or expression in your query results. You create one by placing the AS keyword after the column or expression, followed by the name you want:

SELECT column_name AS alias_name
FROM table_name;

The alias appears as the column header in your results instead of the original column name. Here is the simplest example:

-- Without alias
SELECT first_name, last_name
FROM customers;

Output:

first_namelast_name
AliceJohnson
BobMartinez
CarolSingh
DavidChen
EvaBrown
FrankWilson
GraceTaylor
-- With aliases
SELECT first_name AS first, last_name AS last
FROM customers;

Output:

firstlast
AliceJohnson
BobMartinez
CarolSingh
DavidChen
EvaBrown
FrankWilson
GraceTaylor

The data is identical. Only the column headers changed. The first_name column is now displayed as "first" and last_name as "last."

Key Concept

Aliases are purely cosmetic for your query output. They do not rename, modify, or affect the actual columns in the database. The underlying table structure remains completely unchanged. The next time you query customers, the columns will still be called first_name and last_name.

Renaming Columns for Clarity

The most straightforward use of aliases is making column names more readable or more meaningful to the consumer of the data.

Making Technical Names User-Friendly

Database column names often use abbreviations, snake_case, or technical naming conventions that make sense to developers but not to business users reading a report:

SELECT first_name AS "First Name",
last_name AS "Last Name",
signup_date AS "Member Since"
FROM customers
ORDER BY signup_date;

Output:

First NameLast NameMember Since
AliceJohnson2023-01-15
BobMartinez2023-03-22
CarolSingh2023-06-10
DavidChen2023-08-05
EvaBrown2024-01-18
FrankWilson2024-02-28
GraceTaylor2024-04-01

The column signup_date becomes "Member Since," which is much more meaningful in a customer-facing report.

Disambiguating Columns from Joined Tables

When you join two tables that have columns with the same name, aliases prevent confusion:

-- Without aliases: Two columns named 'name', which is which?
SELECT p.name, c.name
FROM products p
JOIN categories c ON p.category_id = c.id;

Output:

namename
Wireless MouseElectronics
Mechanical KeyboardElectronics
USB-C HubElectronics
SQL for BeginnersBooks
Data Science HandbookBooks
Coffee Maker ProHome & Kitchen
Yoga Mat PremiumSports
Running Shoes X1Sports
Bluetooth SpeakerElectronics
Stainless Water BottleSports

Two columns both labeled "name" makes the results ambiguous. Aliases fix this immediately:

-- With aliases: Crystal clear
SELECT p.name AS product_name, c.name AS category_name
FROM products p
JOIN categories c ON p.category_id = c.id;

Output:

product_namecategory_name
Wireless MouseElectronics
Mechanical KeyboardElectronics
USB-C HubElectronics
SQL for BeginnersBooks
Data Science HandbookBooks
Coffee Maker ProHome & Kitchen
Yoga Mat PremiumSports
Running Shoes X1Sports
Bluetooth SpeakerElectronics
Stainless Water BottleSports

Now there is zero ambiguity about what each column represents.

Shortening Long Column Names

Sometimes the original column name is unnecessarily verbose for the context:

SELECT name,
stock_quantity AS stock,
is_available AS available
FROM products
ORDER BY stock DESC;

Output:

namestockavailable
USB-C Hub200true
Stainless Water Bottle180true
Wireless Mouse150true
Yoga Mat Premium100true
Mechanical Keyboard75true
Running Shoes X160true
SQL for Beginners50true
Data Science Handbook30true
Coffee Maker Pro25true
Bluetooth Speaker0false

The full column names stock_quantity and is_available are shortened to stock and available, keeping the output compact and scannable.

Using Aliases with Expressions

Aliases become essential when your SELECT list includes calculated expressions. Without an alias, the database auto-generates a column name based on the expression, and the result is often unreadable.

The Problem Without Aliases

-- No alias on the calculated column
SELECT name,
price,
stock_quantity,
price * stock_quantity
FROM products
ORDER BY price * stock_quantity DESC
LIMIT 5;

Output (varies by database):

namepricestock_quantity?column?
USB-C Hub45.002009000.00
Mechanical Keyboard89.99756749.25
Running Shoes X1110.00606600.00
Wireless Mouse29.991504498.50
Stainless Water Bottle24.991804498.20

The calculated column gets a label like ?column? (PostgreSQL), price * stock_quantity (MySQL), or (No column name) (SQL Server). None of these are useful.

The Fix: Name Your Expressions

SELECT name,
price,
stock_quantity,
price * stock_quantity AS inventory_value
FROM products
ORDER BY inventory_value DESC
LIMIT 5;

Output:

namepricestock_quantityinventory_value
USB-C Hub45.002009000.00
Mechanical Keyboard89.99756749.25
Running Shoes X1110.00606600.00
Wireless Mouse29.991504498.50
Stainless Water Bottle24.991804498.20

Now the calculated column has a clear, meaningful name: inventory_value.

Common Expression Aliases

Here are several examples of expressions that benefit from aliases:

-- Price with tax (8% tax rate)
SELECT name,
price AS base_price,
price * 0.08 AS tax_amount,
price * 1.08 AS price_with_tax
FROM products
ORDER BY price_with_tax DESC
LIMIT 5;

Output:

namebase_pricetax_amountprice_with_tax
Coffee Maker Pro129.9910.40140.39
Running Shoes X1110.008.80118.80
Mechanical Keyboard89.997.2097.19
Bluetooth Speaker65.005.2070.20
USB-C Hub45.003.6048.60
-- Discount calculation
SELECT name,
price AS original_price,
price * 0.20 AS discount_amount,
price * 0.80 AS sale_price
FROM products
WHERE price > 50
ORDER BY sale_price ASC;

Output:

nameoriginal_pricediscount_amountsale_price
Bluetooth Speaker65.0013.0052.00
Mechanical Keyboard89.9918.0071.99
Running Shoes X1110.0022.0088.00
Coffee Maker Pro129.9926.00103.99

Aliasing String Concatenation

Aliases work with string expressions too:

-- PostgreSQL / SQLite
SELECT first_name || ' ' || last_name AS full_name,
email AS contact_email
FROM customers;

-- MySQL
SELECT CONCAT(first_name, ' ', last_name) AS full_name,
email AS contact_email
FROM customers;

Output:

full_namecontact_email
Alice Johnsonalice@email.com
Bob Martinezbob@email.com
Carol Singhcarol@email.com
David Chendavid@email.com
Eva Browneva@email.com
Frank Wilsonfrank@email.com
Grace Tylorgrace@email.com

Without the alias, the concatenation column would be labeled something like ?column? or CONCAT(first_name, ' ', last_name).

Aliasing Function Results

SELECT COUNT(*) AS total_products,
MIN(price) AS cheapest_price,
MAX(price) AS most_expensive,
ROUND(AVG(price), 2) AS average_price
FROM products;

Output:

total_productscheapest_pricemost_expensiveaverage_price
1024.99129.9961.05

Without aliases, these columns would have auto-generated names like count, min, max, and round, which are vague and can even conflict with SQL keywords in some contexts.

Quoting Aliases with Spaces and Special Characters

By default, alias names must follow the same rules as column names: no spaces, no special characters, and they must start with a letter or underscore. But sometimes you need aliases with spaces or mixed case for presentation purposes.

Double Quotes for Special Aliases

Wrap the alias in double quotes to include spaces, special characters, or preserve specific casing:

SELECT name AS "Product Name",
price AS "Unit Price ($)",
stock_quantity AS "Units in Stock"
FROM products
ORDER BY price DESC
LIMIT 5;

Output:

Product NameUnit Price ($)Units in Stock
Coffee Maker Pro129.9925
Running Shoes X1110.0060
Mechanical Keyboard89.9975
Bluetooth Speaker65.000
USB-C Hub45.00200

The aliases now include spaces, parentheses, and a dollar sign, which makes them perfect for report headers.

Quoting Rules by Database

DatabaseQuoting for Aliases with Spaces
PostgreSQLDouble quotes: "Product Name"
MySQLBackticks: `Product Name` or double quotes
SQLiteDouble quotes: "Product Name" or brackets [Product Name]
SQL ServerBrackets: [Product Name] or double quotes
-- PostgreSQL / SQLite
SELECT name AS "Product Name" FROM products;

-- MySQL
SELECT name AS `Product Name` FROM products;

-- SQL Server
SELECT name AS [Product Name] FROM products;
Best Practice

Double quotes work in most databases and are the ANSI SQL standard for quoted identifiers. Use them as your default when you need spaces in aliases:

SELECT name AS "Product Name" FROM products;

For aliases without spaces, skip the quotes entirely:

SELECT name AS product_name FROM products;
Common Mistake: Using Single Quotes for Aliases

Single quotes are for data values, not for identifiers. Using them for aliases will cause errors or unexpected behavior:

-- Wrong: Single quotes create a string literal, not an alias
SELECT name AS 'Product Name' FROM products;
-- Some databases will error; others will treat 'Product Name' as a string constant

-- Correct: Double quotes for aliases with spaces
SELECT name AS "Product Name" FROM products;

Remember the rule: single quotes for values ('New York'), double quotes for identifiers ("Product Name").

The AS Keyword Is Optional (But You Should Use It)

In most SQL databases, the AS keyword is optional. You can create an alias by simply placing the new name after the column or expression:

-- With AS (explicit)
SELECT name AS product_name, price AS unit_price
FROM products;

-- Without AS (implicit)
SELECT name product_name, price unit_price
FROM products;

Both queries produce identical results:

product_nameprice
Wireless Mouse29.99
Mechanical Keyboard89.99
USB-C Hub45.00
SQL for Beginners34.99
Data Science Handbook42.50
Coffee Maker Pro129.99
Yoga Mat Premium38.00
Running Shoes X1110.00
Bluetooth Speaker65.00
Stainless Water Bottle24.99

Why You Should Always Use AS

Even though omitting AS works, it is strongly recommended to always include it. Here is why:

Readability: AS makes the alias relationship explicitly clear:

-- Clear: Obviously creating aliases
SELECT first_name AS first, last_name AS last FROM customers;

-- Confusing: Are these column names, aliases, or typos?
SELECT first_name first, last_name last FROM customers;

Prevents ambiguity: Without AS, it is easy to accidentally create unintended aliases, especially when you forget a comma:

-- Intended: Select two columns
SELECT name, price FROM products;

-- Bug: Missing comma, 'price' becomes an alias for 'name'
SELECT name price FROM products;

Output of the buggy query:

price
Wireless Mouse
Mechanical Keyboard
USB-C Hub
SQL for Beginners
Data Science Handbook
Coffee Maker Pro
Yoga Mat Premium
Running Shoes X1
Bluetooth Speaker
Stainless Water Bottle

The query returns a single column of product names, but labeled as "price." This is the same missing-comma bug discussed in earlier guides, and it becomes even more confusing in longer queries.

Rule

Always include AS when creating aliases. It costs nothing, prevents bugs, and makes your queries self-documenting. The only reason to omit it is in quick, throwaway queries typed directly into a console.

Where Aliases Can and Cannot Be Used

This is a critical section. Aliases have specific rules about where they can be referenced within a query, and violating these rules is one of the most common beginner frustrations.

SQL Clause Execution Order

To understand alias limitations, you need to know the order in which SQL processes the clauses of a query. This is not the order you write them in:

Execution OrderClauseCan Use Column Aliases?
1FROMNo
2WHERENo
3GROUP BYDepends on database
4HAVINGDepends on database
5SELECTAliases are defined here
6ORDER BYYes
7LIMITN/A

The key insight: aliases are defined in the SELECT clause (step 5), so they do not exist yet when WHERE (step 2) is being evaluated.

Aliases Work in ORDER BY

ORDER BY is processed after SELECT, so it can reference aliases:

-- This works: ORDER BY can use the alias
SELECT name,
price * stock_quantity AS inventory_value
FROM products
ORDER BY inventory_value DESC;

Output:

nameinventory_value
USB-C Hub9000.00
Mechanical Keyboard6749.25
Running Shoes X16600.00
Wireless Mouse4498.50
Stainless Water Bottle4498.20
Yoga Mat Premium3800.00
Coffee Maker Pro3249.75
SQL for Beginners1749.50
Data Science Handbook1275.00
Bluetooth Speaker0.00

Aliases Do NOT Work in WHERE

WHERE is processed before SELECT, so aliases do not exist yet:

-- This FAILS: WHERE cannot see the alias
SELECT name,
price * stock_quantity AS inventory_value
FROM products
WHERE inventory_value > 5000;
-- ERROR: column "inventory_value" does not exist

The fix: Repeat the full expression in the WHERE clause:

-- Correct: Use the full expression in WHERE
SELECT name,
price * stock_quantity AS inventory_value
FROM products
WHERE price * stock_quantity > 5000
ORDER BY inventory_value DESC;

Output:

nameinventory_value
USB-C Hub9000.00
Mechanical Keyboard6749.25
Running Shoes X16600.00

Yes, you must write price * stock_quantity twice. This feels redundant, but it is how SQL works due to the clause evaluation order.

This Rule Trips Up Everyone

The inability to use aliases in WHERE is one of the most common sources of frustration for SQL learners. Here is the complete pattern:

-- Wrong: Alias in WHERE
SELECT price * 1.08 AS price_with_tax
FROM products
WHERE price_with_tax > 50;
-- ERROR

-- Correct: Repeat the expression in WHERE
SELECT price * 1.08 AS price_with_tax
FROM products
WHERE price * 1.08 > 50;
-- Works!

-- Also correct: Use a subquery (advanced technique)
SELECT *
FROM (
SELECT name, price * 1.08 AS price_with_tax
FROM products
) AS subquery
WHERE price_with_tax > 50;
-- Works! The alias exists in the inner query's SELECT,
-- so the outer WHERE can reference it.

MySQL Exception: Aliases in GROUP BY and HAVING

MySQL is more lenient than other databases and allows aliases in GROUP BY and HAVING clauses:

-- MySQL: This works
SELECT category_id AS cat, COUNT(*) AS product_count
FROM products
GROUP BY cat
HAVING product_count > 2;

-- PostgreSQL/SQL Server: This fails
-- You must use the original column name:
SELECT category_id AS cat, COUNT(*) AS product_count
FROM products
GROUP BY category_id
HAVING COUNT(*) > 2;

For portability, always use the original column names or expressions in GROUP BY and HAVING.

Table Aliases

While this guide focuses on column aliases, it is worth noting that AS is also used to create table aliases, which shorten table names in queries, especially when joining:

-- Without table aliases: Verbose
SELECT products.name, categories.name
FROM products
JOIN categories ON products.category_id = categories.id;

-- With table aliases: Clean and concise
SELECT p.name AS product_name, c.name AS category_name
FROM products AS p
JOIN categories AS c ON p.category_id = c.id;

Output:

product_namecategory_name
Wireless MouseElectronics
Mechanical KeyboardElectronics
USB-C HubElectronics
SQL for BeginnersBooks
Data Science HandbookBooks
Coffee Maker ProHome & Kitchen
Yoga Mat PremiumSports
Running Shoes X1Sports
Bluetooth SpeakerElectronics
Stainless Water BottleSports

Table aliases (p for products, c for categories) let you reference columns with shorter prefixes throughout the query. Just like with column aliases, the AS keyword is optional for table aliases, and you will often see it omitted:

-- Both are equivalent
FROM products AS p
FROM products p
Convention for Table Aliases
  • Use short, meaningful abbreviations: p for products, c for categories, o for orders, oi for order_items
  • Keep them consistent throughout your query and across your codebase
  • Table aliases are especially valuable in queries with multiple joins where typing full table names becomes tedious

Practical Examples: Aliases in Real Scenarios

Product Catalog Display

SELECT p.name AS "Product",
c.name AS "Category",
p.price AS "Price",
p.stock_quantity AS "In Stock",
CASE
WHEN p.is_available = true THEN 'Yes'
ELSE 'No'
END AS "Available"
FROM products AS p
JOIN categories AS c ON p.category_id = c.id
ORDER BY c.name, p.name;

Output:

ProductCategoryPriceIn StockAvailable
Data Science HandbookBooks42.5030Yes
SQL for BeginnersBooks34.9950Yes
Bluetooth SpeakerElectronics65.000No
Mechanical KeyboardElectronics89.9975Yes
USB-C HubElectronics45.00200Yes
Wireless MouseElectronics29.99150Yes
Coffee Maker ProHome & Kitchen129.9925Yes
Running Shoes X1Sports110.0060Yes
Stainless Water BottleSports24.99180Yes
Yoga Mat PremiumSports38.00100Yes

This query produces a clean, report-ready result with human-friendly column names.

Order Summary Report

SELECT o.id AS "Order #",
c.first_name || ' ' || c.last_name AS "Customer",
o.order_date AS "Date",
o.total_amount AS "Total",
o.status AS "Status"
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.id
ORDER BY o.order_date DESC;

Output:

Order #CustomerDateTotalStatus
8Frank Wilson2024-04-10199.98pending
7Bob Martinez2024-04-0163.00pending
6Eva Brown2024-03-2034.99pending
5David Chen2024-03-12155.00shipped
4Carol Singh2024-03-05129.99shipped
3Alice Johnson2024-02-2077.49completed
2Bob Martinez2024-01-1589.99completed
1Alice Johnson2024-01-10119.98completed

Pricing Analysis

SELECT name AS product,
price AS current_price,
ROUND(price * 0.85, 2) AS "15% Off",
ROUND(price * 0.75, 2) AS "25% Off",
ROUND(price * 0.50, 2) AS "50% Off"
FROM products
WHERE price > 40
ORDER BY current_price DESC;

Output:

productcurrent_price15% Off25% Off50% Off
Coffee Maker Pro129.99110.4997.4965.00
Running Shoes X1110.0093.5082.5055.00
Mechanical Keyboard89.9976.4967.4945.00
Bluetooth Speaker65.0055.2548.7532.50
USB-C Hub45.0038.2533.7522.50
Data Science Handbook42.5036.1331.8821.25

Inventory Value Dashboard

SELECT name AS product,
price AS unit_price,
stock_quantity AS units,
price * stock_quantity AS total_value,
CASE
WHEN stock_quantity = 0 THEN 'Out of Stock'
WHEN stock_quantity < 30 THEN 'Low Stock'
WHEN stock_quantity < 100 THEN 'Normal'
ELSE 'Well Stocked'
END AS stock_status
FROM products
ORDER BY total_value DESC;

Output:

productunit_priceunitstotal_valuestock_status
USB-C Hub45.002009000.00Well Stocked
Mechanical Keyboard89.99756749.25Normal
Running Shoes X1110.00606600.00Normal
Wireless Mouse29.991504498.50Well Stocked
Stainless Water Bottle24.991804498.20Well Stocked
Yoga Mat Premium38.001003800.00Well Stocked
Coffee Maker Pro129.99253249.75Low Stock
SQL for Beginners34.99501749.50Normal
Data Science Handbook42.50301275.00Normal
Bluetooth Speaker65.0000.00Out of Stock

This dashboard-style query uses aliases on every column, including calculated values and a CASE expression, to produce clean output that could be displayed directly in an admin panel.

Practical Exercises

Exercise 1

Display all products with their name labeled as "Product" and price labeled as "Cost."

SELECT name AS "Product",
price AS "Cost"
FROM products
ORDER BY price;

Expected output:

ProductCost
Stainless Water Bottle24.99
Wireless Mouse29.99
SQL for Beginners34.99
Yoga Mat Premium38.00
Data Science Handbook42.50
USB-C Hub45.00
Bluetooth Speaker65.00
Mechanical Keyboard89.99
Running Shoes X1110.00
Coffee Maker Pro129.99

Exercise 2

Calculate and display a 10% discount price for all products over $50, with clear aliases.

SELECT name AS product,
price AS original_price,
ROUND(price * 0.90, 2) AS discounted_price,
ROUND(price * 0.10, 2) AS savings
FROM products
WHERE price > 50
ORDER BY savings DESC;

Expected output:

productoriginal_pricediscounted_pricesavings
Coffee Maker Pro129.99116.9913.00
Running Shoes X1110.0099.0011.00
Mechanical Keyboard89.9980.999.00
Bluetooth Speaker65.0058.506.50

Exercise 3

Create a customer directory showing full name and city with user-friendly column headers.

SELECT first_name || ' ' || last_name AS "Full Name",
COALESCE(city, 'Unknown') AS "City"
FROM customers
ORDER BY last_name;

Expected output:

Full NameCity
Eva BrownSeattle
David ChenNew York
Alice JohnsonNew York
Bob MartinezLos Angeles
Carol SinghChicago
Grace TaylorUnknown
Frank WilsonChicago

Exercise 4

Show order details with customer names, using table and column aliases throughout.

SELECT o.id AS order_id,
c.first_name || ' ' || c.last_name AS customer,
o.total_amount AS total,
o.status AS order_status
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.id
WHERE o.total_amount > 100
ORDER BY total DESC;

Expected output:

order_idcustomertotalorder_status
8Frank Wilson199.98pending
5David Chen155.00shipped
4Carol Singh129.99shipped
1Alice Johnson119.98completed

Exercise 5

Create an inventory report showing each product's name, stock status, and total inventory value.

SELECT name AS "Product",
stock_quantity AS "Qty",
price AS "Price",
price * stock_quantity AS "Total Value",
CASE
WHEN stock_quantity = 0 THEN 'Reorder Now'
WHEN stock_quantity < 50 THEN 'Running Low'
ELSE 'In Stock'
END AS "Status"
FROM products
ORDER BY "Total Value" DESC;

Expected output:

ProductQtyPriceTotal ValueStatus
USB-C Hub20045.009000.00In Stock
Mechanical Keyboard7589.996749.25In Stock
Running Shoes X160110.006600.00In Stock
Wireless Mouse15029.994498.50In Stock
Stainless Water Bottle18024.994498.20In Stock
Yoga Mat Premium10038.003800.00In Stock
Coffee Maker Pro25129.993249.75Running Low
SQL for Beginners5034.991749.50In Stock
Data Science Handbook3042.501275.00Running Low
Bluetooth Speaker065.000.00Reorder Now

Key Takeaways

Column aliases are a simple but essential tool for producing clean, readable query results. Here is what you should remember:

  • AS creates a temporary name for a column or expression in your query output
  • Aliases do not modify the database. They only affect the current query's results
  • Use aliases to rename columns for clarity: SELECT signup_date AS "Member Since"
  • Use aliases to label expressions: SELECT price * 1.08 AS price_with_tax
  • Use double quotes for aliases with spaces or special characters: AS "Product Name"
  • Never use single quotes for aliases. Single quotes are for data values only
  • The AS keyword is technically optional, but always include it for readability and to prevent bugs
  • Aliases can be used in ORDER BY because it executes after SELECT
  • Aliases cannot be used in WHERE because it executes before SELECT. Repeat the full expression instead
  • Table aliases (FROM products AS p) shorten table references in complex queries with joins
  • Well-chosen aliases turn raw query output into report-ready, presentation-quality results

Aliases are one of those features that seem minor at first but become indispensable as your queries grow in complexity. Every production query, every report, and every API response benefits from clear, well-named columns.