database programming - section 5 - oracle...

58
Database Programming - Section 5 Instructor Guide

Upload: doandieu

Post on 30-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

Database Programming - Section 5 Instructor Guide

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page i

Table of Contents

Database Programming - Section 5................................................................................................................1 Lesson 1 - Practice Exercises ........................................................................................................................1 What Will I Learn? ........................................................................................................................................2 Why Learn It?................................................................................................................................................3 Tell Me / Show Me........................................................................................................................................4 Try It / Solve It ..............................................................................................................................................5 Lesson 2 - Exam 1 Review ............................................................................................................................13 What Will I Learn? ........................................................................................................................................14 Why Learn It?................................................................................................................................................15 Tell Me / Show Me........................................................................................................................................16 Try It / Solve It ..............................................................................................................................................17 Lesson 3 - Exam 1 Information .....................................................................................................................18 What Will I Learn? ........................................................................................................................................19 Why Learn It?................................................................................................................................................20 Tell Me / Show Me........................................................................................................................................21 Try It / Solve It ..............................................................................................................................................22 Lesson 4 - Group Functions...........................................................................................................................23 What Will I Learn? ........................................................................................................................................24 Why Learn It?................................................................................................................................................25 Tell Me / Show Me........................................................................................................................................26 Try It / Solve It ..............................................................................................................................................35 Lesson 5 - COUNT, DISTINCT, and NVL...................................................................................................39 What Will I Learn? ........................................................................................................................................41 Why Learn It?................................................................................................................................................42 Tell Me / Show Me........................................................................................................................................43 Try It / Solve It ..............................................................................................................................................50

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 1

Lesson 1 - Practice Exercises

Lesson 1 - Practice Exercises

Lesson Preparation

This section's practice exercises cover the join syntax presented in the two previous sections. Students should have completed the study guides for these lessons as well as all the practice exercises. Students may want to do the Review of Joins exercises more than once if they have difficulty, depending on your classroom grading rules.

What to Watch For The amount of information students are expected to master can be overwhelming. Help students break the information into sections such as: basic SQL syntax, logical comparison operators, case and character manipulation, numbers and dates, functions, and joins.

Connections None.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 2

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 3

Why Learn It?

Why Learn It?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 4

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Complete and review the practice exercises. Assign a few problems at a time. Review the process used to identify the required information and the order of operations to obtain the result. Avoid simply giving the answers. Relate each problem to the concept it illustrates. For example, ask students to name two ways to join the two words "hello" and "world." Review the two responses: using || or CONCAT. Both accomplish the same result, but CONCAT can be used only to join two expressions." Assign the review of joins. Assessment: You may want to encourage students to review each example until they can get at least 7 out of 10 correct. Or you may prefer to allow only one attempt at the quiz. Allow 20 minutes for assessment/discussion afterward. Have students work in in pairs to review what they missed. Based on what types of questions they missed, have each pair of students choose one question they missed or did not understand to present to the group. Use this discussion to assess student understanding.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 5

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. The (+) is placed on the side of the join whose table has data missing in some rows. 2. The words "right" or "left" refer to the position of the tables in the join statement. A right outer join will return all rows that satisfy the join condition (with or without missing data) in the table listed on the right side of the join statement. A full outer join will return rows that satisfy the join condition (with or without missing data) from both tables. 3. WHERE prevents a cross-join or Cartesian product. Without WHERE, every row in one table is joined to every row in another table. 4. Data in both columns must be the same or compatible data types. 5. Any data in one table that does not have a corresponding matched value in the other table will not be returned.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 6

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 7

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 8

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 9

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 10

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 11

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 12

Try It / Solve It

Try It / Solve It

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 13

Lesson 2 - Exam 1 Review

Lesson 2 - Exam 1 Review

Lesson Preparation

Preview the Section 5 Exam Review. What to Watch For

Continue to help students see beyond just getting the answer. Can they tell you what concept is being tested and how they decided to solve the problem?

Connections None.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 14

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 15

Why Learn It?

Why Learn It?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 16

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 17

Try It / Solve It

Try It / Solve It

Try It / Solve It

Exam Review – Teacher Assign a few problems at a time. Review the process used to identify the required information and the order of operations to obtain the result. Avoid simply giving the answers. Relate each problem to the concept it illustrates.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 18

Lesson 3 - Exam 1 Information

Lesson 3 - Exam 1 Information

Lesson Preparation

Have students go to the Oracle iLearning outline and click SQL EXAM 1. This will be directly under Section 5, Lesson 3 (where we are right now). http://ilearning.oracle.com/ The time allowed for this exam is 45 minutes. Begin class by asking students if there are any last-minute questions. Answer these questions and have students begin the exam.

