Top Banner
Oracle Academy Database Programming with SQL Instructor Resource Guide Oracle Academy 1 Database Programming with SQL INSTRUCTOR NOTES SECTION 1 LESSON 1 – Case and Character Manipulation Slide 1: Case and Character Manipulation No instructor notes for this slide Slide 2: What Will I Learn? No instructor notes for this slide Slide 3: Why Learn It? Ask students for other ways in which we transform ourselves. Possible responses: Many professions wear uniforms; your rough draft of a paper doesn’t usually look like what you eventually turn in. Slide 4: Why Learn It? (continued) No instructor notes for this slide Slide 5: Tell Me / Show Me – DUAL TABLE Begin this lesson with a review question: What are the five types of single-row functions? Answer: Character, Number, Date, Conversion, and General Slide 6: Tell Me / Show Me – DUAL TABLE (continued) Demonstrate and practice the following functions using the DUAL table. 1. Create a query that outputs the CD titles in the DJ on Demand database in all lowercase letters. SELECT LOWER(title) FROM d_cds; 2. Create a query that selects the first names of the DJ on Demand clients who have an “a” somewhere in their name. Output the results set in all uppercase letters. Ask students why UPPER was put in the SELECT statement and not in the WHERE clause. SELECT UPPER(first_name) FROM d_clients WHERE first_name LIKE '%a%'; Slide 7: Tell Me / Show Me – SINGLE-ROW CHARACTER FUNCTIONS Single row functions are very powerful pre-defined code that accepts arguments and returns a value. An argument can be defined as a column name, an expression, or a constant. There are five single row function groups: Character Number Date Conversion Copyright © 2009, Oracle. All rights reserved.
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 1 Database Programming with SQL

INSTRUCTOR NOTES SECTION 1 LESSON 1 – Case and Character Manipulation Slide 1: Case and Character Manipulation No instructor notes for this slide Slide 2: What Will I Learn? No instructor notes for this slide Slide 3: Why Learn It? Ask students for other ways in which we transform ourselves. Possible responses: Many professions wear uniforms; your rough draft of a paper doesn’t usually look like what you eventually turn in. Slide 4: Why Learn It? (continued) No instructor notes for this slide Slide 5: Tell Me / Show Me – DUAL TABLE Begin this lesson with a review question: What are the five types of single-row functions? Answer: Character, Number, Date, Conversion, and General Slide 6: Tell Me / Show Me – DUAL TABLE (continued) Demonstrate and practice the following functions using the DUAL table.

1. Create a query that outputs the CD titles in the DJ on Demand database in all lowercase letters. SELECT LOWER(title) FROM d_cds;

2. Create a query that selects the first names of the DJ on Demand clients who have an “a” somewhere in their name. Output the results set in all uppercase letters. Ask students why UPPER was put in the SELECT statement and not in the WHERE clause.

SELECT UPPER(first_name) FROM d_clients WHERE first_name LIKE '%a%';

Slide 7: Tell Me / Show Me – SINGLE-ROW CHARACTER FUNCTIONS Single row functions are very powerful pre-defined code that accepts arguments and returns a value. An argument can be defined as a column name, an expression, or a constant. There are five single row function groups: Character Number Date Conversion

Copyright © 2009, Oracle. All rights reserved.

Page 2: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 2 Database Programming with SQL

General Slide 8: Tell Me / Show Me – SINGLE-ROW CHARACTER FUNCTIONS (continued) No instructor notes for this slide Slide 9: Tell Me / Show Me – CASE-MANIPULATION FUNCTIONS No instructor notes for this slide Slide 10: Tell Me / Show Me – CASE-MANIPULATION FUNCTIONS (continued) The reason Oracle differentiates between ‘V’ and ‘v’ is due to the way it does the storing of characters. It does not store the chars directly, but their corresponding binary values, depending of the character set of the database. In most of the Western world an ACSII character set will have been used as the database character set, and quite simple the binary codes for ‘V’ and ‘v’ are different numbers, and therefore Oracle does not consider them to be equal. If you have time, consider having the students doing a search on the internet for ASCII. Wikipedia have the following definition: ASCII: American Standard Code for Information Interchange), is a character encoding based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that work with text. Slide 11: Tell Me / Show Me – CASE-MANIPULATION FUNCTIONS (continued) No instructor notes for this slide Slide 12: Tell Me / Show Me – CHARACTER-MANIPULATION FUNCTIONS Demonstrate and practice each character-manipulation function shown in the graphic function/result table. Substitute other character strings and numeric values in the SELECT clause. SELECT CONCAT('Hello', 'World') FROM DUAL; SELECT SUBSTR('HelloWorld', 1, 5) FROM DUAL; SELECT LENGTH('HelloWorld') FROM DUAL; SELECT INSTR('HelloWorld', 'W') FROM DUAL; SELECT last_name, LPAD(salary,10,'*') FROM employees; SELECT first_name, RPAD(salary,10,'*') FROM employees; SELECT TRIM('H' FROM 'HelloWorld') FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 3: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 3 Database Programming with SQL

