Home > Store

SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL

Register your product to gain access to bonus material or receive a coupon.

SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL


  • This product currently is not for sale.
Not for Sale


  • Copyright 2000
  • Dimensions: 7-1/16x9-1/4
  • Pages: 528
  • Edition: 1st
  • Book
  • ISBN-10: 0-201-43336-2
  • ISBN-13: 978-0-201-43336-4

In the past few years, SQL has evolved from a language known only to computer specialists to a widely used, international standard of the computer industry. The number of SQL-compatible databases shipping each year now totals in the millions. If you are accessing corporate information from the Internet or from an internal network, you are probably using SQL. SQL Queries for Mere Mortals will help new users learn the foundations of SQL queries, and will prove an essential reference guide for intermediate and advanced users. The accompanying CD contains five sample databases used for the example queries throughout the book, plus an evaluation copy of Microsoft SQL Server version 7.


Sample Content

Table of Contents


Preface and Acknowledgments.

About the Authors.


Are You a Mere Mortal?

About This Book.

How to Use This Book.

Reading the Diagrams Used in This Book.

Sample Databases Used in This Book.

"Follow the Yellow Brick Road".


1. What Is "Relational?"

Topics Covered in This Chapter.

Types of Databases.

A Brief History of the Relational Model.

In the Beginning . . ..

Relational Database Software.

Anatomy of a Relational Database.







What's in It for Me?

"Where Do I Go from Here?"

2. Ensuring Your Database Structure Is Sound.

Topics Covered in This Chapter.

Why Is This Chapter Here?

Why Worry about Sound Structures?

Fine-tuning Fields.

What's in a Name? (Part One).

Smoothing Out the Rough Edges.

Resolving Multipart Fields.

Resolving Multivalued Fields.

Fine-tuning Tables.

What's in a Name? (Part Two).

Ensuring a Sound Structure.

Resolving Unnecessary Duplicate Fields.

Identification Is the Key.

Establishing Solid Relationships.

Establishing a Deletion Rule.

Setting the TYpe of Participation.

Setting the Degree of Participation.

Is That All?

3. A Concise History of SQL.

Topics Covered in This Chapter.

The Origins of SQL.

Early Vendor Implementation.

". . . And Then There Was a Standard".

Evolution of the ANSI/ISO Standard.

Other SQL Standards.

Commercial Implementations.

What the Future Holds.

Why Should You Learn SQL?


4. Creating a Simple Query.

Topics Covered in This Chapter.

Introducing SELECT.

The SELECT Statement.

Major Clauses in a SELECT Statement.

A Quick Aside: Data vs. Information.

Translating Your Request into SQL.

Expanding the Field of Vision.

Eliminating Duplicate Rows.

Sorting Information.

First Things First: Collating Sequences.

Let's Now Come to Order.

Saving Your Work.

Sample Statements.

Problems for You to Solve.

5. Getting More Than Simple Columns.

Topics Covered in This Chapter.

The SELECT Clause: Take Two.

Specifying Explicit Values.

Moving Beyond Basic Information.

What Is an Expression?

What Are You Trying to Express?

Data Types in SQL.

Types of Expressions.



Date And Time Arithmetic.

Using Expressions in a SELECT Clause.

A Brief Digression: Value Expressions.

That "Nothing" Value--NULL.

Introducing Null.

Sample Statements.

Problems for You to Solve.

6. Filtering Your Data.

Topics Covered in This Chapter.

Refining What You See Using WHERE.

The WHERE Clause.

Using A WHERE Clause.

Defining Search Conditions.



Set Membership.

Pattern Match.


Excluding Rows with NOT.

Using Multiple Conditions.

Introducing AND and OR.

Excluding Rows: Take Two.

Order of Precedence.

Nulls Revisited: A Cautionary Note.

Expressing Conditions in Different Ways.

Sample Statements.

Problems for You to Solve.


7. Thinking in Sets.

Topics Covered in This Chapter.

What Is a Set, Anyway?

Operations on Sets.


Intersection in Set Theory.

Intersection between Result Sets.

Problems You Can Solve with INTERSECT.


Difference in Set Theory.

Difference Between Result Sets.

Problems You Can Solve with DIFFERENCE.


Union in Set Theory.

Combining Result Sets Using UNION.

Problems You Can Solve with UNION.

SQL Set Operations.

"Classical" Set Operations vs. SQL.

Finding Common Values--INTERSECT.

Finding Missing Values--EXCEPT (Difference).

Combining Sets--UNION.


Topics Covered in This Chapter.

What Is a JOIN?


What's "Legal" to JOIN?


Check Those Relationships!

Uses for INNER JOINs.

Find Related Rows.

Find Matching Values.

Sample Statements.

Two Tables.

More Than Two Tables.

Looking for Matching Values.

Problems for You to Solve.


Topics Covered in This Chapter.

What is an OUTER JOIN?

The Left/Right OUTER JOIN.




FULL OUTER JOIN on Non-Key Values.



Find Missing Values.

Find Partially Matched Information.

Sample Statements.

Problems for You to Solve.

10. UNIONs.

Topics Covered in This Chapter.

What Is a Union?

Writing Requests with UNION.

Using Simple SELECT Statements.

Combining Complex SELECT Statements.

Using UNION More Than Once.

Sorting a UNION.

Uses for UNION.

Sample Statements.

Problems for You to Solve.

11. Subqueries.

Topics Covered in This Chapter.

What Is a Subquery?

Row Subqueries.

Table Subqueries.

Scalar Subqueries.

Subqueries as Column Expressions.


An Introduction to Aggregate Functions--COUNT and MAX.

Subqueries as Filters.


