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_name | last_name |
|---|---|
| Alice | Johnson |
| Bob | Martinez |
| Carol | Singh |
| David | Chen |
| Eva | Brown |
| Frank | Wilson |
| Grace | Taylor |
-- With aliases
SELECT first_name AS first, last_name AS last
FROM customers;
Output:
| first | last |
|---|---|
| Alice | Johnson |
| Bob | Martinez |
| Carol | Singh |
| David | Chen |
| Eva | Brown |
| Frank | Wilson |
| Grace | Taylor |
The data is identical. Only the column headers changed. The first_name column is now displayed as "first" and last_name as "last."
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 Name | Last Name | Member Since |
|---|---|---|
| Alice | Johnson | 2023-01-15 |
| Bob | Martinez | 2023-03-22 |
| Carol | Singh | 2023-06-10 |
| David | Chen | 2023-08-05 |
| Eva | Brown | 2024-01-18 |
| Frank | Wilson | 2024-02-28 |
| Grace | Taylor | 2024-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:
| name | name |
|---|---|
| Wireless Mouse | Electronics |
| Mechanical Keyboard | Electronics |
| USB-C Hub | Electronics |
| SQL for Beginners | Books |
| Data Science Handbook | Books |
| Coffee Maker Pro | Home & Kitchen |
| Yoga Mat Premium | Sports |
| Running Shoes X1 | Sports |
| Bluetooth Speaker | Electronics |
| Stainless Water Bottle | Sports |
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_name | category_name |
|---|---|
| Wireless Mouse | Electronics |
| Mechanical Keyboard | Electronics |
| USB-C Hub | Electronics |
| SQL for Beginners | Books |
| Data Science Handbook | Books |
| Coffee Maker Pro | Home & Kitchen |
| Yoga Mat Premium | Sports |
| Running Shoes X1 | Sports |
| Bluetooth Speaker | Electronics |
| Stainless Water Bottle | Sports |
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:
| name | stock | available |
|---|---|---|
| USB-C Hub | 200 | true |
| Stainless Water Bottle | 180 | true |
| Wireless Mouse | 150 | true |
| Yoga Mat Premium | 100 | true |
| Mechanical Keyboard | 75 | true |
| Running Shoes X1 | 60 | true |
| SQL for Beginners | 50 | true |
| Data Science Handbook | 30 | true |
| Coffee Maker Pro | 25 | true |
| Bluetooth Speaker | 0 | false |
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):
| name | price | stock_quantity | ?column? |
|---|---|---|---|
| USB-C Hub | 45.00 | 200 | 9000.00 |
| Mechanical Keyboard | 89.99 | 75 | 6749.25 |
| Running Shoes X1 | 110.00 | 60 | 6600.00 |
| Wireless Mouse | 29.99 | 150 | 4498.50 |
| Stainless Water Bottle | 24.99 | 180 | 4498.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:
| name | price | stock_quantity | inventory_value |
|---|---|---|---|
| USB-C Hub | 45.00 | 200 | 9000.00 |
| Mechanical Keyboard | 89.99 | 75 | 6749.25 |
| Running Shoes X1 | 110.00 | 60 | 6600.00 |
| Wireless Mouse | 29.99 | 150 | 4498.50 |
| Stainless Water Bottle | 24.99 | 180 | 4498.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:
| name | base_price | tax_amount | price_with_tax |
|---|---|---|---|
| Coffee Maker Pro | 129.99 | 10.40 | 140.39 |
| Running Shoes X1 | 110.00 | 8.80 | 118.80 |
| Mechanical Keyboard | 89.99 | 7.20 | 97.19 |
| Bluetooth Speaker | 65.00 | 5.20 | 70.20 |
| USB-C Hub | 45.00 | 3.60 | 48.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:
| name | original_price | discount_amount | sale_price |
|---|---|---|---|
| Bluetooth Speaker | 65.00 | 13.00 | 52.00 |
| Mechanical Keyboard | 89.99 | 18.00 | 71.99 |
| Running Shoes X1 | 110.00 | 22.00 | 88.00 |
| Coffee Maker Pro | 129.99 | 26.00 | 103.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_name | contact_email |
|---|---|
| Alice Johnson | alice@email.com |
| Bob Martinez | bob@email.com |
| Carol Singh | carol@email.com |
| David Chen | david@email.com |
| Eva Brown | eva@email.com |
| Frank Wilson | frank@email.com |
| Grace Tylor | grace@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_products | cheapest_price | most_expensive | average_price |
|---|---|---|---|
| 10 | 24.99 | 129.99 | 61.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 Name | Unit Price ($) | Units in Stock |
|---|---|---|
| Coffee Maker Pro | 129.99 | 25 |
| Running Shoes X1 | 110.00 | 60 |
| Mechanical Keyboard | 89.99 | 75 |
| Bluetooth Speaker | 65.00 | 0 |
| USB-C Hub | 45.00 | 200 |
The aliases now include spaces, parentheses, and a dollar sign, which makes them perfect for report headers.
Quoting Rules by Database
| Database | Quoting for Aliases with Spaces |
|---|---|
| PostgreSQL | Double quotes: "Product Name" |
| MySQL | Backticks: `Product Name` or double quotes |
| SQLite | Double quotes: "Product Name" or brackets [Product Name] |
| SQL Server | Brackets: [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;
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;
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_name | price |
|---|---|
| Wireless Mouse | 29.99 |
| Mechanical Keyboard | 89.99 |
| USB-C Hub | 45.00 |
| SQL for Beginners | 34.99 |
| Data Science Handbook | 42.50 |
| Coffee Maker Pro | 129.99 |
| Yoga Mat Premium | 38.00 |
| Running Shoes X1 | 110.00 |
| Bluetooth Speaker | 65.00 |
| Stainless Water Bottle | 24.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.
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 Order | Clause | Can Use Column Aliases? |
|---|---|---|
| 1 | FROM | No |
| 2 | WHERE | No |
| 3 | GROUP BY | Depends on database |
| 4 | HAVING | Depends on database |
| 5 | SELECT | Aliases are defined here |
| 6 | ORDER BY | Yes |
| 7 | LIMIT | N/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:
| name | inventory_value |
|---|---|
| USB-C Hub | 9000.00 |
| Mechanical Keyboard | 6749.25 |
| Running Shoes X1 | 6600.00 |
| Wireless Mouse | 4498.50 |
| Stainless Water Bottle | 4498.20 |
| Yoga Mat Premium | 3800.00 |
| Coffee Maker Pro | 3249.75 |
| SQL for Beginners | 1749.50 |
| Data Science Handbook | 1275.00 |
| Bluetooth Speaker | 0.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:
| name | inventory_value |
|---|---|
| USB-C Hub | 9000.00 |
| Mechanical Keyboard | 6749.25 |
| Running Shoes X1 | 6600.00 |
Yes, you must write price * stock_quantity twice. This feels redundant, but it is how SQL works due to the clause evaluation order.
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_name | category_name |
|---|---|
| Wireless Mouse | Electronics |
| Mechanical Keyboard | Electronics |
| USB-C Hub | Electronics |
| SQL for Beginners | Books |
| Data Science Handbook | Books |
| Coffee Maker Pro | Home & Kitchen |
| Yoga Mat Premium | Sports |
| Running Shoes X1 | Sports |
| Bluetooth Speaker | Electronics |
| Stainless Water Bottle | Sports |
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
- Use short, meaningful abbreviations:
pforproducts,cforcategories,ofororders,oifororder_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:
| Product | Category | Price | In Stock | Available |
|---|---|---|---|---|
| Data Science Handbook | Books | 42.50 | 30 | Yes |
| SQL for Beginners | Books | 34.99 | 50 | Yes |
| Bluetooth Speaker | Electronics | 65.00 | 0 | No |
| Mechanical Keyboard | Electronics | 89.99 | 75 | Yes |
| USB-C Hub | Electronics | 45.00 | 200 | Yes |
| Wireless Mouse | Electronics | 29.99 | 150 | Yes |
| Coffee Maker Pro | Home & Kitchen | 129.99 | 25 | Yes |
| Running Shoes X1 | Sports | 110.00 | 60 | Yes |
| Stainless Water Bottle | Sports | 24.99 | 180 | Yes |
| Yoga Mat Premium | Sports | 38.00 | 100 | Yes |
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 # | Customer | Date | Total | Status |
|---|---|---|---|---|
| 8 | Frank Wilson | 2024-04-10 | 199.98 | pending |
| 7 | Bob Martinez | 2024-04-01 | 63.00 | pending |
| 6 | Eva Brown | 2024-03-20 | 34.99 | pending |
| 5 | David Chen | 2024-03-12 | 155.00 | shipped |
| 4 | Carol Singh | 2024-03-05 | 129.99 | shipped |
| 3 | Alice Johnson | 2024-02-20 | 77.49 | completed |
| 2 | Bob Martinez | 2024-01-15 | 89.99 | completed |
| 1 | Alice Johnson | 2024-01-10 | 119.98 | completed |
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:
| product | current_price | 15% Off | 25% Off | 50% Off |
|---|---|---|---|---|
| Coffee Maker Pro | 129.99 | 110.49 | 97.49 | 65.00 |
| Running Shoes X1 | 110.00 | 93.50 | 82.50 | 55.00 |
| Mechanical Keyboard | 89.99 | 76.49 | 67.49 | 45.00 |
| Bluetooth Speaker | 65.00 | 55.25 | 48.75 | 32.50 |
| USB-C Hub | 45.00 | 38.25 | 33.75 | 22.50 |
| Data Science Handbook | 42.50 | 36.13 | 31.88 | 21.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:
| product | unit_price | units | total_value | stock_status |
|---|---|---|---|---|
| USB-C Hub | 45.00 | 200 | 9000.00 | Well Stocked |
| Mechanical Keyboard | 89.99 | 75 | 6749.25 | Normal |
| Running Shoes X1 | 110.00 | 60 | 6600.00 | Normal |
| Wireless Mouse | 29.99 | 150 | 4498.50 | Well Stocked |
| Stainless Water Bottle | 24.99 | 180 | 4498.20 | Well Stocked |
| Yoga Mat Premium | 38.00 | 100 | 3800.00 | Well Stocked |
| Coffee Maker Pro | 129.99 | 25 | 3249.75 | Low Stock |
| SQL for Beginners | 34.99 | 50 | 1749.50 | Normal |
| Data Science Handbook | 42.50 | 30 | 1275.00 | Normal |
| Bluetooth Speaker | 65.00 | 0 | 0.00 | Out 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:
| Product | Cost |
|---|---|
| Stainless Water Bottle | 24.99 |
| Wireless Mouse | 29.99 |
| SQL for Beginners | 34.99 |
| Yoga Mat Premium | 38.00 |
| Data Science Handbook | 42.50 |
| USB-C Hub | 45.00 |
| Bluetooth Speaker | 65.00 |
| Mechanical Keyboard | 89.99 |
| Running Shoes X1 | 110.00 |
| Coffee Maker Pro | 129.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:
| product | original_price | discounted_price | savings |
|---|---|---|---|
| Coffee Maker Pro | 129.99 | 116.99 | 13.00 |
| Running Shoes X1 | 110.00 | 99.00 | 11.00 |
| Mechanical Keyboard | 89.99 | 80.99 | 9.00 |
| Bluetooth Speaker | 65.00 | 58.50 | 6.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 Name | City |
|---|---|
| Eva Brown | Seattle |
| David Chen | New York |
| Alice Johnson | New York |
| Bob Martinez | Los Angeles |
| Carol Singh | Chicago |
| Grace Taylor | Unknown |
| Frank Wilson | Chicago |
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_id | customer | total | order_status |
|---|---|---|---|
| 8 | Frank Wilson | 199.98 | pending |
| 5 | David Chen | 155.00 | shipped |
| 4 | Carol Singh | 129.99 | shipped |
| 1 | Alice Johnson | 119.98 | completed |
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:
| Product | Qty | Price | Total Value | Status |
|---|---|---|---|---|
| USB-C Hub | 200 | 45.00 | 9000.00 | In Stock |
| Mechanical Keyboard | 75 | 89.99 | 6749.25 | In Stock |
| Running Shoes X1 | 60 | 110.00 | 6600.00 | In Stock |
| Wireless Mouse | 150 | 29.99 | 4498.50 | In Stock |
| Stainless Water Bottle | 180 | 24.99 | 4498.20 | In Stock |
| Yoga Mat Premium | 100 | 38.00 | 3800.00 | In Stock |
| Coffee Maker Pro | 25 | 129.99 | 3249.75 | Running Low |
| SQL for Beginners | 50 | 34.99 | 1749.50 | In Stock |
| Data Science Handbook | 30 | 42.50 | 1275.00 | Running Low |
| Bluetooth Speaker | 0 | 65.00 | 0.00 | Reorder Now |
Key Takeaways
Column aliases are a simple but essential tool for producing clean, readable query results. Here is what you should remember:
AScreates 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
ASkeyword is technically optional, but always include it for readability and to prevent bugs - Aliases can be used in
ORDER BYbecause it executes afterSELECT - Aliases cannot be used in
WHEREbecause it executes beforeSELECT. 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.