Sams Teach Yourself SQL in 10 Minutes (Learning Lab)


Author: Ben Forta
Publisher: Sams Publishing
ISBN: 978-0-13-419273-4
Copyright © 2013 by Pearson Education, Inc.

Contents at a Glance


1 Understanding SQL

2 Retrieving Data

3 Sorting Retrieved Data

4 Filtering Data

5 Advanced Data Filtering

6 Using Wildcard Filtering

7 Creating Calculated Fields

8 Using Data Manipulation Functions

9 Summarizing Data

10 Grouping Data

11 Working with Subqueries

12 Joining Tables

13 Creating Advanced Joins

14 Combining Queries

15 Inserting Data

16 Updating and Deleting Data

17 Creating and Manipulating Tables

18 Using Views

19 Working with Stored Procedures

20 Managing Transaction Processing

21 Using Cursors

22 Understanding Advanced SQL Features

A Sample Table Scripts

B Working in Popular Applications

C SQL Statement Syntax

D Using SQL Datatypes

E SQL Reserved Words


SQL is the most widely used database language. Whether you are an application developer, database administrator, web application designer, mobile app developer, or Microsoft Office user, a good working knowledge of SQL is an important part of interacting with databases.

This book was born out of necessity. I had been teaching Web application development for several years, and students were constantly asking for SQL book recommendations. There are lots of SQL books out there. Some are actually very good. But they all have one thing in common: for most users they teach just too much information. Instead of teaching SQL itself, most books teach everything from database design and normalization to relational database theory and administrative concerns. And while those are all important topics, they are not of interest to most of us who just need to learn SQL.

And so, not finding a single book that I felt comfortable recommending, I turned that classroom experience into the book you are holding. Sams Teach Yourself SQL in 10 Minutes will teach you SQL you need to know, starting with simple data retrieval and working on to more complex topics including the use of joins, subqueries, stored procedures, cursors, triggers, and table constraints. You’ll learn methodically, systematically, and simply—in lessons that will each take 10 minutes or less to complete.

Now in its fourth edition, this book has taught SQL to over a quarter million English speaking users, and has been translated into over a dozen other languages too so as to help users the globe over. And now it is your turn. So turn to Lesson 1, and get to work. You’ll be writing world class SQL in no time at all.

Who Is Teach Yourself SQL For?

This is for you if

  • You are new to SQL
  • You want to quickly learn how to get the most out of SQL
  • You want to learn how to use SQL in your own application development
  • You want to be productive quickly and easily in SQL without having to call someone for help

DBMSs Covered

For the most part, the SQL taught in this book will apply to any Database Management System (DBMS). However, as all SQL implementations are not created equal, the following DBMSs are explicitly covered (and specific instructions or notes are included where needed):

  • Apache Open Office Base
  • IBM DB2
  • Microsoft Access
  • Microsoft SQL Server (including Microsoft SQL Server Express)
  • MariaDB
  • MySQL
  • Oracle (including Oracle Express)
  • PostgreSQL
  • SQLite

Example databases (or SQL scripts to create the example databases) are available for all of these DBMSs on the author's site at


A Note presents interesting pieces of information related to the surrounding discussion.


A Tip offers advice or teaches an easier way to do something.

Conventions Used

This book uses different typefaces to differentiate between code and regular English, and also to help you identify important concepts.

Text that you type and text that should appear on your screen is presented in monospace type.

It will look like this to mimic the way text looks on your screen.

Placeholders for variables and expressions appear in monospace italic font. You should replace the placeholder with the specific value it represents.

This arrow (Image) at the beginning of a line of code means that a single line of code is too long to fit on the printed page. Continue typing all the characters after the Image as though they were part of the preceding line.

Special Features in the Learning Lab Web Edition

This online Learning Lab edition of the book includes several unique special features:

  • Videos let you watch author Ben Forta explain key concepts and techniques in each lesson
  • Live SQL-editing sandbox widgets for all the examples allow you to try out and customize the queries without having to install a DBMS
  • Short interactive quizzes at the end of each lesson help you test your understanding before going on
  • Exercises at the end of most lessons let you see if you can create your own SQL queries

About the Author

Ben Forta is Adobe Systems’ Director of Developer Relations and has more than 20 years of experience in the computer industry in product development, support, training, and product marketing. He is the author of the best-selling Sams Teach Yourself SQL in 10 Minutes, spinoff titles on MySQL and SQL Server T-SQL, ColdFusion Web Application Construction Kit and Advanced ColdFusion Application Development (both published by Adobe Press), Sams Teach Yourself Regular Expressions in 10 Minutes, as well as books on Flash, Java, Windows, and other subjects. He has extensive experience in database design and development, has implemented databases for several highly successful commercial software programs and websites, and is a frequent lecturer and columnist on Internet and database technologies. Ben lives in Oak Park, Michigan, with his wife Marcy and their seven children. Ben welcomes your e-mail at and invites you to visit his website at

We Want to Hear from You!

As the reader of this book, you are our most important critic and commentator. 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 e-mail or write directly to let us know what you did or didn’t like about this product—as well as what we can do to make our products stronger.

Please note that we cannot help you with technical problems related to the topic of this product, and that due to the high volume of mail we receive, we might not reply to every message.

When you write, please be sure to include this book’s title and author as well as your name and contact information.


Mail:        Reader Feedback
                 Sams Publishing/Pearson Education
                 800 East 96th Street
                 Indianapolis, IN 46240 USA


Thanks to the team at Sams for all these years of support, dedication, and encouragement. A special thank-you to Mark Taber for encouraging this long awaited update and for suggesting and facilitating the code coloring, which significantly enhances the readability and value of this new edition.

Thanks to my colleague Greg Wilson for his thorough technical review.

Thanks to the many hundreds of you who provided feedback on the first three editions of this book. Fortunately, most of it was positive, and all of it was appreciated. The enhancements and changes in this edition are a direct response to your feedback, which I continue to welcome.

Thanks to the dozens of colleges and universities who have made this book part of their IT and computer science curriculums. Being included and trusted by professors and teachers this way is immensely rewarding and equally humbling.

And finally, thanks to the more than one-quarter million of you who bought the previous editions of this book, making it not just my best-selling title, but also one of the best-selling books on the subject. Your continued support is the highest compliment an author can ever be paid.

—Ben Forta

Reader Services

Visit our website and register this book at for convenient access to any updates, downloads, or errata that might be available for this book.