Special Predicate Keywords for Subqueries.

Uses for Subqueries.

Column Expressions.


Sample Statements.

Subqueries in Expressions.

Subqueries in Filters.

Problems for You to Solve.


12. Summarizing Data.

Topics Covered in This Chapter.

Aggregate functions.

Counting Rows and Values with COUNT.

Computing a Total with SUM.

Calculating a Mean Value with AVG.

Finding the Largest Value with MAX.

Finding the Smallest Value with MIN.

Using More Than One Function.

Using Aggregate Functions in Filters.

Sample Statements.

Problems for You to Solve.

13. Grouping Data.

Topics Covered in This Chapter.

Why Group Data?

The GROUP BY Clause.


Mixing Columns and Expressions.

Using GROUP BY in a Subquery in a WHERE Clause.

Simulating a SELECT DISTINCT Statement.

"Some Restrictions Apply".

Column Restrictions.

Grouping on Expressions.

Uses for GROUP BY.

Sample Statements.

Problems for You to Solve.

14. Filtering Grouped Data.

Topics Covered in This Chapter.

A New Meaning of "Focus Groups".

When You Filter Makes A Difference.

Should You Filter in WHERE or in HAVING?

Avoiding the HAVING COUNT Trap.

Uses for HAVING.

Sample Statements.

Problems for You to Solve.

In Closing.


A. SQL Standard Diagrams.
B. Schema for the Sample Databases.
C. Recommended Reading References.
Index. 0201433362T04062001


"Language is by its very nature a communal thing; that is, it expresses never the exact thing but a compromise--that which is common to you, me, and everybody."
--Thomas Earnest Hulme, Speculations

Learning how to retrieve information from a database is commonly a perplexing exercise. However, it can be a relatively easy task--as long as you understand the question you're posing to the database. Once you understand the question, you can translate it into the language used by any database system, which in most cases is Structured Query Language (SQL). You have to translate your request into an SQL statement so that your database system knows what information you want to retrieve. SQL provides the means for you and your database system to communicate with each other.

Throughout our many years as database consultants, we've found that the number of people who merely need to retrieve information from a database far outnumber those who are charged with the task of creating programs and applications for a database. Unfortunately, no books focus solely on the subject of retrieving information, particularly from a "mere mortals" viewpoint. There are numerous good books on SQL, to be sure, but most are targeted toward database programming and development.

With this in mind, we decided it was time to write a book that would help people learn how to query a database properly and effectively. The result of our decision is in your hands. This book is unique among SQL books in that it focuses only on the querying portion of SQL. When you finish reading this book, you'll have the skills you need to retrieve any information you require.

Writing a book such as this is always a cooperative effort. There are always editors, colleagues, friends, and relatives willing to lend their support and provide valuable advice when we need it the most. These folks continually provide us with encouragement, help us to remain focused, and motivate us to see this project through to the end.

First and foremost, we want to thank our editor, Mary O'Brien, for the opportunity to write this book. She saw the potential of an idea we had and pursued it with great dedication. We'd also like to thank Mary and her assistant, Mariann Kourafas, for their great patience and unwavering support throughout the many months we were writing this book. And we can't forget Marilyn Rash and the production staff--great job, guys!

Next, we'd like to acknowledge our technical editors Malcom C. Rubel, Michael Blaha, Alexander Tarasul, and Keith W. Hare. Malcom, as always, it's great to have you on the team! Michael and Alexander, thanks for all your thoughtful comments and suggestions. And a special thanks to Keith--he corrected a few minor errors we had in the SQL history timeline and provided much of the information for the What the Future Holds section of Chapter 3. Thanks once again to all of you for your time and input and for helping us to make this a solid treatise on SQL queries.

Finally, a very special thanks to Joe Celko for providing the Foreword. Joe is an SQL expert, a colleague, and a good friend. We have a lot of respect for Joe's knowledge and expertise on the subject, and we're pleased to have his thoughts and comments at the beginning of our book.

Michael J. Hernandez
& John L. Viescas

I want to give my most sincere thanks to my dear friend and colleague, John L. Viescas, for the opportunity of co-authoring this book with him. It was John who had the initial idea for this book, and over dinner one evening he talked me into writing it with him. John has been in the business a long time and is an established, respected author. It is my honor to share authorship with him on this work.

Finally, I want to thank my wife Kendra. Once again, she has exhibited extreme patience while I toiled away at my writing. Her help has been invaluable, and yet again, I owe her a great debt. I would tell you that she is the love of my life, my closest confidant, and my best friend, but she abhors any sort of public displays of affection. (She refers to this as PDA.) So I'll just end with this:

Well, Ked, we can resume a normal life again--until the next book!

Michael J. Hernandez
Bellevue, Washington

Gee, Mike! Are you trying to give me a big head or something? You're not such a slouch yourself. I may have talked you into doing the "next" Mere Mortals book on SQL, but you're the one who invented the now well-proven format and "voice" for this audience. It has been a fun and interesting task bringing the complex world of SQL (one of my favorite subjects) to such a wide audience. Thanks for letting me in on the project.

Unlike yours, my wife, Suzanne, does not abhor PDAs. We both owe her a big hug--not just for putting up with me while I was "offline" doing this book but also for providing excellent behind-the-scenes editing and critical commentary of our material. She's no stranger when it comes to computers, but she's definitely a "mere mortal" when it comes to databases. She provided a perfect audience to test the draft chapters. I only had to make her one promise: I'm leaving my laptop home this spring on our next trip to Hawaii!

John I. Viescas
Austin, Texas



Submit Errata

More Information

Unlimited one-month access with your purchase
Free Safari Membership