How to Use Aliases for Table Names

Use aliases for table names

Defining a join without table aliases

SELECT orderid, orderdate, companyname FROM orders JOIN customers ON orders.customerid = customers.customerid

Defining a join with table aliases

SELECT orderid, orderdate, companyname FROM orders AS o JOIN customers AS c ON o.customerid = c.customerid

Introduction

Partial syntax for table aliases illegal for non-trainer use

Use aliases for table names, to simplify complex query statements.

Using aliases for table names makes Transact-SQL scripts easier to read and to maintain.

You can replace a long and complex fully qualified table name with a simple, abbreviated alias name when writing scripts. You use an alias name in place of the full table name.

The syntax for table aliases is as follows:

SELECT select_list

FROM table_source AS table_alias

Example of defining a join without using table aliases

This example performs a join on the Orders and Customers tables. The example does not use table aliases. This means that you must write the names of the Orders and Customers tables in full in the join condition.

/* Perform a join without using table aliases */ USE northwind

SELECT orderid, orderdate, companyname FROM orders JOIN customers ON orders.customerid = customers.customerid

Example of defining a join using table aliases

This example performs the same join as the previous example. However, this example uses aliases for the Orders and Customers tables. The aliases are used to simplify the join condition.

/* Perform a join using table aliases */ USE northwind

SELECT orderid, orderdate, companyname FROM orders AS o JOIN customers AS c ON o.customerid = c.customeridGO

0 0

Post a comment