Instructor Guide Database Programming - Section 3 -...

46
Database Programming - Section 3 Instructor Guide

Transcript of Instructor Guide Database Programming - Section 3 -...

Page 1: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Database Programming - Section 3 Instructor Guide

Page 2: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.
Page 3: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page i

Table of Contents

Database Programming - Section 3................................................................................................................1 Lesson 1 - Destinations: What's in My Future? .............................................................................................1 What Will I Learn? ........................................................................................................................................3 Why Learn It?................................................................................................................................................4 Tell Me / Show Me........................................................................................................................................5 Try It / Solve It ..............................................................................................................................................6 Lesson 2 - Cartesian Product and the Join Operations...................................................................................7 What Will I Learn? ........................................................................................................................................8 Why Learn It?................................................................................................................................................9 Tell Me / Show Me........................................................................................................................................10 Try It / Solve It ..............................................................................................................................................18 Lesson 3 - Nonequijoins ................................................................................................................................22 What Will I Learn? ........................................................................................................................................24 Why Learn It?................................................................................................................................................25 Tell Me / Show Me........................................................................................................................................26 Try It / Solve It ..............................................................................................................................................27 Lesson 4 - Outer Joins ...................................................................................................................................29 What Will I Learn? ........................................................................................................................................30 Why Learn It?................................................................................................................................................31 Tell Me / Show Me........................................................................................................................................32 Try It / Solve It ..............................................................................................................................................33 Lesson 5 - Self-Joins......................................................................................................................................37 What Will I Learn? ........................................................................................................................................38 Why Learn It?................................................................................................................................................39 Tell Me / Show Me........................................................................................................................................40 Try It / Solve It ..............................................................................................................................................41

Page 4: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.
Page 5: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 1

Lesson 1 - Destinations: What’s in My Future?

Lesson 1 - Destinations: What’s in My Future?

Lesson Preparation

Review the student requirement for the Career Explorations Worksheet Internet access is required for this lesson. The following Internet sites are good places for students to begin looking: Search engine keywords: best colleges, trade schools United States Listings Worldwide listings www.scholarstuff.com Military careers

What to Watch For Students are not always realistic about what career options they have. Ask students to discuss their plans with a parent or guardian. Students often don't know how to find information about local community colleges, colleges, universities, and trade schools. You may want to have parents review and sign each student's planning documents.

Page 6: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 2

For those students who already have concrete plans for their thirteenth year, ask them to plan another option. What if something happened and they could not pursue what seems to be so certain right now? This will eliminate the "I already know what I'm going to do so I don't have to do this" response.

Connections Relate the process of developing goals, changing plans, and maybe having to restart a few times in life to the same process students experienced in data modeling. Was the first model the one you eventually adopted? Did all your plans as a group materialize? What changed along the way? Change is the only thing you can count on. It's best to have more than one play in your playbook -- more options instead of fewer.

Page 7: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 3

What Will I Learn?

What Will I Learn?

What Will I Learn?

Explain the purpose of a 10-year vision. Explain that long-term goals are things that happen over time. Planning ahead is one step to reaching what you want to accomplish. If students have an end or vision in mind, they are more likely to continue to focus on that goal. Earl Nightingale, a leader in self-improvement, once said, "We become what we think about." Right now, many students think a lot about their weekends and not as much about their futures. This lesson may help them start this lifelong process.

Page 8: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 4

Why Learn It?

Why Learn It?

Page 9: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 5

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Review your requirements for each item in the Step 4: Your Future Plans checklist. Students will struggle with 10-year goals, but that's the point. Students are not used to thinking that far into the future. This is a good mental challenge. Just try to keep students thinking realistically -- they may not all be millionaires.

Page 10: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 6

Try It / Solve It

Try It / Solve It

Page 11: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 7

Lesson 2 - Cartesian Product and the Join Operations

Lesson 2 - Cartesian Product and the Join Operations

Lesson Preparation

