Previous | Table of Contents | Next |
You may have noticed that there are two authors listed on this book. David wrote some of the advanced lessons that appear later in this book. He owns a company called metis technologies, llc. It means doing something really clever instead of by brute force in Greek. I own a company called Geist, LLC. Geisteblitz is German for bursts of genius. Geist means ghost, spirit, or mind. In naming a technology company, its tough to find an English word that isnt already taken.
Besides getting together to drink beer, do some training and consulting, and talk about the next killer app, our companies also codeveloped a very simple Web interface to access SQL Server. If you do not have a SQL Server at work that you can access, or if your administrator is leery of you learning SQL on his production machine, you can use our interface to run all the examples and exercises in this book.
metis maintains a Web site at http://www.metis.com. In order to use the interface, youll need a Web browser. All you need to do is head over to metis.com, and well have instructions ready for you there.
Warning: Our plan is to keep the Web interface up as long as humanly possible. If youre reading this book sometime around the turn of the millennium, theres a chance David has been run over by a bus and his Web site is no longer running. (Hey, SQL Server consulting is a dangerous business.) On the off chance that the site isnt up anymore, please accept my apologiesyoull need a server of your own.
OK! Now, youre ready to proceed. Go on to Your First Query. This is where you finally start interacting with the SQL Server.
Lets start by defining a couple of important terms.
A query is a question that you ask the server, using SQL. A result is the answer to that question.
In order to get information out of a SQL Server, you start queries off with the SELECT keyword. Click in the Query box; then type what appears in the following code listing. Output for this query will appear on your Results tab (or in the Results window, for Web users). When code listings are given in the book, I list the query first, the results second, and then an analysis of what went on. Here is your first query. I explain how to run it in the analysis section:
input select au_lname from authors
au_lname ---------------------------------------- White Green Carson OLeary Straight Smith Bennet Dull Gringlesby Locksley Greene Blotchet-Halls Yokomoto del Castillo DeFrance Stringer MacFeather Karsen Panteley Hunter McBadden Ringer Ringer (23 row(s) affected)
This query asks the server to find a table named authors and retrieve all the data in the au_lname column. The authors table in the pubs database keeps track of information about all the authors who have written books. The au_lname column contains the authors last names. This query retrieves all the last names for all the authors. Youll learn all about the intricacies of SELECT and FROM tomorrow.
At this point, you havent interacted with the server beyond logging in. All your typing and editing are done without the help of the SQL Server. When youre finished entering your first query, click the little green arrow (triangle) in the upper-right corner. This submits your query to the server.
Then one of two things happened: it worked, or it didnt. If it didnt work, you probably got an error message. If you want to figure it out on your own, please do. If you want some help with the error message, Ive listed a few of the most common ones at the end of tomorrows chapter. It wont hurt you to skip ahead to that section for a little extra help.
The query executed OK, and now youre looking at some data. Results are placed on the Results tab of ISQL/w. At the top is the column name. Each column in a table must have a name, and it is this name you request when you submit a query. In this case, you asked for the au_lname column.
The ISQL/w program places a dashed line under the column heading to separate it from the data. Then, it lists the last names, one to a line, until all the names have been listed.
Finally, at the bottom, it reports how many rows of data it returned (in this case, 23 rows). If you have a different version of the pubs database installed on your server, or if it has been there a while and other users have added and deleted rows from tables, you may have gotten something different.
All the data wont fit on the screen. To scroll down to see more rows, use the scrollbar on the right. To scroll over to see more columns, use the scrollbar at the bottom of the screen.
The first week is all about the SELECT statement. Tomorrow, youll learn about the SELECT keyword as well as the things that can appear after it, the FROM keyword, and the ORDER BY keyword. On Day 3, you learn how to restrict rows in the result set with WHERE. On Days 4 and 5, Ill talk a little about manipulating columns and getting aggregate information from a table, such as the minimum value in a column or the number of rows in a table. On Day 6, youll finish up your work with SELECT by learning about the GROUP BY and HAVING clauses.
Here is a SELECT statement that incorporates all the available clauses. For books costing over $8.00, it returns the average price of the books in that type as well as the number of books in that type. In order to qualify, a book type must include at least two books that cost over $8:
select type, avg(price), count(title_id) from titles where price > $8 group by type having count(title_id) > 1 order by avg(price)
Here is the syntax statement for the SELECT statement:
select [DISTINCT] <column-list> from <table list> where <Boolean condition test decides which rows to use in calculations> group by <Column in the column-list> having <Boolean condition test decides which rows to return after aggregates run> order by <column-list>
Previous | Table of Contents | Next |