The SELECTstatement would then look like: SELECT LastName FROM Customers and the resulting data would be: LastName Smith Lopez Harper If you want to select more than one, but not all col
Trang 1Specifying Columns
So far, we’ve done nothing more than simply display all the data in a table But what if you wanted to select only certain columns? Working from the same table, you might want to display only the customer’s last name, for example The SELECTstatement would then look like:
SELECT LastName
FROM Customers
and the resulting data would be:
LastName
Smith
Lopez
Harper
If you want to select more than one, but not all columns, the SELECT might look like:
SELECT
FirstName,
LastName
FROM Customers
and the output would appear as:
FirstName LastName
William Smith
Natalie Lopez
Brenda Harper
The general format of this statement is:
SELECT columnlist
FROM table
The important thing to remember is that if you need to specify more than one
column in the columnlist, then those columns must be separated by commas.
Chapter 2 ■ Basic Data Retrieval
16
Trang 2Also notice that we placed each column in the columnlist on separate lines This
was done to improve readability
Column Names with Embedded Spaces
What if a column contains a space in its name? Let’s say, for example, that the
LastName column was specified as Last Name instead (with a space inserted
between the two words) Clearly, the following would not work:
SELECT
Last Name
FROM Customers
This statement would be considered invalid since Last and Name are not column
names And even if Last and Name were proper column names, they would need
to be separated by a comma The solution is to use a special character around any
column name containing spaces The character to use differs, depending on
which database you’re using For Microsoft SQL Server, the characters to use are
square brackets, and the proper syntax is:
SELECT
[Last Name]
FROM Customers
One additional syntax note: Just as keywords are not case sensitive, it’s also true
that table and column names are not case sensitive For example, the previous
example is identical to:
select
[last name]
from customers
For clarity’s sake, I will print all keywords in all caps and capitalize table and
column names in this book, but that is not truly necessary
D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e
For MySQL, the character to use around column names containing spaces is an accent grave (`) The
syntax for the above example is:
SELECT
`Last Name`
FROM Customers;
Column Names with Embedded Spaces 17
Trang 3For Oracle, the character to use around column names containing spaces is the double quote The syntax for the example is:
SELECT
"Last Name"
FROM Customers;
Additionally, unlike Microsoft SQL Server and MySQL, column names surrounded by double quotes are case sensitive That means that the previous statement is not equivalent to:
SELECT
"LAST NAME"
FROM Customers;
Looking Ahead
In this chapter, we’ve begun our exploration of how to use the SELECT state-ment to retrieve data We learned about basic syntax and have seen how to select specific columns In reality, however, this allows us to accomplish very little of a practical nature Most significantly, we have not yet learned how to apply any type of selection criteria to our data retrieval efforts For example, while we know how to select all customers, we don’t yet know how to only select customers from the state of New York
As it happens, I won’t be covering selection criteria until Chapter 7 What will we
be doing until then? In the next few chapters, we’re going to build on what can be
done with the columnlist component of the SELECT statement In the next chapter, I’ll be moving on to more variations on column selection, allowing us to create complex calculations in a single column I’ll also be talking about ways to rename columns to make them more descriptive
Similarly, Chapters 4 through 6 will build on your ability to create even more
complex and powerful columnlists, so when we finally get to the topic of selection
criteria, you will have a full arsenal of techniques available at your disposal
Chapter 2 ■ Basic Data Retrieval
18
Trang 4chapter 3
Calculations and
Aliases
Keyword Introduced: AS
The topics covered in this chapter will allow you to present information in a more convenient and interesting format for anyone viewing your data The main
technique to be discussed is known as calculated fields This technique will allow
you to perform calculations on individual data items that you retrieve from a database
Using this approach, customer names can be formatted exactly as desired Numeric calculations specific to your business or organization can be made and presented As a SQL developer, you often need the ability to customize the con-tent of individual columns in order to successfully turn data into more intelligent content Calculated fields are very useful tools that can help you accomplish that goal
Calculated Fields
When selecting data from a table, you are not restricted to the columns that happen to be in the table The concept of calculated fields allows for a number of other possibilities With calculated fields, you can do the following:
■ Select specific words or values
■ Perform calculations on single or multiple columns
■ Combine columns and literal values together
19
Trang 5Let’s look at a few examples, all coming from this Orders table:
OrderID FirstName LastName QuantityPurchased PricePerItem
Literal Values
Our first example of a calculated field isn’t really a calculation at all We’re going
to select a specific value as a column, even though the literal value has nothing to
do with data in the table This type of expression is called a literal value Here’s an
example:
SELECT
'First Name: ',
FirstName
FROM Orders
This statement will return this data:
(no column name) FirstName
First Name: William
First Name: Natalie
First Name: Brenda
In this statement, we are selecting two data items The first is the literal value 'First Name:' Note that single quote marks are used to indicate that this is a literal with character data The second data item is the FirstName column Notice two things First, the literal ‘First Name’ is repeated on every row Second, there is no header information for the first column When run in Microsoft SQL Server, the column header displays (‘‘no column name’’) The reason why there is
no header is simply because this is a calculated field There isn’t a column name that can be used for the header
Chapter 3 ■ Calculations and Aliases
20