To introduce joins, discuss the processes involved in taking attendance each day. If your school uses an online attendance system, ask students how they think each teacher's data gets aggregated into the daily totals. If attendance is done manually, how does the office aggregate the data? Somewhere all the data is combined into one master list. Explain that SQL has commands that do the job of joining (or combining) data.

What to Watch For Joins are difficult for students. Create visuals to help students to see how primary-key and foreign-key columns are used to "bridge" the data from two tables. There are many ways to join tables. Students will differ in their approach. Concentrate on the correct result.

Connections Now that students are able to select data from more than one table, they should see the purpose behind a relational database and the power to organize and retrieve data easily. Relate this to drawing relationships between entities and identifying attributes and selecting unique identifiers. All the work in data modeling comes to life when the data tables are created.

Page 12: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 8

What Will I Learn?

What Will I Learn?

What Will I Learn?

It is helpful for students to have a copy of the table structure for the DJs on Demand database table layout in order to be able to draw connections between shared primary- and foreign-key columns.

Page 13: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 9

Why Learn It?

Why Learn It?

Why Learn It?

Explain the process of a join using the graphic.

Page 14: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 10

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Oracle proprietary joins are discussed in the lessons in Section 3. ANSI/ISO SQL 99 compliant joins are introduced in Section 4-2.

Page 15: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 11

Tell Me / Show Me

Tell Me / Show Me

Page 16: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 12

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask for student response to the questions: In the example below, what two tables are being joined? d_track_listings and the d_play_list_items What identical columns do both of these tables share? song_id If you wanted to join three tables, how many joins would it take? n-1 or 3 - 1 = 2. Share this formula with your students. Explain that the added syntax does complicate the query, but soon they will learn how to give the table name an alias in order to simplify the statement.

Page 17: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 13

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Students should recognize this join from a prior example. Ask students how rows were chosen in this query. Answer: If the columns selected shared that same data, they were selected; if not, they were omitted. In this query, song_id 50 in the d_track_listings table was not returned because the d_play_list_item table does not have this song_id.

Page 18: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 14

Tell Me / Show Me

Tell Me / Show Me

Page 19: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 15

Tell Me / Show Me

Tell Me / Show Me

Page 20: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 16

Tell Me / Show Me

Tell Me / Show Me

Page 21: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 17

Tell Me / Show Me

Tell Me / Show Me

Page 22: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 18

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT d.event_id, d.song_id, d.comments, t.cd_number, t.track FROM d_play_list_items d, d_track_listings t; 2. SELECT d.event_id, d.song_id, d.comments, t.cd_number, t.track FROM d_play_list_items d, d_track_listings t WHERE d.song_id = t.song_id; 3. SELECT s.title, s.artist, t.description FROM d_songs s, d_types t WHERE s.type_code = t.code;

Page 23: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 19

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. SELECT s.title, s.artist, t.description FROM d_songs s, d_types t WHERE s.type_code = t.code AND s.ID IN(47,48); 5. SELECT c.last_name, e.name, j.job_date FROM d_clients c , d_events e , d_job_assignments j WHERE c.client_number = e.client_number AND e.id = j.event_id; (Answers may vary.) 6. SELECT t.song_id, c.title FROM d_track_listings t, d_cds c WHERE t.cd_number = c.cd_number;

Page 24: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 20

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 7. F___a. A join is a type of query that gets data from more than one table based on columns with the same name. (Joined based on common values existing in corresponding columns) T___b. To join tables, there must be a common column in both tables and that column is usually a primary key in one of the tables. T___c. A Cartesian product occurs because the query does not specify a WHERE clause. F___d. Table aliases are required to create a join condition. (Table aliases give a column another name and help keep SQL code smaller. If two tables do not have columns with the same name, the join does not have to use the table.column syntax.) T___e. If a table alias is used for a table name in the FROM clause, it must be substituted for the table name throughout the SELECT statement. F___f. Table aliases must be only one character in length. (Table aliases can be up to 30 characters in length, but shorter is better.) T___g. A simple join or inner join is the same as an equijoin.

Page 25: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 21

8. Being able to combine data from different tables allows you to select and organize data in many different ways.

