Previous | Table of Contents | Next |
In the first chapter you learned that a query is a question that you ask the server by using SQL, and that a result is the answer the server provides to your question. You also got to see all the pieces of the SELECT statement. In this chapter, you learn how to retrieve data from the SQL Server by using a few parts (or clauses) of the SELECT statement. Today you learn about the following topics:
There is only one way to use SQL to get data from a SQL Server, and that is to use the SELECT statement. As you go through these chapters, more pieces of the SELECT statement will be brought together to form very complex queries. Ill start out with the SELECT clause, and then Ill move on to the FROM clause.
The simplest form of a SELECT returns one column and one row, for example
select 1 + 1
--------- 2
The server performs the addition and returns the number 2 in response to this query. The query you typed (select 1 + 1) is still on the Query tab, and the answer, 2, is on the Results tab.
A SELECT will always return a result set. The result sets appear on the Results tab of the ISQL/w interface.
A result set is a set of data returned by the server in response to a SELECT query.
For each SELECT that you issue, you receive a single result set. These result sets are composed of columns and rows. The result set may be empty, which means that it contains no rows. Each result set must contain at least one column.
Note: Other terms for a column are attribute and field. Other terms for a row are instance, tuple, and record. Ive always liked column and row, but youll occasionally see tuple pop up in a trade magazine.
A Note About Query Formatting
The examples in this book use a particular style of formatting for the SQL queries. Many companies enforce a particular set of syntax on query formatting so that everyones queries look the same, and also so that the person reading the query knows where to find everything.
This is a good thing. However, the server is not so picky. All it requires is that you separate words with some kind of white space. Things that qualify as white space include spaces
select title, title_id from titles
tabs
select title, title_id from titles
and carriage returns, or, on PCs, a Carriage Return plus a Linefeed character (hereafter referred to as CR/LF):
select title from titles
Two predominant styles of SQL formatting are in use today. The first, called the one-line, tab format, is used in this book. Its rules are simple. Here is a formatting example of a query youre likely to see in this book:
select name, cust_id, phone from customers
First rule: Each clause in the SQL statement gets its own line. So, in the previous example, SELECT is on the first line and FROM is on the second.
Second rule: Lists are separated by a tab from the SQL keyword and from each other by commas. Lists follow the SQL predicates. In the following example, a SELECT list (or column list) contains the title, title_id, price, and au_lname columns; a FROM list (or table list) contains the titles, authors, and titleauthor tables. This example also contains a WHERE predicate and a WHERE list.
Dont worry if you dont understand all the parts in the following exampleI havent talked yet about using more than one table or about using the WHERE clause. Youll get to these things shortly. For now, just look at the way the queries look on the page and imagine what they would look like on your screen:
select title, title_id, price, au_lname from titles, authors, titleauthor where titles.title_id = titleauthor.title_id and titleauthor.au_id = authors.au_id
This formatting style makes the query a little easier to read because the lists are all lined up with each other. Users reading the query can see where the FROM clause begins, where the WHERE clause begins, and so on. Once theyve found the piece for which theyre searching, its easy to read across through the list of columns, tables, or WHERE conditions.
The second major formatting scheme, multiline tab, is like the one used in the book, but further stipulates that each list is separated by a comma and a CR/LF:
select title, title_id, price, au_lname from titles, authors, titleauthor where titles.title_id = titleauthor.title_id and titleauthor.au_id = authors.au_id
This style has the benefits of the first insofar as the pieces of the query are easy to findthe SELECT, the FROM, and the WHERE. Instead of listing stuff out to the right, each piece gets its own line. Its more of a hassle to type and takes up more space in a book (and on a screen). Its also less dense than the style used in this book, but ultimately it all looks the same to the server.
Previous | Table of Contents | Next |