PostgreSQL AS
Create aliases for columns and tables
🏷️ What is PostgreSQL AS?
The AS keyword creates aliases (temporary names) for columns and tables in your queries. Aliases make results more readable and queries shorter. They're especially useful with calculations, joins, and aggregate functions. The AS keyword itself is optional but recommended for clarity.
-- Give a column a friendly name
SELECT price * quantity AS total_cost
FROM orders;
AS Alias Types
Column Alias
Rename columns in results
SELECT name AS customer_name
FROM customers;
Calculation Alias
Name calculated columns
SELECT price * 0.9
AS discounted_price;
Table Alias
Shorten table names
SELECT c.name
FROM customers AS c;
Aggregate Alias
Name aggregate results
SELECT COUNT(*) AS total_orders
FROM orders;
🔹 Basic Column Alias
Column aliases rename columns in query results, making output more user-friendly and descriptive. The original column name in the database remains unchanged. Aliases are temporary and only exist for the duration of the query. Use them to clarify column meanings or replace technical names with business terms.
-- Sample data: employees table
-- emp_id | first_name | last_name | annual_salary
-- 1 | John | Smith | 75000
-- 2 | Jane | Doe | 82000
-- Use aliases for clearer column names
SELECT
first_name AS "First Name",
last_name AS "Last Name",
annual_salary AS "Yearly Salary"
FROM employees;
Output:
| First Name | Last Name | Yearly Salary |
|---|---|---|
| John | Smith | 75000 |
| Jane | Doe | 82000 |
🔹 Alias for Calculations
When performing calculations in SELECT statements, the result column gets a generic name. Use aliases to give calculated columns meaningful names. This makes your results self-documenting and easier to understand. Essential for reports, dashboards, and any output that will be read by humans or processed by applications.
-- Calculate total price with meaningful column name
SELECT
product_name,
price,
quantity,
price * quantity AS total_cost,
price * quantity * 0.1 AS tax_amount
FROM order_items;
Output:
| product_name | price | quantity | total_cost | tax_amount |
|---|---|---|---|---|
| Laptop | 1000 | 2 | 2000 | 200 |
🔹 Alias with Aggregate Functions
Aggregate functions like COUNT, SUM, AVG produce results with generic or complex names. Always use aliases with aggregates to create clear, descriptive column headers. This is crucial for reports and analytics where the meaning of each metric must be immediately obvious to readers and stakeholders.
-- Use aliases for aggregate function results
SELECT
COUNT(*) AS total_orders,
SUM(amount) AS total_revenue,
AVG(amount) AS average_order_value,
MAX(amount) AS largest_order
FROM orders;
Output:
| total_orders | total_revenue | average_order_value | largest_order |
|---|---|---|---|
| 150 | 45000 | 300 | 2500 |
🔹 Table Aliases
Table aliases shorten long table names, making queries more concise and readable. They're essential when working with joins where you need to specify which table a column comes from. Use short, meaningful aliases like 'c' for customers or 'o' for orders. Once defined, use the alias throughout the query.
-- Use table aliases to shorten queries
SELECT
c.name AS customer_name,
c.email,
o.order_date,
o.total_amount
FROM customers AS c
JOIN orders AS o ON c.customer_id = o.customer_id;
Output:
| customer_name | order_date | total_amount | |
|---|---|---|---|
| John Smith | [email protected] | 2024-01-15 | 1500 |
🔹 AS Keyword is Optional
The AS keyword is optional in PostgreSQL - you can create aliases by simply placing the alias name after the column or table. However, using AS makes your code more explicit and easier to read. It's considered best practice to include AS for clarity, especially in complex queries or team environments.
-- Both syntaxes work the same way
-- With AS (recommended for clarity)
SELECT name AS customer_name FROM customers AS c;
-- Without AS (works but less clear)
SELECT name customer_name FROM customers c;
Both produce identical results:
✅ Use AS for better code readability!
🔹 Aliases with Spaces
If your alias contains spaces or special characters, enclose it in double quotes. This allows you to create more natural, human-readable column names. Without quotes, PostgreSQL treats spaces as syntax errors. Use this feature for reports and exports where column headers need to be business-friendly and professional-looking.
-- Use quotes for aliases with spaces
SELECT
first_name AS "First Name",
last_name AS "Last Name",
salary * 12 AS "Annual Salary",
department AS "Department Name"
FROM employees;
Output:
| First Name | Last Name | Annual Salary | Department Name |
|---|---|---|---|
| John | Smith | 900000 | Sales |
🔹 Using Aliases in ORDER BY
You can reference column aliases in ORDER BY and GROUP BY clauses, but not in WHERE clauses. This is because WHERE is evaluated before SELECT, so aliases don't exist yet. However, ORDER BY and GROUP BY run after SELECT, making aliases available. This simplifies sorting and grouping by calculated columns.
-- Use alias in ORDER BY
SELECT
product_name,
price * quantity AS total_value
FROM order_items
ORDER BY total_value DESC;
Output:
| product_name | total_value |
|---|---|
| Laptop | 2400 |
| Monitor | 600 |
| Mouse | 50 |
💡 Important Notes:
- AS keyword is optional but recommended for clarity
- Aliases are temporary and only exist during query execution
- Use double quotes for aliases with spaces: "First Name"
- Column aliases can be used in ORDER BY and GROUP BY
- Column aliases CANNOT be used in WHERE clause
- Table aliases must be used consistently throughout the query
- Aliases make queries more readable and results more meaningful