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

Book

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

Description

  • 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.



0201433362B04062001

Sample Content

Table of Contents



Foreword.


Preface and Acknowledgments.


About the Authors.


Introduction.

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".

I. RELATIONAL DATABASES AND SQL.

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.

Tables.

Fields.

Records.

Keys.

Views.

Relationships.

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?

II. SQL BASICS.

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.

Concatenation.

Mathematical.

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.

Comparison.

Range.

Set Membership.

Pattern Match.

Null.

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.

III. WORKING WITH MULTIPLE TABLES.

7. Thinking in Sets.

Topics Covered in This Chapter.

What Is a Set, Anyway?

Operations on Sets.

Intersection.

Intersection in Set Theory.

Intersection between Result Sets.

Problems You Can Solve with INTERSECT.

Difference.

Difference in Set Theory.

Difference Between Result Sets.

Problems You Can Solve with DIFFERENCE.

Union.

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.

8. INNER JOINs.

Topics Covered in This Chapter.

What Is a JOIN?

The INNER JOIN.

What's "Legal" to JOIN?

Syntax.

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.

9. OUTER JOINs.

Topics Covered in This Chapter.

What is an OUTER JOIN?

The Left/Right OUTER JOIN.

Syntax.

The FULL OUTER JOIN.

Syntax.

FULL OUTER JOIN on Non-Key Values.

UNION JOIN.

Uses for OUTER JOINS.

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.

Syntax.

An Introduction to Aggregate Functions--COUNT and MAX.

Subqueries as Filters.

Syntax.

Special Predicate Keywords for Subqueries.

Uses for Subqueries.

Column Expressions.

Filters.

Sample Statements.

Subqueries in Expressions.

Subqueries in Filters.

Problems for You to Solve.

IV. SUMMARIZING AND GROUPING DATA.

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.

Syntax.

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.

APPENDICES.

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

Preface

"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



0201433362P04062001

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020