- Sams Teach Yourself SQL in 24 Hours, Third Edition
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: A SQL Concepts Overview
- Hour 1. Welcome to the World of SQL
- SQL Definition and History
- SQL Sessions
- Types of SQL Commands
- An Introduction to the Database Used in This Book
- Summary
- Q&A
- Workshop
- Part II: Building Your Database
- Hour 2. Defining Data Structures
- What Is Data?
- Basic Data Types
- Summary
- Q&A
- Workshop
- Hour 3. Managing Database Objects
- What Are Database Objects?
- What Is a Schema?
- A Table: The Primary Storage for Data
- Integrity Constraints
- Summary
- Q&A
- Workshop
- Hour 4. The Normalization Process
- Normalizing a Database
- Summary
- Q&A
- Workshop
- Hour 5. Manipulating Data
- Overview of Data Manipulation
- Populating Tables with New Data
- Updating Existing Data
- Deleting Data from Tables
- Summary
- Q&A
- Workshop
- Hour 6. Managing Database Transactions
- What Is a Transaction?
- What Is Transactional Control?
- Transactional Control and Database Performance
- Summary
- Q&A
- Workshop
- Part III: Getting Effective Results from Queries
- Hour 7. Introduction to the Database Query
- What Is a Query?
- Introduction to the <tt>SELECT</tt> Statement
- Examples of Simple Queries
- Summary
- Q&A
- Workshop
- Hour 8. Using Operators to Categorize Data
- What Is an Operator in SQL?
- Comparison Operators
- Logical Operators
- Conjunctive Operators
- Negating Conditions with the <tt>NOT</tt> Operator
- Arithmetic Operators
- Summary
- Q&A
- Workshop
- Hour 9. Summarizing Data Results from a Query
- What Are Aggregate Functions?
- Summary
- Q&A
- Workshop
- Hour 10. Sorting and Grouping Data
- Why Group Data?
- The <tt>GROUP BY</tt> Clause
- <tt>GROUP BY</tt> Versus <tt>ORDER BY</tt>
- The <tt>HAVING</tt> Clause
- Summary
- Q&A
- Workshop
- Hour 11. Restructuring the Appearance of Data
- The Concepts of ANSI Character Functions
- Various Common Character Functions
- Miscellaneous Character Functions
- Mathematical Functions
- Conversion Functions
- The Concept of Combining Character Functions
- Summary
- Q&A
- Workshop
- Hour 12. Understanding Dates and Times
- How Is a Date Stored?
- Date Functions
- Date Conversions
- Summary
- Q&A
- Workshop
- Part IV: Building Sophisticated Database Queries
- Hour 13. Joining Tables in Queries
- Selecting Data from Multiple Tables
- Types of Joins
- Join Considerations
- Summary
- Q&A
- Workshop
- Hour 14. Using Subqueries to Define Unknown Data
- What Is a Subquery?
- Embedding a Subquery Within a Subquery
- Summary
- Q&A
- Workshop
- Hour 15. Combining Multiple Queries into One
- Single Queries Versus Compound Queries
- Why Would I Ever Want to Use a Compound Query?
- Compound Query Operators
- Using an <tt>ORDER BY</tt> with a Compound Query
- Using <tt>GROUP BY</tt> with a Compound Query
- Retrieving Accurate Data
- Summary
- Workshop
- Q&A
- Part V: SQL Performance Tuning
- Hour 16. Using Indexes to Improve Performance
- What Is an Index?
- How Do Indexes Work?
- The <tt>CREATE INDEX</tt> Command
- Types of Indexes
- When Should Indexes Be Considered?
- When Should Indexes Be Avoided?
- Summary
- Q&A
- Workshop
- Hour 17. Improving Database Performance
- What Is SQL Statement Tuning?
- Database Tuning Versus SQL Tuning
- Formatting Your SQL Statement
- Full Table Scans
- Other Performance Considerations
- Performance Tools
- Summary
- Q&A
- Workshop
- Part VI: Using SQL to Manage Users and Security
- Hour 18. Managing Database Users
- Users Are the Reason
- The Management Process
- Tools Utilized by Database Users
- Summary
- Q&A
- Workshop
- Hour 19. Managing Database Security
- What Is Database Security?
- How Does Security Differ from User Management?
- What Are Privileges?
- Controlling User Access
- Controlling Privileges Through Roles
- Summary
- Q&A
- Workshop
- Part VII: Summarized Data Structures
- Hour 20. Creating and Using Views and Synonyms
- What Is a View?
- Creating Views
- Dropping a View
- What Is a Synonym?
- Summary
- Q&A
- Workshop
- Hour 21. Working with the System Catalog
- What Is the System Catalog?
- How Is the System Catalog Created?
- What Is Contained in the System Catalog?
- Examples of System Catalog Tables by Implementation
- Querying the System Catalog
- Updating System Catalog Objects
- Summary
- Q&A
- Workshop
- Part VIII: Applying SQL Fundamentals in Today's World
- Hour 22. Advanced SQL Topics
- Advanced Topics
- Cursors
- Stored Procedures and Functions
- Triggers
- Dynamic SQL
- Call-Level Interface
- Using SQL to Generate SQL
- Direct Versus Embedded SQL
- Summary
- Q&A
- Workshop
- Hour 23. Extending SQL to the Enterprise, the Internet, and the Intranet
- SQL and the Enterprise
- Accessing a Remote Database
- Accessing a Remote Database Through a Web Interface
- SQL and the Internet
- SQL and the Intranet
- Summary
- Q&A
- Workshop
- Hour 24. Extensions to Standard SQL
- Various Implementations
- Examples of Extensions from Some Implementations
- Interactive SQL Statements
- Summary
- Q&A
- Workshop
- Part IX: Appendixes
- Appendix A. Common SQL Commands
- SQL Statements
- SQL Clauses
- Appendix B. Using MySQL for Exercises
- Windows Installation Instructions
- Linux Installation Instructions
- Appendix C. Answers to Quizzes and Exercises
- Hour 1, "Welcome to the World of SQL"
- Hour 2, "Defining Data Structures"
- Hour 3, "Managing Database Objects"
- Hour 4, "The Normalization Process"
- Hour 5, "Manipulating Data"
- Hour 6, "Managing Database Transactions"
- Hour 7, "Introduction to the Database Query"
- Hour 8, "Using Operators to Categorize Data"
- Hour 9, "Summarizing Data Results from a Query"
- Hour 10, "Sorting and Grouping Data"
- Hour 11, "Restructuring the Appearance of Data"
- Hour 12, "Understanding Dates and Time"
- Hour 13, "Joining Tables in Queries"
- Hour 14, "Using Subqueries to Define Unknown Data"
- Hour 15, "Combining Multiple Queries into One"
- Hour 16, "Using Indexes to Improve Performance"
- Hour 17, "Improving Database Performance"
- Hour 18, "Managing Database Users"
- Hour 19, "Managing Database Security"
- Hour 20, "Creating and Using Views and Synonyms"
- Hour 21, "Working with the System Catalog"
- Hour 22, "Advanced SQL Topics"
- Hour 23, "Extending SQL to the Enterprise, the Internet, and the Intranet"
- Hour 24, "Extensions to Standard SQL"
- Appendix D. <tt>CREATE TABLE</tt> Statements for Book Examples
- <tt>EMPLOYEE_TBL</tt>
- <tt>EMPLOYEE_PAY_TBL</tt>
- <tt>CUSTOMER_TBL</tt>
- <tt>ORDERS_TBL</tt>
- <tt>PRODUCTS_TBL</tt>
- Appendix E. <tt>INSERT</tt> Statements for Data in Book Examples
- <tt>INSERT</tt> Statements
- Appendix F. Glossary
- Appendix G. Bonus Exercises
Logical Operators
Logical operators are those operators that use SQL keywords to make compar-isons instead of symbols. The logical operators covered in the following subsections are |
- IS NULL
- BETWEEN
- IN
- LIKE
- EXISTS
- UNIQUE
- ALL and ANY
IS NULL
The NULL operator is used to compare a value with a NULL value. For example, you might look for employees who do not have a pager by searching for NULL values in the PAGER column of the EMPLOYEE_TBL table.
The following example shows comparing a value to a NULL value:
Example |
Meaning |
WHERE SALARY IS NULL |
Salary has no value |
The following example does not find a NULL value:
Example |
Meaning |
WHERE SALARY = NULL |
Salary has a value containing the letters N-U-L-L |
SELECT EMP_ID, LAST_NAME, FIRST_NAME, PAGER FROM EMPLOYEE_TBL WHERE PAGER IS NULL; EMP_ID LAST_NAM FIRST_NA PAGER --------- -------- -------- ----- 311549902 STEPHENS TINA 442346889 PLEW LINDA 220984332 WALLACE MARIAH 443679012 SPURGEON TIFFANY 4 rows selected.
Understand that the literal word "null" is different than a NULL value. Examine the following example:
SELECT EMP_ID, LAST_NAME, FIRST_NAME, PAGER FROM EMPLOYEE_TBL WHERE PAGER = NULL; no rows selected.
BETWEEN
The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value. The minimum and maximum values are included as part of the conditional set.
Example |
Meaning |
WHERE SALARY BETWEEN '20000' AND '30000' |
The salary must fall between 20000 and 30000, including the values 20000 and 30000 |
SELECT * FROM PRODUCTS_TBL WHERE COST BETWEEN 5.95 AND 14.5; PROD_ID PROD_DESC COST ---------- ------------------------------ ------ 222 PLASTIC PUMPKIN 18 INCH 7.75 90 LIGHTED LANTERNS 14.5 15 ASSORTED COSTUMES 10 1234 KEY CHAIN 5.95 4 rows selected.
Notice that the values 5.95 and 14.5 are included in the output.
IN
The IN operator is used to compare a value to a list of literal values that have been specified. For TRUE to be returned, the compared value must match at least one of the values in the list.
Examples |
Meaning |
WHERE SALARY IN('20000', '30000', '40000') |
The salary must match one of the values 20000, 30000, or 40000 |
SELECT * FROM PRODUCTS_TBL WHERE PROD_ID IN ('13','9','87','119'); PROD_ID PROD_DESC COST ---------- ------------------------------ ------ 119 ASSORTED MASKS 4.95 87 PLASTIC SPIDERS 1.05 9 CANDY CORN 1.35 13 FALSE PARAFFIN TEETH 1.1 4 rows selected.
Using the IN operator can achieve the same results as using the OR operator and can return the results more quickly.
LIKE
The LIKE operator is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator:
- The percent sign (%)
- The underscore (_)
The percent sign represents zero, one, or multiple characters. The underscore represents a single number or character. The symbols can be used in combinations.
Examples are
WHERE SALARY LIKE '200%' |
Finds any values that start with 200 |
WHERE SALARY LIKE '%200%' |
Finds any values that have 200 in any position |
WHERE SALARY LIKE '_00%' |
Finds any values that have 00 in the second and third positions |
WHERE SALARY LIKE '2_%_%' |
Finds any values that start with 2 and are at least three characters in length |
WHERE SALARY LIKE '%2' |
Finds any values that end with 2 |
WHERE SALARY LIKE '_2%3' |
Finds any values that have a 2 in the second position and end with a 3 |
WHERE SALARY LIKE '2___3' |
Finds any values in a five-digit number that start with 2 and end with 3 |
The following example shows all product descriptions that end with the letter S in uppercase:
SELECT PROD_DESC FROM PRODUCTS_TBL WHERE PROD_DESC LIKE '%S'; PROD_DESC ------------------ LIGHTED LANTERNS ASSORTED COSTUMES PLASTIC SPIDERS ASSORTED MASKS 4 rows selected.
The following example shows all product descriptions whose second character is the letter S in uppercase:
SELECT PROD_DESC FROM PRODUCTS_TBL WHERE PROD_DESC LIKE '_S%'; PROD_DESC ------------------ ASSORTED COSTUMES ASSORTED MASKS 2 rows selected.
EXISTS
The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria.
Example |
Meaning |
WHERE EXISTS (SELECT EMP_ID FROM EMPLOYEE_TBL WHERE EMPLOYEE_ID = '333333333') |
Searching to see whether the EMP_ID 3333333333 is in the EMPLOYEE_TBL |
The following example is a form of a subquery, which is further discussed during Hour 14, "Using Subqueries to Define Unknown Data."
SELECT COST FROM PRODUCTS_TBL WHERE EXISTS ( SELECT COST FROM PRODUCTS_TBL WHERE COST > 100 ); No rows selected. ----------
There were no rows selected because no records existed where the cost was greater than 100.
Consider the following example:
SELECT COST FROM PRODUCTS_TBL WHERE EXISTS ( SELECT COST FROM PRODUCTS_TBL WHERE COST < 100 ); COST ---------- 29.99 7.75 1.1 14.5 10 1.35 1.45 1.05 4.95 5.95 59.99 11 rows selected.
The cost was displayed for records in the table because records existed where the product cost was less than 100.
UNIQUE
The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).
Example |
Meaning |
WHERE UNIQUE (SELECT SALARY FROM EMPLOYEE_TBL WHERE EMPLOYEE_ID = '333333333') |
Testing SALARY to see whether there are duplicates |
ALL and ANY Operators
The ALL operator is used to compare a value to all values in another value set.
Example |
Meaning |
WHERE SALARY > ALL SALARY (SELECT FROM EMPLOYEE_TBL WHERE CITY = 'INDIANAPOLIS') |
Testing SALARY to see whether it is greater than all salaries of the employees living in Indianapolis |
SELECT * FROM PRODUCTS_TBL WHERE COST > ALL ( SELECT COST FROM PRODUCTS_TBL WHERE COST < 10 ); PROD_ID PROD_DESC COST ---------- ------------------------------ ------ 11235 WITCHES COSTUME 29.99 90 LIGHTED LANTERNS 14.5 15 ASSORTED COSTUMES 10 2345 OAK BOOKSHELF 59.99 4 rows selected.
In this output, there were five records that had a cost greater than the cost of all records having a cost less than 10.
The ANY operator is used to compare a value to any applicable value in the list according to the condition.
Example |
Meaning |
WHERE SALARY > ANY (SELECT SALARY FROM EMPLOYEE_TBL WHERE CITY = 'INDIANAPOLIS') |
Testing SALARY to see whether it is greater than any of the salaries of employees living in Indianapolis |
SELECT * FROM PRODUCTS_TBL WHERE COST > ANY ( SELECT COST FROM PRODUCTS_TBL WHERE COST < 10 ); PROD_ID PROD_DESC COST ---------- ------------------------------ ------ 11235 WITCHES COSTUME 29.99 222 PLASTIC PUMPKIN 18 INCH 7.75 13 FALSE PARAFFIN TEETH 1.1 90 LIGHTED LANTERNS 14.5 15 ASSORTED COSTUMES 10 9 CANDY CORN 1.35 6 PUMPKIN CANDY 1.45 119 ASSORTED MASKS 4.95 1234 KEY CHAIN 5.95 2345 OAK BOOKSHELF 59.99 10 rows selected.
In this output, more records were returned than when using ALL, because the cost only had to be greater than any of the costs that were less than 10. The one record that was not displayed had a cost of 1.05, which was not greater than any of the values less than 10 (which was, in fact, 1.05) .