Python SQL Aliases: AS Keyword for Columns & Tables
SQL aliases assign temporary, readable names to columns or tables using the `AS` keyword. Column aliases format computed expressions in result headers, while table aliases simplify multi-table joins and self-joins.
"A table alias is like giving a long formal company name a short nickname in conversation (e.g. referring to "ZenCompiler International Logistics Corp" simply as "Z")."
Deep Dive: How It Works
Column Aliases: `SELECT unit_price * quantity AS subtotal FROM cart`. The `AS` keyword is technically optional (`SELECT col name`), but including `AS` is best practice for clarity.
Table Aliases: `FROM orders AS o JOIN customers AS c ON o.customer_id = c.id`. Qualifies columns cleanly (`o.id`, `c.name`).
Identifier Quoting: If an alias contains spaces or special characters, wrap it in double quotes (`AS "Total Price (USD)"`).
Syntax Blueprint
SELECT c.first_name AS customer_name, o.order_date AS placed_at FROM customers AS c JOIN orders AS o ON c.id = o.customer_id;
Use table aliases (c, o) to qualify columns and column aliases for clean output headers.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Attempting to use a column alias in the WHERE clause.Why it happens: Because WHERE executes before SELECT defines the alias.
How to fix: Repeat the formula in WHERE or wrap in a subquery or Common Table Expression (CTE).
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Assign Column Alias with AS
Select `first_name AS name`, `(age * 12) AS age_in_months` from `profiles`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.