Hour 8, Using Operators to Categorize Data

Team-Fly    

 
Sams Teach Yourself SQL in 24 Hours, Third Edition
By Ronald R. Plew, Ryan K. Stephens
Table of Contents
Appendix C.  Answers to Quizzes and Exercises


Hour 8, "Using Operators to Categorize Data"

Quiz Answers

1:

True or false: Both conditions when using the OR operator must be TRUE.

A1:

False. Only one of the conditions must be TRUE.

2:

True or false: All specified values must match when using the IN operator.

A2:

False. Only one of the values must match.

3:

True or false: The AND operator can be used in the SELECT and the WHERE clauses.

A3:

False. The AND can only be used in the WHERE clause.

4:

What, if anything, is wrong with the following SELECT statements?

  1.  SELECT SALARY  FROM EMPLOYEE_PAY_TBL WHERE SALARY BETWEEN 20000, 30000 
  2.  SELECT SALARY + DATE_HIRE  FROM EMPLOYEE_PAY_TBL 
  3.  SELECT SALARY, BONUS  FROM EMPLOYEE_PAY_TBL WHERE DATE_HIRE BETWEEN 1999-09-22 AND 1999-11-23 AND POSITION = 'SALES' OR POSITION = 'MARKETING' AND EMPLOYEE_ID LIKE '%55% 
A4:
  1.  SELECT SALARY  FROM EMPLOYEE_PAY_TBL WHERE SALARY BETWEEN 20000, 30000; 

    The AND is missing between 20000, 30000. The correct syntax is

     SELECT SALARY  FROM EMPLOYEE_PAY_TBL WHERE SALARY BETWEEN 20000 AND 30000; 
  2.  SELECT SALARY + DATE_HIRE  FROM EMPLOYEE_PAY_TBL; 

    The DATE_HIRE column is a DATE data type and is in the incorrect format for arithmetic functions.

  3.  SELECT SALARY, BONUS  FROM EMPLOYEE_PAY_TBL WHERE DATE_HIRE BETWEEN 1999-09-22 AND 1999-11-23 AND POSITION = 'SALES' OR POSITION = 'MARKETING' AND EMPLOYEE_ID LIKE '%55%'; 

    The syntax is correct.

Exercise Answers

1:

Using the following CUSTOMER_TBL:

  DESCRIBE CUSTOMER_TBL;  Name                            Null?    Type -------------------------------- -------- ------------  CUST_ID                         NOT NULL VARCHAR (10)  CUST_NAME                       NOT NULL VARCHAR (30)  CUST_ADDRESS                    NOT NULL VARCHAR (20)  CUST_CITY                       NOT NULL VARCHAR (12)  CUST_STATE                      NOT NULL VARCHAR(2)  CUST_ZIP                        NOT NULL VARCHAR(5)  CUST_PHONE                               VARCHAR (10)  CUST_FAX                                 VARCHAR(10) 

Write a SELECT statement that returns customer IDs and customer names (alpha order) for customers who live in Indiana, Ohio, Michigan, and Illinois, and whose names begin with the letters A or B.

A1:
 SELECT CUST_ID, CUST_NAME, CUST_STATE  FROM CUSTOMER_TBL WHERE CUST_STATE IN ('IN', 'OH', 'MI', 'IL') AND CUST_NAME LIKE 'A%' OR CUST_NAME LIKE 'B%' ORDER BY CUST_NAME; 
2:

Using the following PRODUCTS_TBL:

  DESCRIBE PRODUCTS_TBL  Name                            Null?    Type ------------------------------- --------------------- PROD_ID                         NOT NULL VARCHAR (10) PROD_DESC                       NOT NULL VARCHAR (25) COST                            NOT NULL DECIMAL(6,2) 

Write a SELECT statement that returns the product ID, PROD_DESC, and the product cost. Limit the product cost to range from $1.00 and $12.50.

A2:
 SELECT *  FROM PRODUCTS_TBL WHERE COST BETWEEN 1.00 AND 12.50; 
3:

Assuming that you used the BETWEEN operator in exercise 2 above, rewrite your SQL statement to achieve the same results using different operators. If you did not use the BETWEEN operator, then do so now.

A3:
 SELECT *  FROM PRODUCTS_TBL WHERE COST >= 1.00 AND COST <= 12.50; SELECT * FROM PRODUCTS_TBL WHERE COST BETWEEN 1.00 AND 12.50; 
4:

Write a SELECT statement that returns products that are either less than 1.00 or greater than 12.50. There are two ways to achieve the same results.

A4:
 SELECT *  FROM PRODUCTS_TBL WHERE COST < 1.00 AND COST > 12.50; SELECT * FROM PRODUCTS_TBL WHERE COST NOT BETWEEN 1.00 AND 12.50; 

Also keep in mind that BETWEEN is inclusive of the upper and lower values, whereas NOT BETWEEN is not inclusive.

5:

Write a SELECT statement that returns the following information from PRODUCTS_TBL: product description, product cost, and 5% sales tax for each product. List the products in order from most to least expensive.

A5:
 SELECT PROD_DESC, COST, COST * .05  FROM PRODUCTS_TBL ORDER BY COST DESC; 
6:

Write a SELECT statement that returns the following information from PRODUCTS_TBL: product description, product cost, and 5% sales tax for each product, and total cost with sales tax. List the products in order from most to least expensive. There are two ways to achieve the same results. Try both.

A6:
 SELECT PROD_DESC, COST, COST * .05, COST + (COST * .O5)  FROM PRODUCTS_TBL ORDER BY COST DESC; SELECT PROD_DESC, COST, COST * .05, COST * 1.O5) FROM PRODUCTS_TBL ORDER BY COST DESC; 

Team-Fly    
Top
 


Sams Teach Yourself SQL in 24 Hours
Sams Teach Yourself SQL in 24 Hours (5th Edition) (Sams Teach Yourself -- Hours)
ISBN: 0672335417
EAN: 2147483647
Year: 2002
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net