Sams Teach Yourself SQL in 24 Hours
- 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
- Part II: Building Your Database
- Hour 2. Defining Data Structures
- Hour 3. Managing Database Objects
- Hour 4. The Normalization Process
- Hour 5. Manipulating Data
- Hour 6. Managing Database Transactions
- Part III: Getting Effective Results from Queries
- 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 Times
- Part IV: Building Sophisticated Database Queries
- Hour 13. Joining Tables in Queries
- Hour 14. Using Subqueries to Define Unknown Data
- Hour 15. Combining Multiple Queries into One
- Part V: SQL Performance Tuning
- Hour 16. Using Indexes to Improve Performance
- Hour 17. Improving Database Performance
- Part VI: Using SQL to Manage Users and Security
- Hour 18. Managing Database Users
- Hour 19. Managing Database Security
- Part VII: Summarized Data Structures
- Hour 20. Creating and Using Views and Synonyms
- Hour 21. Working with the System Catalog
- Part VIII: Applying SQL Fundamentals in Today's World
- Hour 22. Advanced SQL Topics
- Hour 23. Extending SQL to the Enterprise, the Internet, and the Intranet
- Hour 24. Extensions to Standard SQL
- Part IX: Appendixes
- Appendix A. Common SQL Commands
- Appendix B. Using MySQL for Exercises
- Appendix C. Answers to Quizzes and Exercises
- Appendix D. CREATE TABLE Statements for Book Examples
- Appendix E. INSERT Statements for Data in Book Examples
- Appendix F. Glossary
- Appendix G. Bonus Exercises
Workshop
The following workshop is composed of a series of quiz questions and practical exercises. The quiz questions are designed to test your overall understanding of the current material. The practical exercises are intended to afford you the opportunity to apply the concepts discussed during the current hour, as well as build upon the knowledge acquired in previous hours of study. Please take time to complete the quiz questions and exercises before continuing. Refer to Appendix C,"Answers to Quizzes and Exercises," for answers.
Quiz
-
What is the function of a subquery when used with a SELECT statement?
-
Can you update more than one column when using the UPDATE statement in conjunction with a subquery?
-
Are the following syntax(s) correct? If not, what is the correct syntax?
-
SELECT CUST_ID, CUST_NAME FROM CUSTOMER_TBL WHERE CUST_ID = (SELECT CUST_ID FROM ORDERS_TBL WHERE ORD_NUM = '16C17'); -
SELECT EMP_ID, SALARY FROM EMPLOYEE_PAY_TBL WHERE SALARY BETWEEN '20000' AND (SELECT SALARY FROM EMPLOYEE_ID WHERE SALARY = '40000'); -
UPDATE PRODUCTS_TBL SET COST = 1.15 WHERE CUST_ID = (SELECT CUST_ID FROM ORDERS_TBL WHERE ORD_NUM = '32A132');
-
-
What would happen if the following statement were run?
DELETE FROM EMPLOYEE_TBL WHERE EMP_ID IN (SELECT EMP_ID FROM EMPLOYEE_PAY_TBL);
Exercises
In this exercise section, refer to the Oracle syntax of suqueries covered in this hour. Write the Oracle SQL code for the requested subqueries by hand on a sheet of paper and compare your results to ours. Use the following tables to complete the exercises:
|
EMPLOYEE_TBL |
|||
|
EMP_ID |
VARCHAR(9) |
NOT NULL |
PRIMARY KEY |
|
LAST_NAME |
VARCHAR(15) |
NOT NULL |
|
|
FIRST_NAME |
VARCHAR(15) |
NOT NULL |
|
|
MIDDLE_NAME |
VARCHAR(15) |
||
|
ADDRESS |
VARCHAR(30) |
NOT NULL |
|
|
CITY |
VARCHAR(15) |
NOT NULL |
|
|
STATE |
VARCHAR(2) |
NOT NULL |
|
|
ZIP |
INTEGER(5) |
NOT NULL |
|
|
PHONE |
VARCHAR(10) |
||
|
PAGER |
VARCHAR(10) |
|
EMPLOYEE_PAY_TBL |
|||
|
EMP_ID |
VARCHAR(9) |
NOT NULL |
PRIMARY KEY |
|
POSITION |
VARCHAR(15) |
NOT NULL |
|
|
DATE_HIRE |
DATETIME |
||
|
PAY_RATE |
DECIMAL(4,2) |
NOT NULL |
|
|
DATE_LAST_RAISE DATETIME |
|||
|
CONSTRAINT EMP_FK FOREIGN KEY (EMP_ID_ REFERENCES EMPLOYEE_TBL (EMP_ID) |
|
CUSTOMER_TBL |
|||
|
CUST_ID |
VARCHAR(10) |
NOT NULL |
PRIMARY KEY |
|
CUST_NAME |
VARCHAR(30) |
NOT NULL |
|
|
CUST_ADDRESS |
VARCHAR(20) |
NOT NULL |
|
|
CUST_CITY |
VARCHAR(15) |
NOT NULL |
|
|
CUST_STATE |
VARCHAR(2) |
NOT NULL |
|
|
CUST_ZIP |
INTEGER(5) |
NOT NULL |
|
|
CUST_PHONE |
INTEGER(10) |
||
|
CUST_FAX |
INTEGER(10) |
|
ORDERS_TBL |
|||
|
ORD_NUM |
VARCHAR(10) |
NOT NULL |
PRIMARY KEY |
|
CUST_ID |
VARCHAR(10) |
NOT NULL |
|
|
PROD_ID |
VARCHAR(10) |
NOT NULL |
|
|
QTY |
INTEGER(6) |
NOT NULL |
|
|
ORD_DATE |
DATETIME |
|
PRODUCTS_TBL |
|||
|
PROD_ID |
VARCHAR(10) |
NOT NULL |
PRIMARY KEY |
|
PROD_DESC |
VARCHAR(40) |
NOT NULL |
|
|
COST |
DECIMAL(6,2) |
NOT NULL |
-
Using a subquery, write an SQL statement to update the CUSTOMER_TBL table, changing the customer name to DAVIDS MARKET, who has an order with order number 23E934.
-
Using a subquery, write a query that returns all the names of all employees who have a pay rate greater than JOHN DOE, whose employee identification number is 343559876.
-
Using a subquery, write a query that lists all products that cost more than the average cost of all products.
Hour 15. Combining Multiple Queries into One | Next Section

Account Sign In
View your cart