Slide 13: Tell Me / Show Me – CHARACTER-MANIPULATION FUNCTIONS (continued) No instructor notes for this slide Slide 14: Tell Me / Show Me – CHARACTER-MANIPULATION FUNCTIONS (continued) No instructor notes for this slide Slide 15: Tell Me / Show Me – All functions operate on values that are in… Explain the use of aliases in functions to eliminate the function syntax appearing as the column heading. Drill and practice are essential for learning the functions. Slide 16: Tell Me / Show Me – All functions operate on values that are in… (continued) The use of aliases as column headings and why they are important in queries using functions. This slide has the same example one with a column alias and one without. Slide 17: Tell Me / Show Me – Substitution Variables The use of substitution variables, what they are for, how to use them. Do remind your students that the datatypes are as always important, and if we are using character strings or dates, they need to be enclosed in single quotes, exactly the same as if the values were hardcoded into the where-clause. The quotes ‘’ should be entered into the query for ease of use, but quotes can also be entered on the Variable Input pop-up. Be sure to demonstrate the effect of a char string where-clause substitution variable with and without quotes. Slide 18: Tell Me / Show Me – Substitution Variables (continued) This slide shows the use of substitution variables, but lots of practice is needed to be comfortable using substitution variables. Slide 19: Tell Me / Show Me – Substitution Variables (continued) This slide shows more substitution variables. NOTE: Pop-Up blockers must be disabled, otherwise OAE cannot ask for the variable value, as this is entered via a pop-up. Slide 20: Tell Me / Show Me – Terminology DUAL: Dummy table used to view results from functions and calculations Format: The arrangement of data for storage or display. INITCAP: Converts alpha character values to uppercase for the first letter of each word, all other letters in lowercase. Character functions: Functions that accept character data as input and can return both character and numeric values. TRIM: Removes all specified characters from either the beginning or the ending of a string. Expression: A symbol that represents a quantity or a relationship between quantities

Copyright © 2009, Oracle. All rights reserved.

Page 4: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 4 Database Programming with SQL

Single-row functions: Functions that operate on single rows only and return one result per row UPPER: Converts alpha characters to upper case Input: Raw data entered into the computer CONCAT: Concatenates the first character value to the second character value; equivalent to concatenation operator (||). Output: Data that is processed into information LOWER: Converts alpha character values to lowercase. LPAD: Pads the left side of a character, resulting in a right-justified value SUBSTR: Returns specific characters from character value starting at a specific character position and going specified character positions long REPLACE: Replaces a sequence of characters in a string with another set of characters. INSTR: Returns the numeric position of a named string. LENGTH: Returns the number of characters in the expression RPAD: Pads the right-hand side of a character, resulting in a left- justified value. Slide 21: Summary – In this lesson you have learned to: No instructor notes for this slide Slide 22: Summary – Practice Guide No instructor notes for this slide

Copyright © 2009, Oracle. All rights reserved.

Page 5: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 5 Database Programming with SQL

SECTION 1 LESSON 2 – Number Functions Slide 1: Number Functions What to Watch For Check for student understanding of each function. Remind students that all of the functions operate on a value in parentheses. Connections Relate the use and importance of numeric data to everyday student life. Show how this information could be stored in a database table. Ask students to respond to each of the following questions and give an example of the decisions that would need to be made about storing this number in a database.

1. Exactly how old are you? Possible response: 16 years 3 months, Database storage 16.25 or 16 or 16.00;

2. How much money do you have in your wallet? Possible response depending on currency: $4.73, Database storage 4.73 or 5.00 or 5;

3. What did you score on one exam this year? Possible response: 73%, Database storage 73 or 73.00;

