Datasheet

statement, for example. SQL statements are often made up of several parts, which are known as clauses.
SQL statements may also contain embedded (nested) statements known as subqueries.
SQL statements are executed, and may return one or more results. Multiple statements can be executed
as a batch, which simply means “execute statements sequentially in the order that they are written.” In
some SQL dialects, a semicolon (
;) is used to signify the end of a statement, although that is generally
not required, because the context is enough to tell where one statement ends and the next begins.
Retrieving Data from Tables
Data retrieval is, as you’ve already seen, the job of the SELECT keyword. There are a huge number of
ways to write select statements, involving a multitude of additional keywords and techniques, and the
basic result is the same in all cases — you obtain a single value (a scalar result) or zero or more rows
of data. The data that’s returned may not be in the same format as the data in the database, because
columns may be retrieved from multiple tables, combined in some way, renamed, or processed by func-
tions before reaching you.
Here’s the simplest form that a select statement can take:
SELECT [Column(s)] FROM [Table]
In it, [Column(s)] is a comma-separated list of column names and [Table] is the table containing the
columns. For example:
SELECT EntryName, PhoneNumber FROM PhoneBookEntry
This retrieves a set of rows consisting of two columns, EntryName and PhoneNumber, from a table called
PhoneBookEntry. This is shown graphically in Figure 1-4.
Figure 1-4: PhoneBookEntry query result set
In some circumstances, column or table names may match SQL keywords —
Name, for instance, is a
SQL keyword and might be used as a column name. To solve this ambiguity, SQL Server enables you to
use square brackets to signify that the enclosed text should not be interpreted as a SQL keyword. You
could use the text
[Name] in a query to refer to a column called Name, for example. Nevertheless, it is
good practice not to use SQL keywords to name columns or tables so that this is not an issue.
Often you will want to retrieve all of the data in all of the rows in a table; in that case, you can use the
shorthand
* to refer to all columns. For example:
SELECT * FROM PhoneBookEntry
19
Database Fundamentals
44063c01.qxd:WroxBeg 9/12/06 10:31 PM Page 19