You are previewing Learning SQL.
O'Reilly logo
Learning SQL

Book Description

SQL (Structured Query Language) is a standard programming language for generating, manipulating, and retrieving information from a relational database. If you're working with a relational database--whether you're writing applications, performing administrative tasks, or generating reports--you need to know how to interact with your data. Even if you are using a tool that generates SQL for you, such as a reporting tool, there may still be cases where you need to bypass the automatic generation feature and write your own SQL statements.

To help you attain this fundamental SQL knowledge, look to Learning SQL, an introductory guide to SQL, designed primarily for developers just cutting their teeth on the language.

Learning SQL moves you quickly through the basics and then on to some of the more commonly used advanced features. Among the topics discussed:

  • The history of the computerized database

  • SQL Data Statements--those used to create, manipulate, and retrieve data stored in your database; example statements include select, update, insert, and delete

  • SQL Schema Statements--those used to create database objects, such as tables, indexes, and constraints

  • How data sets can interact with queries

  • The importance of subqueries

  • Data conversion and manipulation via SQL's built-in functions

  • How conditional logic can be used in Data Statements

Best of all, Learning SQL talks to you in a real-world manner, discussing various platform differences that you're likely to encounter and offering a series of chapter exercises that walk you through the learning process. Whenever possible, the book sticks to the features included in the ANSI SQL standards. This means you'll be able to apply what you learn to any of several different databases; the book covers MySQL, Microsoft SQL Server, and Oracle Database, but the features and syntax should apply just as well (perhaps with some tweaking) to IBM DB2, Sybase Adaptive Server, and PostgreSQL.

Put the power and flexibility of SQL to work. With Learning SQL you can master this important skill and know that the SQL statements you write are indeed correct.