4. Were you born in an odd year or an even year? Possible response: 1989 or 1990, Database storage 1989%2 as odd or 1990%2 as even.

Slide 2: What Will I Learn? No instructor notes for this slide Slide 3: Why Learn It? No instructor notes for this slide Slide 4: Why Learn It? (continued) No instructor notes for this slide Slide 5: Tell Me / Show Me – As you might expect, the… Explain the ROUND, TRUNC, and MOD functions. Ask students to access the computer calculator. Do a simple division problem that has a remainder such as 35/13 and note the output. The large number of places shown to the right of the decimal may be what a machinist needs when making precision parts, but for DJ on Demand’s calculation of a customer’s bill, it’s not. Slide 6: Tell Me / Show Me – ROUND: Can be used with both numbers and… No instructor notes for this slide

Copyright © 2009, Oracle. All rights reserved.

Page 6: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 6 Database Programming with SQL

Slide 7: Tell Me / Show Me – ROUND Demonstrate the ROUND function. Practice using negative and zero values for ROUND. SELECT ROUND((7547.1698 * 1.75),2) AS "Total" FROM DUAL; Slide 8: Tell Me / Show Me – ROUND (continued) No instructor notes for this slide Slide 9: Tell Me / Show Me – TRUNC No instructor notes for this slide Slide 10: Tell Me / Show Me –TRUNC (continued) No instructor notes for this slide Slide 11: Tell Me / Show Me – MOD Ask students what the MOD of 100/25 is. 0 Ask students what the MOD of 100/30 is. 10 Ask students what the MOD of 18.5/3 is. .5 Slide 12: Tell Me / Show Me – MOD (continued) No instructor notes for this slide Slide 13: Tell Me / Show Me – Terminology TRUNC: Used to terminate the column, expression, or value to a specified number of decimal places Number functions: These functions accept numeric input and return numeric values. MOD: Returns the remainder of a division. ROUND: Rounds the column, expression, or value to a set number of decimal places. Slide 14: Summary – In this lesson you have learned to: No instructor notes for this slide Slide 15: Summary – Practice Guide No instructor notes for this slide

Copyright © 2009, Oracle. All rights reserved.

Page 7: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 7 Database Programming with SQL

SECTION 1 LESSON 3 – Date Functions Slide 1: Date Functions What to Watch For This lesson adds many new functions. Students need ways to organize them and to practice using them. Check for correct solutions. Many queries will execute, but the result set is not what was desired. Connections In data modeling, you will recall, modeling time and changes over time were important considerations in developing the ERD. It was necessary to decide whether “time” should be an entity because the day was what we were interested in or should “time” be an attribute when we were interested only in the tracking “time” as it relates to another business function. If a business decides that it is important only to track time as it relates to another business function, such as the hire dates and retire dates of employees, then in a database, “time” would be a column in a table. However, if time is modeled as an entity, time would be a table in a database with columns of attributes important to the business. A “time” table could be a database for a train schedule or manufacturing schedule. Columns in a time table could be input_hour, assembly_time, shipping_time, departure_time, arrival_time. Slide 2: What Will I Learn? No instructor notes for this slide Slide 3: Why Learn It? No instructor notes for this slide Slide 4: Tell Me / Show Me – DATE Begin this lesson with a review question: If a swimming event was clocked at 2.147 minutes, but in our results we wanted it to show as 2.15, how would we write the SELECT statement? SELECT ROUND(2.147,2) FROM DUAL; Read and discuss with students how dates are stored in a database. Because they are stored as numbers, arithmetic operations can be performed on them Slide 5: Tell Me / Show Me – When a record with a date column… No instructor notes for this slide Slide 6: Tell Me / Show Me – The DATE data type always stores… No instructor notes for this slide

Copyright © 2009, Oracle. All rights reserved.

Page 8: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 8 Database Programming with SQL