What to Watch For Allow enough class time for all students to finish the exam

Connections None.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 19

What Will I Learn?

What Will I Learn?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 20

Why Learn It?

Why Learn It?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 21

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 22

Try It / Solve It

Try It / Solve It

Try It / Solve It

If all students finish the exam with class time remaining, give an overview of the content to be learned in the next couple of weeks. Mention that up to now, all the SQL functions treated a table as one large group of information. In the coming lessons, they will learn SQL commands that will enable them to divide the tables into smaller groups and then use a group to return information specific to that group. Relate the functionality of being able to group information to being able to calculate the GPA of all the students in the 14-16 age group and in the 17-19 age group.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 23

Lesson 4 - Group Functions

Lesson 4 - Group Functions

Lesson Preparation

None. What to Watch For

Make sure students understand that a group function uses a group of rows to present one value. Reinforce the definition of NULL and that it is not equal to zero and will not be evaluated as a zero in a group function.

Connections Relate group functions to their use in business. For each group function studied in the lesson, ask students to apply the function to a business use. Answers may include: being able to find the average salary, average monthly sales, sum of the year's orders, the last name in the employee list, how much a machined part's dimensions vary from the average size of all such parts manufactured, how many standard deviations the classes' test scores are from the national test-score average. Students are learning group functions in their math class. Ask students for examples from their math book or class: maximum value, minimum value, average value, counting, adding, standard deviation, variance, and null.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 24

What Will I Learn?

What Will I Learn?

What Will I Learn?

There are many new syntax terms in this lesson. Read "What Will I Learn" to the students. Ask them to guess what they think the function of each term is in a query.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 25

Why Learn It?

Why Learn It?

Why Learn It?

Explain the term "aggregate." To be able to calculate the average age of all the students in the school would require finding the age of each student, adding all the ages together and then dividing by the number of students in the list. Explain that finding the SUM is an example of aggregating or grouping ages into one result.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 26

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Answer to the review question is: SELECT t.description AS "Music Type", s.title AS "Song Title" FROM d_types t, d_songs s WHERE t.code = s.type_code(+);

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 27

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain the group function graphic. Use the words "aggregating data" when describing the process of gathering together data to produce a single output. Briefly explain standard deviation and variance as a measure of the spread of data around the mean or average. Draw a normal curve and relate it to the height of people -- some are very short, some are very tall, but most people are within a few inches of "average." If a group of people had lots of very tall people and lots of very short people, the group would have a large variance in heights.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 28

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 29

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 30

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 31

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 32

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 33

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Check for understanding of the group function rules. Being able to apply the rules is critical in the upcoming lesson and practice exercises. For each of the slides above, explain why the rule is important. For example, because group functions ignore nulls, you may want to substitute zeros for nulls if you’re trying to find the average of something. It may be significant to know about null values. Or you may want to ignore the null values and no modification is necessary. If a column such as student_volunteer_hours has nulls in it, and you are finding the average number of hours students have volunteered in the community at your school, nulls are important! A null would mean that the student hasn’t completed any volunteer hours! This would affect your average. So you would need to replace the nulls with zeros before using the AVG group function.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 34

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review the use of MIN and MAX with all data types. Students may have difficulty thinking of a minimum date -- is minimum yesterday or is the minimum 10 years ago? Write the query below from the Global Fast Foods database on the board. Ask students what date will be returned if the database has the following three dates: 01-JUL-80, 19-MAR-79, 30-MAR-69. SELECT MIN(birthdate) FROM f_staffs

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 35

Try It / Solve It

Try It / Solve It

Try It / Solve It

Information written on walls of caves, on stone tablets, designed into structures like Stonehenge, smoke signals, telegraph dits/dahs, floppy disks, etc. See Extension activity later. Answers: 1. SELECT ROUND(AVG(cost), 2) FROM d_events; 2. SELECT manager_id, AVG(salary) FROM f_staffs WHERE manager_id = 19 GROUP BY manager_id; 3. SELECT SUM(salary) FROM f_staffs WHERE id IN(12, 9);

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 36

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. SELECT MIN(salary), MAX(hire_date), MIN(last_name), MAX(last_name) FROM employees WHERE department_id IN (50 , 60 ); 5. Answer: 1 Group functions return one result. 6. Group functions such as AVG ignore null values. For hourly employees, the salary field was null. These were not used to calculate the average, thus making the average salary very high.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 37

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 7. March 30, 1969 8. SELECT AVG(order_total) FROM f_orders WHERE order_date BETWEEN '01-JAN-02' AND '21-DEC-02'; 9. SELECT MAX(hire_date) FROM employees; 10. SUM(operating_cost) Extension: "Brains to Bytes" in an excellent article showing students how people have always had the need to store and transmit information (www.moah.org/). Ask students to brainstorm ways that man has devised methods to store and transmit information (see the "Brains to Bytes" article): cave paintings, stories woven in tapestries, monks transcribing manuscripts, knots in ropes to determine the depth of water, oriental rug designs, smoke signals, notches on the handle of a gun, graffiti, lantern light signals, dahs and

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 38