Page 26: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 22

Lesson 3 - Nonequijoins

Lesson 3 - Nonequijoins

Lesson Preparation

None. What to Watch For

The nonequijoin should not be difficult for students. Use other verbal examples to make the point of joining two tables using BETWEEN...AND. For example, a company pays its employees who earn an hourly wage in 15-minute increments. One table stores the hours and minutes recorded by a time clock and another table stores the pay range. If the minutes worked is between 0 and 15, the worker is paid for 15 minutes. If the minutes worked is between 16 and 30, the worker is paid for 30 minutes. If the minutes worked is between 31 and 45, the worker is paid for 45 minutes. If the minutes worked is between 46 and 60, the worker is paid for 1 hour.

Page 27: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 23

Connections Relate the BETWEEN...AND statement to its mathematical equivalent. Use the example: 50 is between 30 and 60 as 30 < = 50 < = 60 Ask students to state the WHERE condition in the following query using the mathematical equivalent: SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal The WHERE condition can be stated as WHERE salary is greater or equal to the lowest salary and less than or equal to the highest salary.

Page 28: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 24

What Will I Learn?

What Will I Learn?

What Will I Learn?

Read the objective for this lesson. Ask students if they can guess what a nonequijoin might be designed to do. Accept all answers.

Page 29: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 25

Why Learn It?

Why Learn It?

Why Learn It?

Read the objective for this lesson. Ask students if they can guess what a nonequijoin might be designed to do. Accept all answers.

Page 30: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 26

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask students to look at the D_EVENTS and D_PACKAGES tables. Ask "What package does the birthday bash event fall into?” Ask them how they arrived at their answers. Look for responses that indicate use of a nonequijoin and the BETWEEN...AND operator.

Page 31: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 27

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT e.name, p.code FROM d_events e, d_packages p WHERE e.cost BETWEEN p.low_range AND p.high_range; 2. SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal; 3. Join condition containing something other than an equality operator 4. WHERE a.ranking >=g.lowest_rank AND a.ranking < = g.highest_rank 5. Use an alias when two tables have columns with the same name, although you can always safely use an alias to qualify column names.

Page 32: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 28

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 6. To prevent a Cartesian product where all combinations of rows from both tables are returned 7. nonequijoin 8. equijoin

Page 33: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 29

Lesson 4 - Outer Joins

Lesson 4 - Outer Joins

Lesson Preparation

This lesson has a career-exploration activity. Verify Internet access to the following site: http://jobstar.org/tools/career/career.cfm

What to Watch For Joins take work and practice. Be sure students understand the use of the plus sign (+) and on which side it needs to be placed in the join condition. In the nontraditional-career activity, encourage students to find a realistic job.

Connections Using the information students found about a nontraditional career, relate it to the development of their personal portfolios, and the importance of having a plan. No matter what kind of career anyone chooses, everyone in any career attended high school at one time, just like you! The plans they made and the goals they accomplished after high school began in high school and continued throughout their lives.

Page 34: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 30

What Will I Learn?

What Will I Learn?

Page 35: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 31

Why Learn It?

Why Learn It?

Page 36: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 32

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with the following questions: When do you know whether to use an equijoin or a nonequijoin? In equijoins, the values in both tables must be equal. Frequently, this type of join involves primary- and foreign-key values. A nonequijoin is a join condition containing something other than the equality operator. Use a nonequijoin when the joining of two columns in different tables does not have matching values. Instead, the match occurs within a range of values. Use equijoin when you can match two columns in different tables using equal values (WHERE employees.department_id = departments.department_id). Ask students if we could get an accurate count of all the students that attend our school by just counting the students in class. Obviously, there are students that are home sick, students who are working in the office or attending classes somewhere else part of the day. There has to be another way to count all of the students whether they are in class now or not. Possible Answers: Use an outer join that return student names whether they are in class or not. It’s always a challenge for students (and instructors!) to remember which side the plus sign (+) goes on. A good way to remember this is: Think of the plus being used to fill in missing data in a column. Put the plus next to that column. Visualize plus signs filling in all the gaps.