Slide 7: Tell Me / Show Me – The date functions shown in the … Ask students to find the months between today and last year on the same day. Ask them to explain why the number is negative. Explain that the MONTHS_BETWEEN will return a negative number if the more recent day is stated last in the statement – for example, ( '04-NOV-01', SYSDATE). The following example rounds the result to zero decimal places when no rounding value is specified. SELECT ROUND(MONTHS_BETWEEN( '04-JAN-04', SYSDATE)) AS "MONTHS UNTIL MY BIRTHDAY" FROM DUAL; Slide 8: Tell Me / Show Me – The following query shows how the… No instructor notes for this slide Slide 9: Tell Me / Show Me – SELECT employee_id, hire_date… No instructor notes for this slide Slide 10: Tell Me /Show Me – Terminology SYSDATE: These functions accept numeric input and return numeric values. ADD_MONTHS: Add calendar months to date LAST_DAY: Last day of the month NEXT_DAY: Next day of the date specified MONTHS_BETWEEN: Number of months between due dates Slide 11: Summary – In this lesson you have learned to: No instructor notes for this slide Slide 12: Summary – Practice Guide No instructor notes for this slide

Copyright © 2009, Oracle. All rights reserved.

Page 9: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 9 Database Programming with SQL

SECTION 1 LESSON 4 – Interest and Aptitudes and Career Exploration Slide 1: Interest and Aptitudes and Career Exploration Lesson Preparation Preview the list of Internet sites suggested below ensuring that students have access. Select those sites most appropriate for your class. Many Internet resources are appropriate for this lesson. Use key terms to search at http://www.google.com or http://www.yahoo.com that include: careers, job interest inventories, jobs. Review the Career Explorations Worksheet. Interest Inventory and Aptitude Sites: www.careerexplorer.net/aptitude.asp http://career.missouri.edu http://www.myfuture.com/ http://www.careerkey.org/english/ http://www.cmi-lmi.com/kingdomality.html http://www.uncwil.edu/stuaff/career/students/assessments.htm http://www.princetonreview.com/cte/quiz/career_quiz1.asp http://www.jobweb.com/ http://www.funeducation.com/PersonalityProfile/ Career Exploration: http://www.careervoyages.gov/index.cfm http://stats.bls.gov http://www.dice.com http://career-advice.monster.com/job-search-essentials/technology/home.aspx http://www.it-careers.ca/ http://www.computerworld.com/careertopics/careers http://www.jobprofiles.org/ http://www.acinet.org/acinet/default.asp http://www.martynemko.com/pub/excerpts/Cool_Careers.shtm Students who completed the Database Fundamentals may have finished one or more interest inventories. If not, start this section with two or three of the URL addresses listed or obtain written copies of career and interest inventories from the career counselor, if available. What to Watch For This is the time to be realistic with students about the qualifications for careers that they may be interested in pursuing. Help them look at the courses they’ve completed successfully. Have them look at challenging courses they have taken, such as advanced

Copyright © 2009, Oracle. All rights reserved.

Page 10: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 10 Database Programming with SQL

mathematics, physics, chemistry, writing, and foreign languages. Course choices and achievement are good indicators of student interests. Students are not good at just surfing the net for information. They quickly become derailed. Give them specific website URL addresses to view. Students can also spend too much time completing interest and aptitude inventories. Encourage students to do some of the research at home, if possible. Connections Use the biographies of successful people to show students that they may face many obstacles, but it they have a plan and continue to explore what they would like to do, they have a better chance of reaching their goal. Use the Internet search keywords: biographies of successful people. Some biographical resources include: http://www.google.com/Top/Reference/Biography/ http://www.internet-success-stories.com/index.htm http://www.infoplease.com/people.html http://www.biography.com/ http://www.s9.com/ Slide 2: What Will I Learn? No instructor notes for this slide Slide 3: Why Learn It? Many students haven’t really thought much about their lives after graduation nor have a realistic picture of what options they have. Relate your own feelings or experiences. When did you decide to be a teacher? Was it your first career choice? Let students know that they may change their minds many times, but without any plan, they will have trouble setting any career course. Slide 4: Tell Me / Show Me – The next step in preparing your portfolio… Explain how to locate Internet resources or library materials for the four items listed in Step 3: Your Personal Interests/Aptitudes and Career Preferences. Use Internet and/or a career counselor or center to identify applicable inventories. Ask students to explain what they think an “inventory” is. Possible response: A personal inventory is an accounting of one’s interests, skills, preferences, and values. A personal inventory is used to “take a snapshot in time” to decide what you know, what you don’t know, and what direction you want to pursue in life. Personal inventories are used to make decisions.

Copyright © 2009, Oracle. All rights reserved.

Page 11: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 11 Database Programming with SQL

