An alias is just that, an alternative name for a field or value.
Aliases are assigned with the AS keyword.
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable.
SELECT column_name AS alias_name FROM table_name; |
SELECT column_name(s) FROM table_name AS alias_name; |
Below is a selection from the "Product" table in the DB2 database.
ProductID | ProductName | Category | Price | QtyOnHand | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | PEN | 7.45 | 10 | 74.50 | |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 | ||
7009 | Amplifier | Components | 25.00 | ||
7010 | Headphone | Accessories | 100.00 |
The following SQL statement creates two aliases, one for the ProductID column and one for the ProductName column:
SELECT ProductID AS ID, ProductName AS Product FROM Product; |
The result of this query is as follows.
ID | Product |
---|---|
7001 | Mouse |
7002 | Harddrive |
7003 | Keyboard |
7004 | RAM |
7005 | Honda |
7006 | PEN |
7007 | Cddrive |
7008 | Speaker |
7009 | Amplifier |
7010 | Headphone |
If the alias name contains spaces, It requires single quotation marks or square brackets.
SELECT CONCAT('IBM','MAINFRAMER') AS 'Combined Value' FROM SYSIBM.SYSDUMMY1; |
Here is the output:
Combined Value ------------- IBMMAINFRAMER |
The following SQL statement sellects all the products in product table. We use the "Product" table, and give the table aliases as "P" (Here we use aliases to make the SQL shorter):
This is simple example of Table Aliases. we mostly don't use aliases for single table query. Just for an understanding purpose We used aliases for single table query.
SELECT P.ProductID, P.ProductName FROM Product AS P; |
The result of this query is as follows.
ProductID | ProductName |
---|---|
7001 | Mouse |
7002 | Harddrive |
7003 | Keyboard |
7004 | RAM |
7005 | Honda |
7006 | PEN |
7007 | Cddrive |
7008 | Speaker |
7009 | Amplifier |
7010 | Headphone |
Aliases can be useful when:
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!