• Home
  • SQL Aliases

    In SQL, aliases are used to assign temporary names to tables or columns in a query. Aliases can make the query more readable and concise, especially when dealing with complex queries or when joining multiple tables. The `AS` keyword is typically used to specify an alias.


    Here are examples of using aliases in SQL:


    1. Alias for a table:

    sql
    SELECT c.name, c.age
    FROM customers AS c

    In this example, the `AS c` assigns the alias "c" to the "customers" table. This allows us to refer to the table as "c" in the rest of the query, simplifying the syntax.


    2. Alias for a column:

    sql
    SELECT c.name AS customer_name, c.age AS customer_age
    FROM customers AS c

    In this example, we assign aliases "customer_name" and "customer_age" to the "name" and "age" columns, respectively. This allows us to refer to the columns with more descriptive names in the result set.


    3. Alias for a calculated expression:

    sql
    SELECT c.name, c.age, c.age * 2 AS double_age
    FROM customers AS c

    In this example, we calculate the double of the "age" column and assign it the alias "double_age". This allows us to include the calculated expression in the result set with a meaningful name.


    Aliases can be used in various parts of a SQL query, including the SELECT clause, FROM clause, WHERE clause, JOINs, and subqueries. They are especially helpful when using self-joins or when there are multiple tables with similar column names.

    Note that not all database systems require the use of the `AS` keyword for aliases. Some database systems allow you to directly assign the alias without the `AS` keyword. For example, you can write `FROM customers c` instead of `FROM customers AS c`.

    Using aliases can improve the readability and maintainability of your SQL queries, making them easier to understand and work with.



    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext