Quiz Answers
1. Name the required parts for any SELECTstatement.
A. TheSELECTandFROMkeywords, also called clauses, are required for all SELECTstatements.
2. In the WHEREclause, are single quotation marks required for all the data?
A. No. Single quotation marks are required when selecting alphanumeric data types. Number data types do not require single quotation marks.
3. Under what part of the SQL language does the SELECTstatement (database query) fall?
A. TheSELECTstatement is considered Data Query Language.
4. Can multiple conditions be used in the WHEREclause?
A. Yes. Multiple conditions can be specified in the WHEREclause of SELECT,INSERT,UPDATE, and DELETEstatements. Multiple condi- tions are used with the operators ANDandOR, which are thor- oughly discussed in Hour 8, “Using Operators to Categorize Data.”
5. What is the purpose of the DISTINCToption?
A. TheDISTINCToption suppresses the display of duplicates.
6. Is the ALLoption required?
A. No. Even though the ALLoption can be used, it is not really required.
7. How are numeric characters treated when ordering based upon a character field?
A. They are sorted as ASCII characters. This means that numbers would be ordered like this: 1, 12, 2, 222, 22222, 3, 33.
8. How does Oracle handle its default case sensitivity differently from MySQL and Microsoft SQL Server?
A. Oracle by default performs matches as case sensitive.
Hour 7, “Introduction to the Database Query” 405
Exercise Answers
1. Invoke your RDBMS query editor on your computer. Using your learnsqldatabase, enter the following SELECTstatements.
Determine whether the syntax is correct. If the syntax is incorrect, make corrections to the code as necessary. We are using the EMPLOYEE_TBLhere.
a. SELECT EMP_ID, LAST_NAME, FIRST_NAME, FROM EMPLOYEE_TBL;
A. ThisSELECTstatement does not work because there is a comma after the FIRST_NAMEcolumn that does not belong there. The correct syntax follows:
a. SELECT EMP_ID, LAST_NAME, FIRST_NAME FROM EMPLOYEE_TBL;
b. SELECT EMP_ID, LAST_NAME ORDER BY EMP_ID
FROM EMPLOYEE_TBL;
A. ThisSELECTstatement does not work because the FROMandORDER BY clauses are in the incorrect order. The correct syntax follows:
SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TBL ORDER BY EMP_ID;
c. SELECT EMP_ID, LAST_NAME, FIRST_NAME FROM EMPLOYEE_TBL
WHERE EMP_ID = ‘213764555’
ORDER BY EMP_ID;
A. The syntax for this SELECTstatement is correct.
d. SELECT EMP_ID SSN, LAST_NAME FROM EMPLOYEE_TBL
WHERE EMP_ID = ‘213764555’
ORDER BY 1;
A. The syntax for this SELECTstatement is correct. Notice that the EMP_IDcolumn is renamed SSN.
e. SELECT EMP_ID, LAST_NAME, FIRST_NAME FROM EMPLOYEE_TBL
WHERE EMP_ID = ‘213764555’
ORDER BY 3, 1, 2;
A. The syntax for this SELECTstatement is correct. Notice the order of the columns in the ORDER BY. This SELECTstatement returns records from the database that are sorted by
FIRST_NAME, and then by EMP_ID, and finally by LAST_NAME. 2. Does the following SELECTstatement work?
SELECT LAST_NAME, FIRST_NAME, PHONE FROM EMPLOYEE_TBL
WHERE EMP_ID = ‘333333333’;
A. The syntax is correct and the statement worked, even though no data was returned. No data was returned because there was no row with an EMP_IDof333333333.
3. Write a SELECTstatement that returns the name and cost of each product from the PRODUCTS_TBL. Which product is the most expen- sive?
A. SELECT PROD_DESC,COST FROM PRODUCTS_TBL;
The witch costume is the most expensive.
4. Write a query that generates a list of all customers and their tele- phone numbers.
A. SELECT CUST_NAME,CUST_PHONE FROM CUSTOMER_TBL;
5. Answers will vary.
Hour 8, “Using Operators to Categorize Data”
Quiz Answers
1. True or false: Both conditions when using the ORoperator must be TRUE.
A. False. Only one of the conditions must be TRUE.
2. True or false: All specified values must match when using the IN operator.
A. False. Only one of the values must match.
3. True or false: The ANDoperator can be used in the SELECTand the WHEREclauses.
Hour 8, “Using Operators to Categorize Data” 407
A. False. The ANDoperator can only be used in the WHEREclause.
4. True or false: The ANYoperator can accept an expression list.
A. False. TheANYoperator cannot take an expression list.
5. What is the logical negation of the INoperator?
A. NOT IN.
6. What is the logical negation of the ANYandALLoperators?
A. <>ANYand<>ALL.
7. What, if anything, is wrong with the following SELECTstatements?
a. SELECT SALARY
FROM EMPLOYEE_PAY_TBL
WHERE SALARY BETWEEN 20000, 30000;
A. TheANDis missing between 20000, 30000. The correct syntax is
SELECT SALARY
FROM EMPLOYEE_PAY_TBL
WHERE SALARY BETWEEN 20000 AND 30000;
b. SELECT SALARY + DATE_HIRE FROM EMPLOYEE_PAY_TBL;
A. TheDATE_HIREcolumn is a DATEdata type and is in the incor- rect format for arithmetic functions.
c. 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 EMP_ID LIKE ‘%55%’;
A. The syntax is correct.
Exercise Answers
1. Using the following CUSTOMER_TBL, write a SELECTstatement that returns customer IDs and customer names (alpha order) for cus- tomers who live in Indiana, Ohio, Michigan, and Illinois, and whose names begin with the letters A or B:
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)
A. 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, write a SELECTstatement that returns the product ID, product description, and product cost. Limit the product cost to between $1.00 and $12.50:
DESCRIBE PRODUCTS_TBL
Name Null? Type
--- --- --- PROD_ID NOT NULL VARCHAR (10) PROD_DESC NOT NULL VARCHAR (25) COST NOT NULL DECIMAL(6,2)
A. SELECT *
FROM PRODUCTS_TBL
WHERE COST BETWEEN 1.00 AND 12.50;
3. Assuming that you used the BETWEENoperator in Exercise 2, rewrite your SQL statement to achieve the same results using different operators. If you did not use the BETWEENoperator, do so now.
A. 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 SELECTstatement that returns products that are either less than1.00or greater than 12.50. There are two ways to achieve the same results.
Hour 8, “Using Operators to Categorize Data” 409
A. SELECT *
FROM PRODUCTS_TBL
WHERE COST < 1.00 OR COST > 12.50;
SELECT *
FROM PRODUCTS_TBL
WHERE COST NOT BETWEEN 1.00 AND 12.50;
Also keep in mind that BETWEENis inclusive of the upper and lower values, whereas NOT BETWEENis not inclusive.
5. Write a SELECTstatement that returns the following information fromPRODUCTS_TBL: product description, product cost, and 5% sales tax for each product. List the products in order from most to least expensive.
A. SELECT PROD_DESC, COST, COST * .05 FROM PRODUCTS_TBL
ORDER BY COST DESC;
6. Write a SELECTstatement that returns the following information fromPRODUCTS_TBL: product description, product cost, 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.
A. SELECT PROD_DESC, COST, COST * .05, COST + (COST * .05) FROM PRODUCTS_TBL
ORDER BY COST DESC;
SELECT PROD_DESC, COST, COST * .05, COST * 1.05 FROM PRODUCTS_TBL
ORDER BY COST DESC;
7. Pick three items from the PRODUCTS table. Now write a query to return the rows of data from the table associated with those three items. Now rewrite the query to return everything but those three items. For your query, use combinations of equality operators and conjunctive operators.
A. SELECT *
FROM PRODUCTS_TBL WHERE PROD_ID=11235 OR PROD_ID=119 OR PROD_ID=13;
SELECT *
FROM PRODUCTS_TBL
WHERE PROD_ID<>11235 AND PROD_ID<>119 AND PROD_ID<>13;
8. Rewrite the queries you wrote in Exercise 7 using the INoperator.
Which statement is more efficient? Which one is more readable?
A. SELECT * FROM PRODUCT_TBL
WHERE PROD_ID IN (11235,119,13);
SELECT *
FROM PRODUCT_TBL
WHERE PROD_ID IN (11235,119,13);
9. Write a query to return all the products that start with the letter P.
Now write a query to return all products that do notstart with the letterP.
A. SELECT *
FROM PRODUCTS_TBL
WHERE PROD_DESC LIKE (‘P%’);
SELECT * FROM PRODUCTS_TBL
WHERE PROD_DESC NOT LIKE (‘P%’);