Using the SELECT Statement
The most common of all SQL queries is the SELECT query. This query is generally
constructed using a SELECT clause and a FROM clause. To understand this concept
more clearly, take a look at the following statement, which retrieves all columns
of all records in the Departments table:
FROM Departments
In this case, the SELECT clause lists the columns that you want to retrieve. In this
case, we used *, which means all columns. The FROM clause specifies the table
from which you want to pull the records. Together, these two clauses create an
SQL statement that extracts all data from the Departments table.
Youve probably noticed that the two clauses appear on separate lines. If you
wanted to keep the entire statement on one line, thats fine, but SQL lets you
separate the statements on multiple lines to make complex queries easier to read.
Also note that although SQL is not actually a case-sensitive language, well capit-
alize the keywords (such as SELECT and FROM) according to the popular convention.
To sum up, heres the basic syntax used in a SELECT query:
This keyword indicates that we want to retrieve data, rather than modify,
add, or delete datathese activities use the UPDATE, INSERT, and DELETE
keywords, respectively, in place of SELECT.
We must provide the names of one or more columns in the database table
from which we want to retrieve data. We can list multiple columns by separ-
ating the column names with commas, or we can use * to select all columns.
We can also prefix each column name with the table name, as shown here:
SELECT Employees.Name, Employees.Username
FROM Employees.Name
This approach is mandatory when two or more of the tables were dealing
with contain columns that have the same names. Well learn to read data
from multiple tables a little later in the chapter.
Using the SELECT Statement
The FROM keyword ends the SELECT clause and starts the FROM clause, which
identifies the tables from which the data will be extracted. This clause is re-
quired in all SELECT statements.
We need to identify the names of the tables from which we want to extract
data. To list multiple tables, separate their names with commas. Querying
multiple tables is called a table joinwell cover this a bit later.
Armed with this knowledge, we can see that the preceding sample statement
would retrieve all records from the Departments table, producing a set of results
like that shown in Figure 8.4.
Figure 8.4. Reading the list of departments
See how easy it is? The SELECT query is probably the one youll use most.
The Number of Affected Rows
As you can see in Figure 8.4, SQL Server reports the number of records that
have been affected by a certain query. This report doesnt indicate that those
records were modified. Instead, the figure represents the number of rows
that were read, modified, deleted, or inserted by a certain query.
Chapter 8: Speaking SQL

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.