Section 2 ORACLE Database Programming Additional Review for Section 2 Answer Key

S. Sherman, E. Williams Academy Instructors

1. What are the two functions you should be able to accomplish after Section 2? LIMIT THE ROWS RETRIEVED BY THE QUERY SORT THE ROWS RETRIEVED BY THE QUERY

2. What clause do you use to restrict the rows of data in a search? WHERE

3. Where in the script is this clause placed? AFTER THE FROM CLAUSE

4. What three things does a WHERE clause consist of? COLUMN NAME, COMPARISON CONDITION, COLUMN NAME, CONSTANT, OR LIST OF VALUES 5. What are the four comparisons a WHERE clause can complete? COMPARE VALUES IN COLUMNS, LITERAL VALUES, ARITHMETIC EXPRESSIONS, FUNCTIONS

6. True or FALSE: WHERE job_id = SA_REP and WHERE job_id = Sa_Rep will return the same result.

7. Of the three comparisons a WHERE clause can do, list each as well as any special marks that must accompany each. NUMERIC VALUE – NO MARKS CHARACTER VALUE – SINGLE QUOTES ( ‘ ) DATE VALUE – SINGLE QUOTES ( ‘ ) 8. What can NOT be used in a WHERE clause? A COLUMN ALIAS 9. Write a WHERE clause for each of the ten (10) Comparison Conditions. Make sure that each clause you write is correct in terms of format and markings (such as quotation marks). You must use at least one date comparison, one numeric comparison, and one character comparison. ANSWERS WILL VARY

WHERE job_id = 10 WHERE salary > 2500 WHERE hire_date >= ’01-JAN-95’ WHERE salary < 2500 WHERE job_id <= ’01-JAN-98’ WHERE last_name <> ‘KING’ WHERE job_id BETWEEN 10 and 50 WHERE department_id IN (90, 100, 110) WHERE last_name LIKE ‘_o%’ WHERE commission_pct IS NULL Section 2 ORACLE Database Programming Additional Review for Section 2 Answer Key

10. Write today’s date using the Default Oracle Date format. 23-JAN-04

11. Suppose you wanted to find all rows containing commission percentages of 15%, 20%, or 25%. Which Comparison Condition would you use? THE IN CONDITION

12. If you wanted to find the employees with the salary between $2000 and $4000, and you wrote the following select statement, it would fail. Give the two reasons why this statement is not accurate based on your desired results. SELECT last_name, salary FROM employees WHERE salary BETWEEN 4001 and 2499; VALUES NEED TO GO FROM LOWEST TO HIGHEST AND ARE INCLUSIVE

13. TRUE or False: The IN condition can be used with any data type (date, character, number).

14. TRUE or False: When using the IN condition, you must use parenthesis ( ) to list comparison values.

15. True or FALSE: When using the IN condition, you must use single quotes ( ‘ ) for numeric values.

16. Which Comparison Condition allows you to search for a partial character string? THE LIKE CONDITION

17. What two things can be included in this search condition? A % WHICH DENOTES ZERO OR MANY CHARACTERS A _ WHICH DENOTES ONE AND ONLY ONE CHARACTER

18. True or False: Using literal characters to search for conditions is not case sensitive.

19. True or FALSE: The following two scripts would return the exact same value:

SELECT last_name, hire_date FROM employees WHERE hire_date LIKE ‘%95’;

SELECT last_name, hire_date FROM employees WHERE hire_date BETWEEN ‘01-JAN-95’ AND ‘01-DEC-95’; Section 2 ORACLE Database Programming Additional Review for Section 2 Answer Key

20. What results would the following query return:

SELECT last_name, hire_date FROM employees WHERE last_name LIKE ‘_i%’; ALL LAST NAMES AND HIRE DATES FOR ALL EMPLOYEES WITH AN “i” AS THE SECOND CHARACTER OF THEIR LAST NAME

21. What are the three Logical Conditions to restrict results? AND OR NOT

22. What does a Logical Condition do? COMBINES THE RESULT OF TWO COMPONENT CONDITIONS TO PRODUCE A SINGLE RESULT BASED ON THEM

23. When is a row returned when using Logical Conditions? ONLY IF THE OVERALL RESULT OF THE CONDITION IS TRUE

24. What does the AND condition mean or return? RETURNS TRUE IF BOTH CONDITIONS ARE TRUE

25. What does the OR condition mean or return? RETURNS TRUE IF EITHER CONDITION IS TRUE

26. What does the NOT condition mean or return? RETURNS TRUE IF THE FOLLOWING IS FALSE

27. Where do you put a Logical Condition in a statement? AFTER THE WHERE CLAUSE

28. How many conditions can you have in one WHERE clause? AS MANY AS NECESSARY TO ACHIEVE RESULTS

29. TRUE or False: When using the AND operator, all character string searches are case sensitive.

30. True or FALSE: When using the AND operator, if the first condition is true and the second condition is false, the row will be returned.

31. TRUE or False: When using the OR operator, if the first condition is true and the second condition is null, the row will be returned. Section 2 ORACLE Database Programming Additional Review for Section 2 Answer Key 32. Which operator is more likely to return rows, the AND operator or the OR operator? OR

33. True or FALSE: You must have at least two conditions for the NOT operator to function properly.

34. True or FALSE: The order of precedence is fixed and cannot be modified.

35. What is the default order of rows when returning rows in a query? IT IS UNDEFINED OR RANDOM (SOME STUDENTS PUT ASCENDING BECAUSE IT IS NOT CLARIFIED IN THE QUESTION THAT YOU ARE NOT USING AN ORDER BY CLAUSE)

36. What clause do you use to sort rows returned? ORDER BY

37. Where must this clause be placed? IT MUST BE THE LAST CLAUSE OF THE STATEMENT

38. What can you specify as the sort condition? AN EXPRESSION OR ALIAS, OR COLUMN POSITION

39. TRUE or False: If you do not use the ORDER BY clause, and you run the same query twice, you could get two different orders of rows returned.

40. If you type this line: ORDER BY hire_date Which would come first, 24-MAR-03, 12-DEC-03, 29-JAN-04?

41. If you typed this line: ORDER BY commission_pct Which would come first, .20, .15, null

42. True or FALSE: You can use a column alias in the ORDER BY clause if you have not used an alias in the SELECT clause.

43. What is the maximum number of columns you can sort data by? THE NUMBER OF COLUMNS IN THE TABLE

44. True or FALSE: You can only sort by columns listed in the SELECT clause of your query.

45. True or FALSE: When sorting by more than one column, all columns need to be ascending or descending order, you cannot mix. Section 2 ORACLE Database Programming Additional Review for Section 2 Answer Key

46. What will the output be of this statement (be very specific): SELECT last_name, salary * .1 AS raises FROM employees WHERE job_id LIKE '%SA\_%' ESCAPE '\' AND raises >2000 ORDER BY raises DESC, job_id YOU WOULD HAVE AN ERROR RETURNED AS YOU CAN NOT HAVE AN ALIAS IN THE WHERE STATEMENT (AND raises > 2000). IF YOU HAD THIS FIXED, YOUR RESULT WOULD BE THE LAST NAME AND SALARY TIMES 10% FOR ALL EMPLOYEES WHOSE JOB TITLE CONTAINS SA_ AND HAS A NEW SALARY (RAISES) OF $2000 OR MORE SORTED IN DESCENDING ORDER BY NEW SALARY (RAISES) AND THEN BY THE JOB TITLE.