Encourage students to find local community colleges, colleges/universities, and trade-school sites online. Identify programs that these institutions offer. What requirements do these schools require for the programs that they may be interested in pursuing? Students should cite the Internet sources and summarize the results obtained. Students will complete the Career Explorations Worksheet to record information. Be specific in what you expect students to prepare for each item.

• Two inventories of personal interests and aptitudes with a summary of strengths and/or weaknesses

• Identification of three careers of interest or preference • List of educational requirements for three careers of interest • Evaluation of current personal skills and career of interest; justification of

aptitude for chosen careers When students have completed Step 3, add the Career Explorations Worksheet to the student portfolio. Slide 5: Tell Me / Show Me – Step 1: Your Academic Achievements No instructor notes for this slide Slide 6: Summary – In this lesson you have learned about: No instructor notes for this slide Slide 7: Summary – Practice Guide No instructor notes for this slide

Copyright © 2009, Oracle. All rights reserved.

Page 12: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 12 Database Programming with SQL

PRACTICE SOLUTIONS SECTION 1 LESSON 1 – Case and Character Manipulation Vocabulary Directions: Identify the vocabulary word for each definition below. DUAL Dummy table used to view results from functions

and calculations

Format The arrangement of data for storage or display.

INITCAP Converts alpha character values to uppercase for the first letter of each word, all other letters in lowercase.

Character functions Functions that accept character data as input and can return both character and numeric values.

TRIM Removes all specified characters from either the beginning or the ending of a string.

Expression A symbol that represents a quantity or a relationship between quantities

Single-row functions Functions that operate on single rows only and return one result per row

UPPER Converts alpha characters to upper case

Input Raw data entered into the computer

CONCAT Concatenates the first character value to the second character value; equivalent to concatenation operator (||).

Output Data that is processed into information

LOWER Converts alpha character values to lowercase.

LPAD Pads the left side of a character, resulting in a right-

justified value

Copyright © 2009, Oracle. All rights reserved.

Page 13: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 13 Database Programming with SQL

SUBSTR Returns specific characters from character value

starting at a specific character position and going specified character positions long

REPLACE Replaces a sequence of characters in a string with another set of characters.

INSTR Returns the numeric position of a named string.

LENGTH Returns the number of characters in the expression

RPAD Pads the right-hand side of a character, resulting in a left- justified value.

Try It / Solve It 1. Using the three separate words “Oracle,” “Internet,” and

“Academy,” use one command to produce the following output: The Best Class Oracle Internet Academy

Solution: SELECT CONCAT('Oracle ', 'Internet')||' Academy' AS "The Best Class" FROM DUAL;

2. Use the string “Oracle Internet Academy” to produce the following output:

The Net net

Solution: SELECT SUBSTR('Oracle Internet Academy', 13,3)AS "The Net" FROM DUAL;

3. What is the length of the string “Oracle Internet Academy”?

Solution: 23 SELECT LENGTH('Oracle Internet Academy')AS "Length" FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 14: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 14 Database Programming with SQL

4. What’s the position of “I” in “Oracle Internet Academy”?

Solution: 8 SELECT INSTR('Oracle Internet Academy', 'I')AS "Position" FROM DUAL;

5. Starting with the string “Oracle Internet Academy”, pad the string to create

****Oracle****Internet****Academy****

Solution: SELECT LPAD('Oracle',10,'*')||LPAD('Internet',12,'*')||RPAD(LPAD('Academy',11,'*'),15,'*')AS "OIA" FROM DUAL;

6. Starting with the string “Oracle Internet Academy”, pad the string to produce:

Oracle$$$Internet$$$Academy

Solution: SELECT RPAD('Oracle',9,'$')||RPAD('Internet',11,'$')||'Academy' AS "OIA" FROM DUAL;

7. Using the string ‘Oracle Internet Academy’, produce the output shown using the

REPLACE function.

The Best Class Oracle 2004-2005 Academy

Solution: SELECT REPLACE('Oracle Internet Academy', 'Internet','2004-2005') AS "The Best Class" FROM DUAL;

8. List the order date and the order total from the Global Fast Foods F_ORDERS table.

Name the order total as TOTAL, and fill in the empty spaces to the left of the order total with $.

Solution: SELECT order_date, LPAD(ORDER_TOTAL,10,'$')AS "TOTAL" FROM f_orders;

Copyright © 2009, Oracle. All rights reserved.

Page 15: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 15 Database Programming with SQL

9. Write a query that will output a column called “ADDRESS” which has the following information: ZOE TWEE 1009 OLIVER AVENUE BOSTON, MA 12889. Use the Global Fast Foods F_CUSTOMERS table. Solution: SELECT UPPER(first_name) ||' '|| UPPER(last_name) ||' '|| UPPER(address)||' ' ||UPPER(city) ||', '||UPPER(state)||' '||zip AS "ADDRESS" FROM f_customers WHERE id = 456;

10. Write a query to return the first character of the first name concatenated to the

last_name, the salary and the department id for employees working in department 20. Give the first expression an alias of Name. Use the EMPLOYEES table. Change the query to use a substitution variable instead of the hard coded value 20 for department id. Run the query for department 30 and 50 without changing the original where-clause in your statement.

Solution: SELECT SUBSTR(first_name, 1,1) || last_name AS "Name", salary, department_id FROM employees WHERE department_id = 20; SELECT SUBSTR(first_name, 1,1) || last_name AS "Name", salary, department_id FROM employees WHERE department_id = :dept_id;

11. Using a substitution variable for the department name, write a query listing

department id, department name and location id for departments located in the_department_of_your_choice. Use the DEPARTMENTS table. Note: All substitution variables in OAE are treated as character strings, so no quotes (‘ ‘) are needed.

Solution: SELECT department_id, department_name, location_id FROM departments WHERE department_name = :dept_name;

Copyright © 2009, Oracle. All rights reserved.

Page 16: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 16 Database Programming with SQL

12. Write a query that returns all the employee data depending on the month of their hire date. Use the EMPLOYEES table. The statement should return the month part of the hiredate which is then compared to an abbreviated month (JAN, FEB, MAR) passed into the query via a substitution variable.

Solution: SELECT * FROM employees WHERE SUBSTR(hire_date, 4, 3) = :entered_month;

Copyright © 2009, Oracle. All rights reserved.

Page 17: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 17 Database Programming with SQL

SECTION 1 LESSON 2 – Number Functions Vocabulary Directions: Identify the vocabulary word for each definition below. TRUNC Used to terminate the column, expression, or value

to a specified number of decimal places Number functions These functions accept numeric input and return

numeric values.

MOD Returns the remainder of a division.

ROUND Rounds the column, expression, or value to a set number of decimal places.

Try It / Solve It 1. Display Oracle database employee last_name and salary for employee_ids between

100 and 102. Include a third column that divides each salary by 1.55 and rounds the result to two decimal places.

Solution: SELECT last_name, ROUND(salary/1.55,2) AS "Salary Calculation" FROM employees WHERE employee_id BETWEEN 100 AND 102;

2. Display employee last_name and salary for those employees who work in department

80. Give each of them a raise of 5.33% and truncate the result to two decimal places. Solution: SELECT last_name, TRUNC(salary * 1.0533,2) AS "Salary with Raise" FROM employees WHERE department_id = 80; -Alternate answer - SELECT last_name, TRUNC(salary * .0533,2) AS "Raise Amount" FROM employees WHERE department_id = 80;

Copyright © 2009, Oracle. All rights reserved.

Page 18: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 18 Database Programming with SQL

3. Use a MOD number function to determine whether 38873 is an even number or an odd number.

Solution: Odd. This is an example of using MOD when dividing by 2 to determine whether a number is odd or even. SELECT MOD(38873,2) FROM DUAL;

4. Use the DUAL table to process the following numbers:

845.553 - round to one decimal place 30695.348 - round to two decimal places 30695.348 - round to -2 decimal places 2.3454 - truncate the 454 from the decimal place

Solution: SELECT round(845.553,1) FROM DUAL; 30695.348 - round to two decimal places SELECT round(30695.348,2) FROM DUAL; 30695.348 - round to -2 decimal places SELECT ROUND(30695.348,-2) FROM DUAL; 2.3454 - truncate the 454 from the decimal place SELECT TRUNC(2.3454,1) FROM DUAL;

5. Divide each employee’s salary by 3. Display only those employees’ last names and

salaries who earn a salary that is a multiple of 3.

Solution: SELECT last_name, salary FROM employees WHERE MOD(salary,3) = 0;

6. Divide 34 by 8. Show only the remainder of the division. Name the output as

EXAMPLE.

Solution: SELECT MOD(34,8) AS Example FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 19: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 19 Database Programming with SQL

7. How would you like your paycheck – rounded or truncated? What if your paycheck was calculated to be $565.784 for the week, but you noticed that it was issued for $565.78. The loss of .004 cent would probably make very little difference to you. However, what if this was done to a thousand people, a 100,000 people, or a million people! Would it make a difference then? How much difference?

Solution: .004 * 1 = .004; .004 *1000 = $4.00; .004*100,000 = $ 400.00; .004 * 1,000,000 = $4000.00

Copyright © 2009, Oracle. All rights reserved.

Page 20: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 20 Database Programming with SQL

SECTION 1 LESSON 3 – Date Functions Vocabulary Directions: Identify the vocabulary word for each definition below. SYSDATE These functions accept numeric input and return

numeric values.

ADD_MONTHS Add calendar months to date

LAST_DAY Last day of the month

NEXT_DAY Next day of the date specified

MONTHS_BETWEEN Number of months between due dates Try It / Solve It 1. For DJ on Demand, display the number of months between the event_date of the

Vigil wedding and today’s date. Round to the nearest month.

Solution: SELECT ROUND(MONTHS_BETWEEN (SYSDATE,event_date)) AS MONTHS FROM d_events where id=105;

2. Display the days between the start of last summer’s school vacation break and the day

school started this year. Assume 30.5 days per month. Name the output “Days.”

Solution: SELECT ROUND(MONTHS_BETWEEN ('05-SEP-04', '15-JUN-04')*30.5) AS DAYS FROM dual; (Answers will vary.)

3. Display the days between January 1 and December 31.

Solution: SELECT ROUND(MONTHS_BETWEEN ('31-DEC-04','01-JAN-04')*30.5) AS DAYS FROM dual;

Copyright © 2009, Oracle. All rights reserved.

Page 21: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 21 Database Programming with SQL

4. Using one statement, round today's date to the nearest month and nearest year and truncate it to the nearest month and nearest year. Use an alias for each column.

Solution: SELECT ROUND(SYSDATE,'MONTH') AS Month,ROUND(SYSDATE,'YEAR') AS YEAR,TRUNC(SYSDATE,'MONTH')AS Month, TRUNC(SYSDATE,'YEAR') AS Year FROM DUAL;

5. What is the last day of the month for June 2005? Use an alias for the output.

Solution: SELECT LAST_DAY('01-JUN-05') AS "LAST DAY" FROM DUAL;

6. Display the number of years between the Global Fast Foods employee Bob Miller’s

birthday and today. Round to the nearest year. Solution: SELECT last_name, ROUND(MONTHS_BETWEEN(SYSDATE, birthdate)/12) AS YEARS FROM f_staffs WHERE id = 9;

7. Your next appointment with the dentist is six months from today. On what day will

you go to the dentist? Name the output, “Appointment.”

Solution: SELECT ADD_MONTHS(SYSDATE,6) AS Appointment FROM DUAL;

8. The teacher said you have until the last day of this month to turn in your research

paper. What day will this be? Name the output, “Deadline.”

Solution: SELECT LAST_DAY(SYSDATE) AS Deadline FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 22: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 22 Database Programming with SQL

9. How many months between your birthday this year and January 1 next year?

Solution: SELECT ROUND(MONTHS_BETWEEN('01-JAN-05', '19-JUN-04')) FROM DUAL; (Answers will vary.)

10. What’s the date of the next Friday after your birthday this year? Name the output,

“First Friday.”

Solution: SELECT ROUND(NEXT_DAY('19-JUN-04','Friday'))AS "First Friday" FROM DUAL; (Answers will vary.)

11. Name a date function that will return a number.

Solution: MONTHS_BETWEEN

12. Name a date function that will return a date.

Solution: Any of ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, and TRUNC

13. Give one example of why it is important for businesses to be able to manipulate date

data?

Solution: Schedule payrolls and payments, track employee performance reviews and years of service, or keep track of orders and shipments. (Answers will vary.)

Extension Exercise 1. Using DUAL, write a statement that will convert 86.678 to 86.68.

Solution: SELECT ROUND(86.678,2) FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 23: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 23 Database Programming with SQL

2. Write a statement that will display the DJ on Demand CD titles for cd_numbers 90 and 91 in uppercase in a column headed “DJ on Demand Collections.”

Solution: SELECT UPPER(title) AS "DJ on Demand Collections" FROM d_cds WHERE cd_number IN (90,91);

