Coding Ref

What is the AS statement in SQL?

What is the AS statement in SQL?

In SQL, the AS keyword is used to specify an alias for a table or a column in a query. An alias is a temporary name given to a table or a column, and it can be used to make the code easier to read or to create a shorter version of a longer name.

Here are a few examples of how the AS keyword is used:

  • To specify an alias for a table, you can use the following syntax: SELECT * FROM TableName AS AliasName
  • To specify an alias for a column, you can use the following syntax: SELECT ColumnName AS AliasName FROM TableName

In both cases, the AS keyword is used to specify the alias for the table or column.

Here are some examples that illustrate how this works:

  • To give the Customer table the alias "C", you can use the following query: SELECT * FROM Customer AS C
  • To give the TotalSales column in the SalesData table the alias "TS", you can use the following query: SELECT TS FROM SalesData AS TS

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

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.

Conclusion

In SQL, the AS keyword is used to specify an alias for a table or a column in a query. An alias is a temporary name given to a table or a column, and it can be used to make the code easier to read or to create a shorter version of a longer name.

You'll also like

Related tutorials curated for you

    What is the AS statement in SQL?

    What is SQL ANY?

    What is SQL ALL?

    How to get the day of the week in SQL

    Calculating averages in SQL

    Filtering in GraphQL

    Find column names in SQL

    What is cardinality in SQL?

    SQL aliases

    What is a blind SQL injection?

    How to concatenate strings in SQL

    What is the unique constraint in SQL?