Chapter 30: PostgreSQL AS

the AS keyword (and its very frequent optional version — no AS at all).

Many beginners think “AS is just for renaming columns”, but it is actually much more powerful and appears in several different places in a query.

Today I’m going to explain every realistic use of AS in PostgreSQL (version 18 reality in 2026), step-by-step, like we’re writing queries together on your laptop.

1. The main job of AS — column aliasing (90% of its daily usage)

AS gives a temporary new name to a column or an expression in the result set.

SQL

Very important rules in PostgreSQL:

  • If the alias contains spaces, uppercase letters you want to preserve, or special characters → you must use double quotes ” “
  • Without quotes → alias becomes lowercase (PostgreSQL folds unquoted identifiers)
  • AS is optional when the alias is a simple word (most developers skip it)

Examples side-by-side — both are correct:

SQL

2. Table aliasing (extremely common — especially with JOINs)

When you join multiple tables, you give short temporary names to each table so you don’t have to write the full table name every time.

SQL
  • AS is optional here too → most people write students s (without AS)
  • Short aliases (s, e, c, u, o…) make queries much easier to read

Without aliases (painful):

SQL

3. AS in CTEs (Common Table Expressions) — very modern & powerful

SQL

Here AS is required after the CTE name.

4. AS in subqueries (derived tables)

SQL

Again — AS is required when you give a name to a subquery (derived table).

5. AS with functions & expressions (very clean)

SQL

6. Quick cheat-sheet — when AS is required vs optional

Place AS required? Example (preferred style 2026) Notes
Column / expression alias Optional gpa * 10 AS percentage or gpa * 10 percentage Use AS when alias has spaces
Table alias in FROM/JOIN Optional students s or students AS s Most people skip AS
CTE name Required WITH active AS (…) Must have name
Subquery / derived table Required FROM (SELECT …) AS sub Must have name
CAST / type conversion Optional gpa::numeric(4,2) AS gpa_formatted

7. Common beginner mistakes & fixes

Mistake Error / Problem Fix
SELECT name as “Full Name” (single quotes) Syntax error Use double quotes “Full Name”
SELECT name AS full name (no quotes) Becomes lowercase full name (two words) “Full Name” or full_name
Forget alias on subquery ERROR: subquery must have alias Add AS tmp or any name
Use same alias name twice in same SELECT ERROR: column reference ambiguous Use different names or qualify
AS in WHERE / GROUP BY / HAVING Syntax error AS only in SELECT / FROM / WITH

Your mini practice right now (try in your database)

  1. Select first_name || ‘ ‘ || last_name as “Full Name”
  2. Join students and enrollments using short aliases s and e
  3. Create a CTE called good_students with GPA > 8.5, then select from it
  4. Use a subquery to count students per city and alias it city_summary

Next topic?

Tell me:

  • Want to compare AS alias vs no alias performance / readability?
  • Deep dive into CTE (WITH clause) with many real examples?
  • How AS works with JOIN USING / JOIN ON?
  • Or move to another small-but-important keyword (DISTINCT, ALL, CAST)?

Your teacher is ready — what’s next? 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *