Coding Ref

SQL aliases

SQL aliases

In SQL, an alias is a temporary name that is given to a table or column. Aliases are typically used to make column names more readable or to create abbreviations for table names.

  • If you have a table called "Customer" and you want to refer to it as "C" in your query, you can use the following alias: SELECT * FROM Customer AS C
  • If you have a column called "TotalSales" in a table called "SalesData" and you want to refer to it as "TS" in your query, you can use the following alias: SELECT TS FROM SalesData AS TS

Aliases are often used in JOIN clauses to give the joined tables different names in the same query.

Examples

Example #1

Here are some examples of how aliases can be used in SQL:

SELECT c.customer_id, c.first_name, c.last_name
FROM customers c;

In this example, the c alias is used for the customers table, which makes the query more concise and easier to read.

Example #2

SELECT *
FROM Customer AS C
JOIN SalesData AS SD
ON C.CustomerID = SD.CustomerID

In this query, the Customer table is given the alias "C" and the SalesData table is given the alias "SD". This allows the query to reference the columns in each table using the corresponding alias.

Overall, aliases can make your SQL queries more readable and easier to understand. They can also make it easier to work with long or complex table and column names, by allowing you to use more concise aliases in your queries.

Conclusion

In SQL, an alias is a temporary name that is given to a table or column. Aliases are typically used to make column names more readable or to create abbreviations for table names.

You'll also like

Related tutorials curated for you

    What is SQL ANY?

    How to get the day of the week in SQL

    How to combine two columns in SQL

    What is SQL ALL?

    How to fix the 'Ambiguous Column Name' error in SQL

    SQL aliases

    What is cardinality in SQL?

    SQL Comments

    What is the unique constraint in SQL?

    How to concatenate strings in SQL

    Block comments in SQL

    Find column names in SQL