Next chapterContents

Teach Yourself SQL in 21 Days, Second Edition


A special thanks to the following individuals: foremost to my loving wife, Tina, for her tolerance and endless support, to Dan Wilson for his contributions, and to Thomas McCarthy at IUPUI. Also, thank you Jordan for your encouragement over the past few years.

-- Ryan K. Stephens

Special thanks to my wife for putting up with me through this busiest of times. I apologize to my mom for not seeing her as often as I should (I'll make it up to you). Also, thanks to my loyal dog, Toby. He was with me every night and wouldn't leave my side.

-- Ronald Plew

Special thanks to the following people: Jeff Perkins, David Blankenbeckler, Shannon Little, Jr., Clint and Linda Morgan, and Shannon and Kaye Little.

This book is dedicated to my beautiful wife, Becky. I am truly appreciative to you for your support, encouragement, and love. Thanks for staying up with me during all those late-night sessions. You are absolutely the best.

-- Bryan Morgan

Thanks to my family, Leslie, Laura, Kelly, Valerie, Jeff, Mom, and Dad. Their support made working on this book possible.

-- Jeff Perkins

About the Authors

Ryan K. Stephens

Ryan K. Stephens started using SQL as a programmer/analyst while serving on active duty in the Indiana Army National Guard. Hundreds of programs later, Ryan became a database administrator. He currently works for Unisys Federal Systems, where he is responsible for government-owned databases throughout the United States. In addition to his full-time job, Ryan teaches SQL and various database classes at Indiana University-Purdue University Indianapolis. He also serves part-time as a programmer for the Indiana Army National Guard. Along with Ron Plew and two others, Ryan owns a U.S. patent on a modified chess game. Some of his interests include active sports, chess, nature, and writing. Ryan lives in Indianapolis with his wife, Tina, and their three dogs, Bailey, Onyx, and Sugar.

Ronald R. Plew

Ronald R. Plew is a database administrator for Unisys Federal Systems. He holds a bachelor of science degree in business administration/management from the Indiana Institute of Technology. He is an instructor for Indiana University-Purdue University Indianapolis where he teaches SQL and various database classes. Ron also serves as a programmer for the Indiana Army National Guard. His hobbies include collecting Indy 500 racing memorabilia. He also owns and operates Plew's Indy 500 Museum. He lives in Indianapolis with his wife, Linda. They have four grown children (Leslie, Nancy, Angela, and Wendy) and eight grandchildren (Andy, Ryan, Holly, Morgan, Schyler, Heather, Gavin, and Regan).

Bryan Morgan

Bryan Morgan is a software developer with TASC, Inc., in Fort Walton Beach, Florida. In addition to writing code and chasing the golf balls he hits, Bryan has authored several books for Sams Publishing including Visual J++ Unleashed, Java Developer's Reference, and Teach Yourself ODBC Programming in 21 Days. He lives in Navarre, Florida, with his wife, Becky, and their daughter, Emma.

Jeff Perkins

Jeff Perkins is a senior software engineer with TYBRIN Corporation. He has been a program manager, team leader, project lead, technical lead, and analyst. A graduate of the United States Air Force Academy, he is a veteran with more than 2,500 hours of flying time as a navigator and bombardier in the B-52. He has co-authored three other books, Teach Yourself NT Workstation in 24 Hours, Teach Yourself ODBC Programming in 21 Days, and Teach Yourself ActiveX in 21 Days.

Tell Us What You Think!

As a reader, you are the most important critic and commentator of our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.

Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at

NOTE: If you have a technical question about this book, call the technical support line at 317-581-3833 or send e-mail to

As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book--as well as what we can do to make our books stronger. Here's the information:

FAX: 317-581-4669


Mail: Rosemarie Graham
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290


Who Should Read This Book?

Late one Friday afternoon your boss comes into your undersized cubicle and drops a new project on your desk. This project looks just like the others you have been working on except it includes ties to several databases. Recently your company decided to move away from homegrown, flat-file data and is now using a relational database. You have seen terms like SQL, tables, records, queries, and RDBMS, but you don't remember exactly what they all mean. You notice the due date on the program is three, no, make that two, weeks away. (Apparently it had been on your boss's desk for a week!) As you begin looking for definitions and sample code to put those definitions into context, you discover this book.

This book is for people who want to learn the fundamentals of Structured Query Language (SQL)--quickly. Through the use of countless examples, this book depicts all the major components of SQL as well as options that are available with various database implementations. You should be able to apply what you learn here to relational databases in a business setting.


The first 14 days of this book show you how to use SQL to incorporate the power of modern relational databases into your code. By the end of Week 1, you will be able to use basic SQL commands to retrieve selected data.

NOTE: If you are familiar with the basics and history of SQL, we suggest you skim the first week's chapters and begin in earnest with Day 8, "Manipulating Data."

At the end of Week 2, you will be able to use the more advanced features of SQL, such as stored procedures and triggers, to make your programs more powerful. Week 3 teaches you how to streamline SQL code; use the data dictionary; use SQL to generate more SQL code; work with PL/SQL, Transact-SQL, and SQL*Plus; and handle common SQL mistakes and errors.

The syntax of SQL is explained and then brought to life in examples using Personal Oracle7, Microsoft Query, and other database tools. You don't need access to any of these products to use this book--it can stand alone as an SQL syntax reference. However, using one of these platforms and walking though the examples will help you understand the nuances.

Conventions Used in This Book

This book uses the following typeface conventions:

The following special design features enhance the text:

NOTE: Notes explain interesting or important points that can help you understand SQL concepts and techniques.

TIP: Tips are little pieces of information to begin to help you in real-world situations. Tips often offer shortcuts or information to make a task easier or faster.

WARNING: Warnings provide information about detrimental performance issues or dangerous errors. Pay careful attention to Warnings.

Next chapterContents

Macmillan Computer Publishing USA

© Copyright, Macmillan Computer Publishing. All rights reserved.

Casa de Bender