3. Write a statement that will create computer usernames for the DJ on Demand

partners. The usernames will be the lowercase letters of the last name + the uppercase first letter in the first name. Title the column “User Passwords.” For example, Mary Smythers would be smythersM.

Solution: SELECT CONCAT(LOWER(last_name),UPPER(SUBSTR(first_name,1,1))) AS "User Passwords" FROM d_partners;

4. Write a statement that will convert “It’s a small world” to “HELLO WORLD.”

Solution: SELECT UPPER(CONCAT('hello ',(SUBSTR('Its a small world',13, 18)))) FROM DUAL;

5. Write a statement that will remove the “fiddle” from “fiddledeedee” and the “dum”

from “fiddledeedum.” Display the result “fiddledeedeedee” in a column with the heading “Nonsense.” Solution: SELECT SUBSTR('fiddledeedum',1,9)||SUBSTR('fiddledeedee',7, 12)AS "Nonsense" FROM DUAL;

6. Replace every “i” in Mississippi with “$.”

Solution: SELECT REPLACE('Mississippi','i','$') FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 24: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 24 Database Programming with SQL

7. Using DUAL, convert 5332.342 to 5300. Solution: SELECT ROUND(5332.342,-2) FROM DUAL;

8. Using DUAL, convert 3.14159 to 3.14.

Solution: SELECT ROUND(3.14159 ,2) FROM DUAL;

9. Using DUAL, convert 73.892 to 73.8.

Solution: SELECT TRUNC(73.892,1) FROM DUAL;

10. What is the next Friday six months from now? Label the column “Future.”

Solution: SELECT NEXT_DAY(ADD_MONTHS(SYSDATE,6),'Friday') AS "Future" FROM DUAL;

11. What is the date 10 years from now? Label the column “Future.”

Solution: SELECT ADD_MONTHS(SYSDATE,120) AS "Future" FROM DUAL;

12. Leap years occur every four years. Remember, 2004 was a leap year. Now create a

function that will show the date of the next leap year as 29-FEB-08. Label the column “Future.”

Solution: SELECT ADD_MONTHS(LAST_DAY('01-FEB-04'),48) AS "Future" FROM DUAL;

Copyright © 2009, Oracle. All rights reserved.

Page 25: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 25 Database Programming with SQL

13. Write a statement that will find any of the DJ on Demand CD themes that have an “ie” in their names.

Solution: SELECT description FROM d_themes WHERE description LIKE '%ie%';

14. Write a statement that will return only the DJ on Demand CDs with years greater than

2000 but less than 2003. Display both the title and year.

Solution: SELECT title, year FROM d_cds WHERE year > 2000 AND year < 2003;

15. Write a statement that will return the Oracle database employee’s employee ID and

their starting hire dates between January 1, 1997 and today. Display the result ordered from most recently hired to the oldest.

Solution: SELECT employee_id, start_date FROM job_history WHERE start_date BETWEEN '01-JAN-97'AND SYSDATE ORDER BY start_date DESC, employee_id;

Copyright © 2009, Oracle. All rights reserved.

Page 26: Oracle

Oracle Academy Database Programming with SQL

Instructor Resource Guide

Oracle Academy 26 Database Programming with SQL Copyright © 2009, Oracle. All rights reserved.

SECTION 1 LESSON 4 – Interest and Aptitudes and Career Exploration Try It / Solve It Career Explorations Worksheet 1. From the aptitude/interest inventory information you obtained, briefly describe what

you learned about yourself and your interests. I like to... I prefer to...

Solution: Individual Responses

2. Make a table like the one shown below. From your research on the Internet or from

other sources, list three careers that you have explored so far.

Information Source Name/URL

Job Title Basic Work Responsibilities

Education/Training Required

Solution: Individual Responses Answer each of the following. Use complete sentences. 3. Which career(s) do you have the most interest in?

Solution: Individual Responses 4. For each of the careers you are most interested in, describe in detail why you are

interested in it. What makes this job sound like something you’d like to do?

Solution: Individual Responses 5. Based on the educational/training requirements for the career(s) you are most

interested in, list the courses you’ve taken in high school or college that you feel will help you prepare for this career?

Solution: Individual Responses

6. What future steps will you take to gain more knowledge and experience about your

chosen career(s)?

Solution: Individual Responses