IBM Books

SQL Getting Started


Naming Expressions

The optional AS clause lets you assign a meaningful name to an expression, which makes referring back to the expression easier. You can use an AS clause to provide a name for any item in the select list.

The following statement displays all employees whose salary plus commission is less than $13, 000. The expression SALARY + COMM is named PAY:

     SELECT NAME, JOB, SALARY + COMM AS PAY
        FROM STAFF
        WHERE (SALARY + COMM) < 13000
        ORDER BY PAY

This statement produces the following result:

     NAME      JOB   PAY       
     --------- ----- ----------
     Yamaguchi Clerk   10581.50
     Burke     Clerk   11043.50
     Scoutten  Clerk   11592.80
     Abrahams  Clerk   12246.25
     Kermisch  Clerk   12368.60
     Ngan      Clerk   12714.80

By using the AS clause, you can refer to a particular column name rather than the system generated number in the ORDER BY clause. In this example we compare (SALARY + COMM) with 13000 in the WHERE clause, instead of using the name PAY. This is a result of the order of operations. The WHERE clause is evaluated before (SALARY + COMM) is given the name PAY. Hence, PAY cannot be used in the predicate.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]