Table of Contents

  1. Learning SQL
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Why Learn SQL?
    2. Why Use This Book to Do It?
    3. Structure of This Book
    4. Conventions Used in This Book
    5. How to Contact Us
    6. Using Code Examples
    7. Safari Enabled
    8. Acknowledgments
  4. 1. A Little Background
    1. 1.1. Introduction to Databases
      1. 1.1.1. Nonrelational Database Systems
      2. 1.1.2. The Relational Model
      3. 1.1.3. Some Terminology
    2. 1.2. What Is SQL?
      1. 1.2.1. SQL Statement Classes
      2. 1.2.2. SQL: A Nonprocedural Language
      3. 1.2.3. SQL Examples
    3. 1.3. What Is MySQL?
    4. 1.4. What's in Store
  5. 2. Creating and Populating a Database
    1. 2.1. Creating a MySQL Database
    2. 2.2. Using the mysql Command-Line Tool
    3. 2.3. MySQL Data Types
      1. 2.3.1. Character Data
        1. 2.3.1.1. Character sets
        2. 2.3.1.2. Text data
      2. 2.3.2. Numeric Data
      3. 2.3.3. Temporal Data
    4. 2.4. Table Creation
      1. 2.4.1. Step 1: Design
      2. 2.4.2. Step 2: Refinement
      3. 2.4.3. Step 3: Building SQL Schema Statements
    5. 2.5. Populating and Modifying Tables
      1. 2.5.1. Inserting Data
        1. 2.5.1.1. Generating numeric key data
        2. 2.5.1.2. The insert statement
      2. 2.5.2. Updating Data
      3. 2.5.3. Deleting Data
    6. 2.6. When Good Statements Go Bad
      1. 2.6.1. Nonunique Primary Key
      2. 2.6.2. Nonexistent Foreign Key
      3. 2.6.3. Column Value Violations
      4. 2.6.4. Invalid Date Conversions
    7. 2.7. The Bank Schema
  6. 3. Query Primer
    1. 3.1. Query Mechanics
    2. 3.2. Query Clauses
    3. 3.3. The select Clause
      1. 3.3.1. Column Aliases
      2. 3.3.2. Removing Duplicates
    4. 3.4. The from Clause
      1. 3.4.1. Tables
        1. 3.4.1.1. Subquery-generated tables
        2. 3.4.1.2. Views
      2. 3.4.2. Table Links
      3. 3.4.3. Defining Table Aliases
    5. 3.5. The where Clause
    6. 3.6. The group by and having Clauses
    7. 3.7. The order by Clause
      1. 3.7.1. Ascending Versus Descending Sort Order
      2. 3.7.2. Sorting via Expressions
      3. 3.7.3. Sorting via Numeric Placeholders
    8. 3.8. Exercises
      1. 3.8.1. 3-1
      2. 3.8.2. 3-2
      3. 3.8.3. 3-3
      4. 3.8.4. 3-4
  7. 4. Filtering
    1. 4.1. Condition Evaluation
      1. 4.1.1. Using Parentheses
      2. 4.1.2. Using the not Operator
    2. 4.2. Building a Condition
    3. 4.3. Condition Types
      1. 4.3.1. Equality Conditions
        1. 4.3.1.1. Inequality conditions
        2. 4.3.1.2. Data modification using equality conditions
      2. 4.3.2. Range Conditions
        1. 4.3.2.1. The between operator
        2. 4.3.2.2. String ranges
      3. 4.3.3. Membership Conditions
        1. 4.3.3.1. Using subqueries
        2. 4.3.3.2. Using not in
      4. 4.3.4. Matching Conditions
        1. 4.3.4.1. Using wildcards
        2. 4.3.4.2. Using regular expressions
    4. 4.4. NULL: That Four-Letter Word
    5. 4.5. Exercises
      1. 4.5.1. 4-1
      2. 4.5.2. 4-2
      3. 4.5.3. 4-3
      4. 4.5.4. 4-4
  8. 5. Querying Multiple Tables
    1. 5.1. What Is a Join?
      1. 5.1.1. Cartesian Product
      2. 5.1.2. Inner Joins
      3. 5.1.3. The ANSI Join Syntax
    2. 5.2. Joining Three or More Tables
      1. 5.2.1. Using Subqueries as Tables
      2. 5.2.2. Using the Same Table Twice
    3. 5.3. Self-Joins
    4. 5.4. Equi-Joins Versus Non-Equi-Joins
    5. 5.5. Join Conditions Versus Filter Conditions
    6. 5.6. Exercises
      1. 5.6.1. 5-1
      2. 5.6.2. 5-2
      3. 5.6.3. 5-3
  9. 6. Working with Sets
    1. 6.1. Set Theory Primer
    2. 6.2. Set Theory in Practice
    3. 6.3. Set Operators
      1. 6.3.1. The union Operator
      2. 6.3.2. The intersect Operator
      3. 6.3.3. The except Operator
    4. 6.4. Set Operation Rules
      1. 6.4.1. Sorting Compound Query Results
      2. 6.4.2. Set Operation Precedence
    5. 6.5. Exercises
      1. 6.5.1. 6-1
      2. 6.5.2. 6-2
      3. 6.5.3. 6-3
  10. 7. Data Generation, Conversion, and Manipulation
    1. 7.1. Working with String Data
      1. 7.1.1. String Generation
        1. 7.1.1.1. Including single quotes
        2. 7.1.1.2. Including special characters
      2. 7.1.2. String Manipulation
        1. 7.1.2.1. String functions that return numbers
        2. 7.1.2.2. String functions that return strings
    2. 7.2. Working with Numeric Data
      1. 7.2.1. Performing Arithmetic Functions
      2. 7.2.2. Controlling Number Precision
      3. 7.2.3. Handling Signed Data
    3. 7.3. Working with Temporal Data
      1. 7.3.1. Dealing with Time Zones
      2. 7.3.2. Temporal Data Generation
        1. 7.3.2.1. String representations of temporal data
        2. 7.3.2.2. String-to-date conversions
        3. 7.3.2.3. Functions for generating dates
      3. 7.3.3. Temporal Data Manipulation
        1. 7.3.3.1. Temporal functions that return dates
        2. 7.3.3.2. Temporal functions that return strings
        3. 7.3.3.3. Temporal functions that return numbers
    4. 7.4. Conversion Functions
    5. 7.5. Exercises
      1. 7.5.1. 7-1
      2. 7.5.2. 7-2
      3. 7.5.3. 7-3
  11. 8. Grouping and Aggregates
    1. 8.1. Grouping Concepts
    2. 8.2. Aggregate Functions
      1. 8.2.1. Implicit Versus Explicit Groups
      2. 8.2.2. Counting Distinct Values
      3. 8.2.3. Using Expressions
      4. 8.2.4. How Nulls Are Handled
    3. 8.3. Generating Groups
      1. 8.3.1. Single-Column Grouping
      2. 8.3.2. Multi-Column Grouping
      3. 8.3.3. Grouping via Expressions
      4. 8.3.4. Generating Rollups
    4. 8.4. Group Filter Conditions
    5. 8.5. Exercises
      1. 8.5.1. 8-1
      2. 8.5.2. 8-2
      3. 8.5.3. 8-3
    6. 8.6. 8-4 (Extra Credit)
  12. 9. Subqueries
    1. 9.1. What Is a Subquery?
    2. 9.2. Subquery Types
    3. 9.3. Noncorrelated Subqueries
      1. 9.3.1. Multiple-Row, Single-Column Subqueries
        1. 9.3.1.1. The in operator
        2. 9.3.1.2. The all operator
        3. 9.3.1.3. The any operator
      2. 9.3.2. Multicolumn Subqueries
    4. 9.4. Correlated Subqueries
      1. 9.4.1. The Exists Operator
      2. 9.4.2. Data Manipulation Using Correlated Subqueries
    5. 9.5. When to Use Subqueries
      1. 9.5.1. Subqueries as Data Sources
        1. 9.5.1.1. Table fabrication
        2. 9.5.1.2. Task-oriented subqueries
      2. 9.5.2. Subqueries in Filter Conditions
      3. 9.5.3. Subqueries as Expression Generators
    6. 9.6. Subquery Wrap-up
    7. 9.7. Exercises
      1. 9.7.1. 9-1
      2. 9.7.2. 9-2
      3. 9.7.3. 9-3
      4. 9.7.4. 9-4
  13. 10. Joins Revisited
    1. 10.1. Outer Joins
      1. 10.1.1. Left Versus Right Outer Joins
      2. 10.1.2. Three-Way Outer Joins
      3. 10.1.3. Self Outer Joins
    2. 10.2. Cross Joins
    3. 10.3. Natural Joins
    4. 10.4. Exercises
      1. 10.4.1. 10-1
      2. 10.4.2. 10-2
      3. 10.4.3. 10-3
      4. 10.4.4. 10-4 (Extra Credit)
  14. 11. Conditional Logic
    1. 11.1. What Is Conditional Logic?
    2. 11.2. The Case Expression
      1. 11.2.1. Searched Case Expressions
      2. 11.2.2. Simple Case Expressions
    3. 11.3. Case Expression Examples
      1. 11.3.1. Result Set Transformations
      2. 11.3.2. Selective Aggregation
      3. 11.3.3. Checking for Existence
      4. 11.3.4. Division by Zero Errors
      5. 11.3.5. Conditional Updates
      6. 11.3.6. Handling Null Values
    4. 11.4. Exercises
      1. 11.4.1. 11-1
      2. 11.4.2. 11-2
  15. 12. Transactions
    1. 12.1. Multiuser Databases
    2. 12.2. What Is a Transaction?
      1. 12.2.1. Starting a Transaction
      2. 12.2.2. Ending a Transaction
      3. 12.2.3. Transaction Savepoints
  16. 13. Indexes and Constraints
    1. 13.1. Indexes
      1. 13.1.1. Index Creation
        1. 13.1.1.1. Unique indexes
        2. 13.1.1.2. Multi-column indexes
      2. 13.1.2. Types of Indexes
        1. 13.1.2.1. B-tree indexes
        2. 13.1.2.2. Bitmap indexes
        3. 13.1.2.3. Text indexes
      3. 13.1.3. How Indexes Are Used
      4. 13.1.4. The Downside of Indexes
    2. 13.2. Constraints
      1. 13.2.1. Constraint Creation
      2. 13.2.2. Constraints and Indexes
      3. 13.2.3. Cascading Constraints
  17. A. ER Diagram for Example Database
  18. B. MySQL Extensions to the SQL Language
    1. B.1. Select Extensions
      1. B.1.1. The limit Clause
        1. B.1.1.1. Combining the limit clause with the order by clause
        2. B.1.1.2. limit's optional second parameter
        3. B.1.1.3. Ranking queries
      2. B.1.2. The into outfile Clause
    2. B.2. Combination Insert/Update Statements
    3. B.3. Ordered Updates and Deletes
    4. B.4. Multitable Updates and Deletes
  19. C. Solutions to Exercises
    1. C.1. Chapter 3
      1. C.1.1. 3-1
      2. C.1.2. 3-2
      3. C.1.3. 3-3
      4. C.1.4. 3-4
    2. C.2. Chapter 4
      1. C.2.1. 4-1
      2. C.2.2. 4-2
      3. C.2.3. 4-3
      4. C.2.4. 4-4
    3. C.3. Chapter 5
      1. C.3.1. 5-1
      2. C.3.2. 5-2
      3. C.3.3. 5-3
    4. C.4. Chapter 6
      1. C.4.1. 6-1
      2. C.4.2. 6-2
      3. C.4.3. 6-3
    5. C.5. Chapter 7
      1. C.5.1. 7-1
      2. C.5.2. 7-2
      3. C.5.3. 7-3
    6. C.6. Chapter 8
      1. C.6.1. 8-1
      2. C.6.2. 8-2
      3. C.6.3. 8-3
    7. C.7. 8-4 (Extra Credit)
    8. C.8. Chapter 9
      1. C.8.1. 9-1
      2. C.8.2. 9-2
      3. C.8.3. 9-3
      4. C.8.4. 9-4
    9. C.9. Chapter 10
      1. C.9.1. 10-1
      2. C.9.2. 10-2
      3. C.9.3. 10-3
      4. C.9.4. 10-4 (Extra Credit)
    10. C.10. Chapter 11
      1. C.10.1. 11-1
      2. C.10.2. 11-2
  20. D. Further Resources
    1. D.1. Advanced SQL
    2. D.2. Database Programming
    3. D.3. Database Design
    4. D.4. Database Tuning
    5. D.5. Database Administration
    6. D.6. Report Generation
  21. About the Author
  22. Colophon
  23. Copyright