Page 37: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 33

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT c.id, o.order_date,c.last_name,o.order_total FROM f_customers c, f_orders o WHERE c.id = o.cust_id(+); 2. SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id (+); 3. SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id(+) = d.department_id ;

Page 38: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 34

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. a. Cannot have (+) on both sides. How can you return data from two tables that is not in either one? One possible corrected solution is: WHERE e.department_id(+) = d.department_id; b. The department_id column exists in both the employees and departments tables. In the select statement, it is not clear which department_id is being referenced. A column alias for department_id is needed in the select statement. One possible corrected solution is: SELECT first_name, last_name, department_name, e.department_id FROM employees e, departments d WHERE e.department_id = d.department_id; c. There are two problems: 1. The operators are reversed (they can’t be greater than the highest and less than the lowest); 2. the clause is syntactically incorrect because the WHERE clause is missing e.position after the AND. The corrected statement should be: WHERE e.position <= p.highest AND e.position >= p.lowest; d. Missing aliases in the FROM line; column names must have underscores: location_id; cannot have space between alias and column name. One possible solution is:

Page 39: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 35

SELECT e.employee_id, e.last_name, d.location_id FROM employees e, departments d WHERE e.department_id = d.department_id; 5. SELECT t.song_id, c.title FROM d_track_listings t, d_cds c WHERE t.cd_number(+) = c.cd_number;

Page 40: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 36

Try It / Solve It

Try It / Solve It

Try It / Solve It

Career Exploration Activity: The purpose of this activity is to expand students' thinking about all the possibilities that exist to make a living and enjoy what they do. Help women focus on nontraditional careers. Internet search keywords for this topic include: "odd jobs," nontraditional careers," "nontraditional job titles" or http://jobstar.org/tools/career/career.cfm Ask students to prepare a short description to share with the class about a job they may be interested in doing.

Page 41: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 37

Lesson 5 - Self-Joins

Lesson 5 - Self-Joins

Lesson Preparation

None. What to Watch For

Before going on to Section 4 and the ANSI/ISO SQL 99 standard joins, make sure students understand the basics of joins and self-joins.

Connections Relate joins to data modeling and the direct relationship between how a data model is designed and how the database functions to store and retrieve information. Relate hierarchical data models to self-joins. Relate unique identifiers and table primary keys. A primary key placed in a related table becomes a foreign key in that table. These table relationship columns become the links that enable joins between tables. It is these relationships between tables that enable a database to have many hundreds of tables from which data can be selected.

Page 42: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 38

What Will I Learn?

What Will I Learn?

Page 43: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 39

Why Learn It?

Why Learn It?

Why Learn It?

Ask students: Who is the manager of employee 17? Who does Bob Jones manage?

Page 44: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 40

Tell Me / Show Me

Tell Me / Show Me

Page 45: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 41

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT e.last_name as "Employee", e.employee_id AS "Emp#", m.last_name AS "Manager", m.employee_id AS "Mgr#" FROM employees e, employees m WHERE e.manager_id = m.employee_id; 2. SELECT e.last_name as "Employee", e.employee_id AS "Emp#", m.last_name AS "Manager", m.employee_id AS "Mgr#" FROM employees e, employees m WHERE e.manager_id = m.employee_id(+) ORDER BY e.last_name; 3. SELECT e.last_name AS "Employee", e.hire_date AS "Emp Hired",m.last_name AS "Manager", m.hire_date AS "Mgr Hired" FROM employees e, employees m WHERE e.manager_id = m.employee_id AND e.hire_date < m.hire_date;

Page 46: Instructor Guide Database Programming - Section 3 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/2004_2005_curriculum/pdfs… · Database Programming - Section 3 Page 2 ... over time.

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 3 Page 42

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. FROM employees e, departments d 5. FROM locations l, departments d WHERE l.location_id(+) = d.location_id 6. FROM employees e, departments d WHERE e.department_id = d.department_id and last_name='Lorentz' 7. The side missing the data