User Guide

Table Of Contents
Using SQL 457
However, this query does not to return the table rows in alphabetical order. You can include an
ORDER clause in the SQL, as follows:
SELECT the FirstName, LastName, Phone
FROM employees
ORDER BY LastName, FirstName
Using column aliases
You might have column names that you do not want to retain in the results of your SQL
statement. For example, your database is set up with a column that uses a reserved word in
ColdFusion, such as EQ. In this case, you can rename the column as part of the query, as follows:
SELECT EmpID, LastName, EQ as MyEQ FROM employees
The results returned by this query contains columns named EmpID, LastName, and MyEQ.
Accessing multiple tables
In a database, you can have multiple tables containing related information. You can extract
information from multiple tables as part of a query. In this case, you specify multiple table names
in the SELECT statement, as follows:
SELECT LastName, FirstName, Street, City, State, Zip
FROM employees, addresses
WHERE employees.EmpID = addresses.EmpID
ORDER BY LastName, FirstName
This SELECT statement uses the EmpID field to connect the two tables. This query prefixes the
EmpID column with the table name. This is necessary because each table has a column named
EmpID. You must prefix a column name with its table name if the column name appears in
multiple tables.
In this case, you extract LastName and FirstName information from the employees table and
Street, City, State, and Zip information from the addresses table. You can use output such as this
is to generate mailing addresses for an employee newsletter.
The results of a SELECT statement that references multiple tables is a single result table
containing a join of the information from corresponding rows. A join means information from
two or more rows is combined to form a single row of the result. In this case, the resultant record
set has the following structure:
What is interesting in this result is that even though you used the EmpID field to combine
information from the two tables, you did not include that field in the output.