dits over telegraph lines, hieroglyphics, totem poles, artwork, music and songs, stories, sign language of the deaf, drum beats, dance, etc. Interview the school librarian. How did a student in 1960 use the library to find information? How are libraries different today and how have databases changed the way people store and find information? What will the library of the future look like? Will there still be rooms of bound books?

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 39

Lesson 5 - COUNT, DISTINCT, and NVL

Lesson 5 - COUNT, DISTINCT, and NVL

Lesson Preparation

None. What to Watch For

Check understanding by reviewing practice work. Connections

Choose two sample tables (use two that could be queried using a join) from any one of the three databases used in this course. Without referring to notes, ask pairs of students to do the following: Query the database using a SELECT * for each table. - Copy the table structure into MSWord. - Based on the table results set returned for each table, ask students to list all the SQL commands they know that could be used on the data. Students must name the query and give an example. Make it a game with prizes! Possible table combinations for this activity are: Oracle database: employees and departments; job_history and employees;departments and job_history

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 40

DJs on Demand: d_songs and d_types; d_clients and d_events; d_cds and d_track_listings Global Fast Foods: f_staffs and f_shift_assignments; f_food_items and f_promotional_menus; f_food_items and f_order_lines

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 41

What Will I Learn?

What Will I Learn?

What Will I Learn?

Explain that NVL is an abbreviation for null value. The three functions COUNT, DISTINCT, and NVL should be straightforward and easy for students to learn and use. Explain that "pseudocode" is code that is designed to structure a problem but will not actually run. The syntax will not be exact, but the meaning will be accurate.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 42

Why Learn It?

Why Learn It?

Why Learn It?

Before beginning this lesson, ask students to list all the ways the people in their town could be counted. List their responses on the board. Possible answers include: Go door to door asking each resident for the number of people living at that residence; use a mail-in form that asks for the number of people in their household; count all the names in the phone book; go to the tax records and count the number of taxpayers, etc. If there were millions of rows of names on a list, how easily could they be counted? How accurate would the final count be? SQL uses the COUNT function to accomplish this task.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 43

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Discuss the three examples in the student graphic.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 44

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Explain the examples shown. Ask students why the query shown returns null values. There are six values in the comment list, but the count returned only five. Point out that COUNT ignores the null value in the column. Discuss with students, "Why is it important, from a business perspective, to be able to handle null values when aggregating data?" Use the example of calculating the class average for an exam. If there are 30 students in the class, but only 28 were present who took the exam, how would the class average be affected if the total of all the scores were divided by 30 rather than 28? How does the null value test score for the two absent students affect the class average? Answer: In SQL, null values are not included in an AVG function. However, if you want the null values to be part of the class average, the NVL function could be used to replace each null value with a zero.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 45

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 46

Tell Me / Show Me

Tell Me / Show Me

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 47

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Discuss the difference in the output shown in the student graphic.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 48

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

The DISTINCT keyword was introduced in Section 1. In this lesson, DISTINCT is used in combination with group functions. Review the examples with students.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 49

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask students how the restaurant customer average would change if the owner did not use null values in the average. Answer: The average number of customers per day would be higher if nulls were eliminated.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 50

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT COUNT(*) FROM d_songs; 2. SELECT COUNT(DISTINCT loc_type) FROM d_venues; 3. SELECT COUNT(song_id), COUNT(DISTINCT cd_number) FROM d_track_listings; 4. SELECT COUNT(email) FROM d_clients;

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 51

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. SELECT COUNT(auth_expense_amt) FROM d_partners; 6. COUNT = 4 DISTINCT = 3 7. SELECT ROUND(AVG(NVL(auth_expense_amt, 100000)),2) FROM d_partners;

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 52

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 8. Valid statements are: b, d, g 9. b and c are True

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 53

Try It / Solve It

Try It / Solve It

Try It / Solve It

10. c is True.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 5 Page 54