Skip to main content
    Skip to main content
    SQL
    Aliases
    Beginner

    SQL Aliases Explained: AS for Tables and Columns

    SQiLs TeamMarch 23, 2026
    SQL Aliases Explained: AS for Tables and Columns

    SQL aliases make every query you write easier to read and faster to type. The AS keyword lets you give temporary names to columns and tables, making your output cleaner and your multi-table queries far more manageable.

    In this guide, we'll cover everything you need to know about SQL aliases with practical examples using the Chinook database (a digital music store with artists, albums, tracks, invoices, and customers).

    Table of Contents

    What Is a SQL Alias?

    A SQL alias is a temporary name you assign to a column or table for the duration of a query. Aliases exist only while the query runs — they don't rename anything permanently in the database.

    The basic syntax uses the AS keyword:

    -- Column alias
    SELECT column_name AS alias_name
    FROM table_name;
    
    -- Table alias
    SELECT t.column_name
    FROM table_name AS t;
    

    Good to know: The AS keyword is technically optional in most databases. SELECT Name artist_name FROM artists works the same as SELECT Name AS artist_name FROM artists. However, always include AS for readability — it makes your intent crystal clear.


    Column Aliases

    Column aliases rename the output columns in your result set. This is useful when the original column name is unclear, when you're computing a value, or when you need a presentation-ready label.

    Example: Renaming Columns for Clarity

    SELECT
      FirstName AS first_name,
      LastName AS last_name,
      Email AS contact_email
    FROM customers
    LIMIT 5;
    
    first_namelast_namecontact_email
    LuísGonçalvesluisg@embraer.com.br
    LeonieKöhlerleonekohler@surfeu.de
    FrançoisTremblayftremblay@gmail.com
    BjørnHansenbjorn.hansen@yahoo.no
    FrantišekWichterlováfrantisekw@jetbrains.com

    Without aliases, the columns would display as FirstName, LastName, and Email — which may not match the lowercase convention your application or report expects.

    Example: Aliases for Computed Columns

    When you create a calculated column, it has no name by default. Aliases give it one:

    SELECT
      Name AS track_name,
      Milliseconds / 1000 AS duration_seconds,
      Bytes / 1048576.0 AS size_mb
    FROM tracks
    LIMIT 5;
    
    track_nameduration_secondssize_mb
    For Those About To Rock (We Salute You)34310.97
    Balls to the Wall3425.26
    Fast As a Shark2303.86
    Restless and Wild2524.27
    Princess of the Dawn3756.29

    Without the AS duration_seconds alias, the column header would show Milliseconds / 1000 — not exactly user-friendly.

    Example: Aliases with String Concatenation

    SELECT
      FirstName || ' ' || LastName AS full_name,
      City || ', ' || Country AS location
    FROM customers
    LIMIT 5;
    
    full_namelocation
    Luís GonçalvesSão José dos Campos, Brazil
    Leonie KöhlerStuttgart, Germany
    François TremblayMontréal, Canada
    Bjørn HansenOslo, Norway
    František WichterlováPrague, Czech Republic

    Try it yourselfPractice column aliases with customer data →


    Table Aliases

    Table aliases give short names to tables, reducing the amount of typing in your queries. They become essential once you start joining multiple tables.

    Example: Simple Table Alias

    SELECT
      t.Name AS track_name,
      t.Composer AS composer
    FROM tracks AS t
    WHERE t.Composer IS NOT NULL
    LIMIT 5;
    
    track_namecomposer
    For Those About To Rock (We Salute You)Angus Young, Malcolm Young, Brian Johnson
    Fast As a SharkF. Baltes, S. Kaufman, U. Dirkschneider
    Restless and WildF. Baltes, R.A. Smith-Diesel, S. Kaufman
    Princess of the DawnDeaffy, R.A. Smith-Diesel
    Put The Finger On YouAngus Young, Malcolm Young, Brian Johnson

    Here, t is a short alias for tracks. Instead of writing tracks.Name, you write t.Name. The savings add up fast in complex queries.

    Common Table Alias Conventions

    TableCommon Alias
    customersc
    invoicesi
    invoice_itemsii
    trackst
    albumsal
    artistsar
    genresg
    employeese

    Pick short, meaningful abbreviations. Single letters work well for simple queries; two-letter abbreviations help when multiple tables start with the same letter (like albums and artists).


    Aliases in JOINs

    Table aliases truly shine when you're writing JOIN queries. Without them, multi-table queries become painfully verbose.

    Example: Without Aliases (Verbose)

    SELECT
      artists.Name,
      albums.Title
    FROM artists
    INNER JOIN albums ON artists.ArtistId = albums.ArtistId
    LIMIT 5;
    

    Example: With Aliases (Clean)

    SELECT
      ar.Name AS artist_name,
      al.Title AS album_title
    FROM artists AS ar
    INNER JOIN albums AS al ON ar.ArtistId = al.ArtistId
    LIMIT 5;
    
    artist_namealbum_title
    AC/DCFor Those About To Rock We Salute You
    AcceptBalls to the Wall
    AcceptRestless and Wild
    AC/DCLet There Be Rock
    AerosmithBig Ones

    Both queries return identical results, but the aliased version is shorter, more readable, and much easier to modify.

    Example: Multi-Table JOIN with Aliases

    When you're joining three or more tables, aliases keep the query manageable:

    SELECT
      c.FirstName || ' ' || c.LastName AS customer,
      i.InvoiceDate AS purchase_date,
      t.Name AS track_name
    FROM invoices AS i
    INNER JOIN customers AS c ON i.CustomerId = c.CustomerId
    INNER JOIN invoice_items AS ii ON i.InvoiceId = ii.InvoiceId
    INNER JOIN tracks AS t ON ii.TrackId = t.TrackId
    LIMIT 5;
    
    customerpurchase_datetrack_name
    Luís Gonçalves2021-01-01Balls to the Wall
    Luís Gonçalves2021-01-01Restless and Wild
    Leonie Köhler2021-01-02Put The Finger On You
    Leonie Köhler2021-01-02Inject The Venom
    Leonie Köhler2021-01-02Evil Walks

    Imagine writing invoice_items.InvoiceId and customers.CustomerId every time — aliases save you from that. For a deeper dive on combining tables, see our complete JOINs guide.

    Try it yourselfPractice multi-table JOINs with aliases →


    Aliases with Aggregate Functions

    Aggregate functions like COUNT(), SUM(), and AVG() produce calculated columns that have no natural name. Aliases are essential here. For more on aggregation, check out our GROUP BY and HAVING guide.

    Example: Counting Tracks per Genre

    SELECT
      g.Name AS genre,
      COUNT(t.TrackId) AS track_count
    FROM genres AS g
    INNER JOIN tracks AS t ON g.GenreId = t.GenreId
    GROUP BY g.Name
    ORDER BY track_count DESC
    LIMIT 5;
    
    genretrack_count
    Rock1297
    Latin579
    Metal374
    Alternative & Punk332
    Jazz130

    Without the AS track_count alias, the column would display as COUNT(t.TrackId) — confusing for anyone reading a report or dashboard.

    Example: Revenue Summary with Aliases

    SELECT
      c.Country AS country,
      COUNT(i.InvoiceId) AS total_orders,
      ROUND(SUM(i.Total), 2) AS total_revenue,
      ROUND(AVG(i.Total), 2) AS avg_order_value
    FROM customers AS c
    INNER JOIN invoices AS i ON c.CustomerId = i.CustomerId
    GROUP BY c.Country
    ORDER BY total_revenue DESC
    LIMIT 5;
    
    countrytotal_orderstotal_revenueavg_order_value
    USA91523.065.75
    Canada56303.965.43
    France35195.105.57
    Brazil35190.105.43
    Germany28156.485.59

    Notice how every alias in the SELECT is reused in the ORDER BY clause — ORDER BY total_revenue DESC. This is one of the practical benefits of column aliases: you can reference them in ORDER BY without repeating the expression.

    Try it yourselfPractice aggregate queries on the Chinook database →


    Cheat Sheet

    What You WantSyntaxExample
    Rename a columnSELECT col AS aliasSELECT Name AS track_name
    Name a computed columnSELECT expr AS aliasSELECT Bytes / 1024 AS size_kb
    Alias with spacesSELECT col AS "alias name"SELECT Name AS "Track Name"
    Shorten a table nameFROM table AS tFROM tracks AS t
    Alias in a JOINJOIN table AS t ON ...JOIN albums AS al ON ...
    Use alias in ORDER BYORDER BY aliasORDER BY track_count DESC
    Alias in aggregateSELECT COUNT(*) AS aliasSELECT COUNT(*) AS total

    Common Mistakes to Avoid

    1. Using a Column Alias in WHERE

    SQL processes the WHERE clause before SELECT, so column aliases don't exist yet at that stage:

    -- This will NOT work
    SELECT Milliseconds / 1000 AS duration_sec
    FROM tracks
    WHERE duration_sec > 300;
    
    -- Do this instead: repeat the expression
    SELECT Milliseconds / 1000 AS duration_sec
    FROM tracks
    WHERE Milliseconds / 1000 > 300;
    

    You can use column aliases in ORDER BY and (in most databases) HAVING, but never in WHERE.

    2. Forgetting Aliases in Self-Joins

    When a table is joined to itself, aliases are required — otherwise the database can't tell the two copies apart:

    -- Required: use aliases to distinguish the two copies
    SELECT
      e.FirstName || ' ' || e.LastName AS employee,
      m.FirstName || ' ' || m.LastName AS manager
    FROM employees AS e
    LEFT JOIN employees AS m ON e.ReportsTo = m.EmployeeId
    LIMIT 5;
    

    3. Using Reserved Words as Aliases

    Avoid naming your alias something that clashes with SQL keywords like SELECT, FROM, ORDER, GROUP, or TABLE. If you must, wrap it in double quotes: AS "order". Better yet, choose a different name: AS order_id.

    4. Inconsistent Alias Conventions

    Mixing styles — customer_name in one column and AlbumTitle in another — creates confusing output. Pick a convention (typically snake_case for aliases) and stick with it throughout your query.


    Practice Exercises

    Put your alias knowledge to work with these hands-on exercises on SQiLs — no setup required, just write SQL in your browser:

    ExerciseWhat You'll Practice
    List All Music Artist Names →Basic SELECT with column aliases
    Format Customer Names →String concatenation with aliases
    Calculate Track Duration in Minutes →Computed columns with aliases
    Average Song Length per Genre →Aggregate functions with table and column aliases
    Revenue Distribution by Country →GROUP BY with aliased aggregates
    Customer Names and Their Invoices →JOIN queries using table aliases

    Join 500+ SQL learners already practicing on SQiLs — Browse all exercises →


    Summary

    SQL aliases are a small syntax feature with a big impact on readability and productivity:

    • Column aliases (SELECT col AS alias) rename output columns and are essential for computed or aggregated values.
    • Table aliases (FROM table AS t) shorten table references and are critical in JOIN queries.
    • The AS keyword is optional but always recommended for clarity.
    • Aliases are temporary — they exist only for the duration of the query.
    • You can use column aliases in ORDER BY but not in WHERE.

    Start using aliases in every query you write. Your future self — and anyone reading your SQL — will thank you.

    Ready to practice? Start solving SQL exercises on SQiLs →


    Frequently Asked Questions

    Is the AS keyword required in SQL?

    No. In most SQL databases, AS is optional — SELECT Name artist_name works the same as SELECT Name AS artist_name. However, omitting AS can make your query harder to read, especially for table aliases. Best practice is to always include it.

    Can I use a column alias in a WHERE clause?

    No. SQL evaluates the WHERE clause before SELECT, so the alias doesn't exist yet. You need to repeat the full expression in your WHERE condition. However, you can use column aliases in ORDER BY and, in most databases, in HAVING clauses. See our GROUP BY guide for more on HAVING.

    What is the difference between a column alias and a table alias?

    A column alias renames a column in the output (SELECT Name AS artist_name), while a table alias gives a table a shorthand reference for the query (FROM artists AS ar). Column aliases affect what you see in results; table aliases affect how you reference columns throughout the query. Both are temporary and exist only for that single query.

    When should I use table aliases?

    Always use table aliases when writing JOINs — they keep your query concise and readable. For single-table queries, aliases are optional but still helpful if the table name is long. In self-joins (where a table joins to itself), table aliases are required because the database needs a way to tell the two instances apart.

    Related Posts