Chapter 2. Beginning SQL Commands in SQL Server

In this chapter, we discuss how to write (build) simple SQL query statements in SQL Server 2005 using the SELECT statement. We examine how to retrieve data from a table by the use of SELECT statements, how to SELECT fields (columns) and rows from tables, how to use the ORDER BY and WHERE clauses, and how to use the AND, OR, and BETWEEN operators. The concept of COUNT and null values is also to be established. Then, to make writing queries simpler, we discuss how to use table and column aliases, table qualifiers, synonyms, and finally we present a convention for writing SQL statements.

Displaying Data with the SELECT Statement

One of the very first things that you would usually want to do with a set of tables (or a database) is to see what information the tables contain. To display the information in a table using a query, you use a SELECT command on the table. SELECT is usually the first word in a SQL statement or query. The SELECT statement returns information from a table (or a set of tables, the database) as a set of records, or a result set. The result set is a tabular arrangement of data, composed of rows and columns. The SELECT statement shows the output on the computer screen (as shown in Figures 1-26 and 1-28 of Chapter 1). It does not save the results. The simplest and most commonly used form of the SELECT syntax is:

SELECT fields (a.k.a. columns or attributes)
FROM Table

Here, Table is the name of the table from which the data ...

Get Learning SQL on SQL Server 2005 now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.