52
Database Programming - Section 4 Instructor Guide

Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Embed Size (px)

Citation preview

Page 1: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Database Programming - Section 4 Instructor Guide

Page 2: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability
Page 3: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page i

Table of Contents

Database Programming - Section 4................................................................................................................1 Lesson 1 - Putting It All Together .................................................................................................................1 What Will I Learn? ........................................................................................................................................3 Why Learn It?................................................................................................................................................4 Tell Me / Show Me........................................................................................................................................5 Try It / Solve It ..............................................................................................................................................6 Lesson 2 - Cross-Joins and Natural Joins ......................................................................................................8 What Will I Learn? ........................................................................................................................................10 Why Learn It?................................................................................................................................................11 Tell Me / Show Me........................................................................................................................................12 Try It / Solve It ..............................................................................................................................................14 Lesson 3 - Join Clauses .................................................................................................................................17 What Will I Learn? ........................................................................................................................................18 Why Learn It?................................................................................................................................................19 Tell Me / Show Me........................................................................................................................................20 Try It / Solve It ..............................................................................................................................................23 Lesson 4 - Inner versus Outer Joins...............................................................................................................27 What Will I Learn? ........................................................................................................................................28 Why Learn It?................................................................................................................................................29 Tell Me / Show Me........................................................................................................................................30 Try It / Solve It ..............................................................................................................................................33 Lesson 5 - Practice Exercises ........................................................................................................................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 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability
Page 5: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 1

Lesson 1 - Putting It All Together

Lesson 1 - Putting It All Together

Lesson Preparation

In this lesson, students will take a look into the future and the kinds of jobs that will be in demand. Use Internet resources to locate information about job predictions for the future (keyword search: "future careers" and "predicted future jobs"). The U.S. Department of Labor website provides excellent information about all aspects of the labor market and "tomorrow's jobs" in the United States. Additional sites include: http://www.bls.gov/news.release/nlsoy.t01.htm ftp://ftp.bls.gov/pub/news.release/nlsoy.txt United Kingdom: Department for Education and Skills, www.london.edu Careers in Europe: www.hobsons.com, http://www.exp.ie/advice/eurograduate.html, http://www.iht.com/IHT/SUP/052799/car01.html http://doctorjob.com/about/ Additional sites include:

Page 6: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 2

http://www.careerplanner.com/Career-Articles/Top_Jobs.htm http://www.readersdigest.ca/mag/2000/06/living_job.html

What to Watch For Explain that there is no crystal ball to predict what the future holds. Life-changing events such as the World Trade Center disaster, wars, stock-market speculation, environmental disasters such as earthquakes, volcanoes, hurricanes, etc. can have a radical impact on world economies. That aside, planning is still the best deterrent to becoming obsolete in the job market.

Connections Students could interview a veteran teacher to find out how much additional education/training he/she has obtained since graduating from college. Ask what courses are no longer being taught and what new courses have been added since they began teaching. Suggested answers: Obsolete training/courses: typing using a manual typewriter, cooking/sewing for girls only, printing-press trades New training/courses: computer programming classes, digital media, English as a Second Language (ESL)

Page 7: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 3

What Will I Learn?

What Will I Learn?

Page 8: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 4

Why Learn It?

Why Learn It?

Page 9: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 5

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Ask students to think of jobs that are now extinct or types of jobs they think will be obsolete in the next few years. Ask them to explain why they feel the job will no longer exist. Suggested answers: Film developer being replaced by digital-camera technology; movie theaters and video stores competing against the home entertainment centers; pay-phone repair technician versus inexpensive cell-phone technology; traditional CRT (cathode ray tube) computer monitors being replaced by the LCD (liquid crystal display) screens; floppy disks replaced with flash disks that hold hundreds of times more data; local small businesses competing against Internet online ordering; schools, colleges, and universities moving education from the campus classroom to the home computer.

Page 10: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 6

Try It / Solve It

Try It / Solve It

Try It / Solve It

Help students take a futuristic view of technology and the jobs predicted over the next 10 years. Relate the statistic that in 1990 the average number of career changes was three; by 1996, it had increased to seven. Discuss with students how the following factors have had an effect in changing the workplace and will continue to affect jobs in the future: - Population growth: increased demand for goods and services -- the greater number of people, the more goods and services needed - Shift from goods-producing to service business: manufacturing jobs decline and service-industry jobs increase - Immigration: brings workers competing for jobs; workers must have skills to compete with skilled workers from other countries - Aging population and longer life expectancies: create need for health and social services and influence government spending for health care and education - Global markets: businesses have more customers and suppliers - Internet: increases business opportunites without having a physical business location

Page 11: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 7

- Increased use of technology: demand for more sophisticated, skilled workers - Outsourcing to Third World countries: globalization has forced companies to cut positions and move them to places such as Mexico, China, or India, where labor is much cheaper - Downsizing and corporate restructuring: loss of jobs in order to be more efficient - Virtual companies/nonstandard workplace: workers can be hired part-time, working at home and on a temporary basis just to get the immediate job done

Page 12: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 8

Lesson 2 - Cross-Joins and Natural Joins

Lesson 2 - Cross-Joins and Natural Joins

Lesson Preparation

The joins in the next three lessons will use the ANSI/ISO SQL: 1999 syntax. Write the following list on the board to give students an overview of what will be covered in the next three lessons. The ANSI/ISO SQL: 1999 join terms are: cross-join, natural join, USING clause, outer join. Explain that ANSI stands for American National Standards Institute. Founded in 1918, ANSI is a private, non-profit organization (501(c)3) that administers and coordinates the U.S. voluntary standardization and conformity assessment system. The Institute's mission is to enhance both the global competitiveness of U.S. business and the U.S. quality of life by promoting and facilitating voluntary consensus standards and conformity assessment systems, and safeguarding their integrity. Reference: http://www.ansi.org/default.aspx Structured query language (SQL) is the information-processing industry-standard language of relational database management systems (RDMS). The language was originally conceived by IBM in the mid 1970s, gained prevalence in the early 1980s and became an industry standard in 1986, when it was adopted by ANSI. The purpose of the standard is to promote portability and interoperability of database application programs, to facilitate maintenance of database

Page 13: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 9

systems among heterogeneous data- processing environments, and to allow for the efficient exchange of programmers among different data-management projects. Discuss with students: "Why is it important for Oracle to comply with the current ANSI standard for SQL? Why is it important to comply with any standard? What standards do we rely on every day?" For example, a measuring cup in one store is the same size as a measuring cup in another, or the 185/60HR14 tire size in one store is the same tire in another store. Any time that students have extra time in class, have them work on the self-test software questions section "Displaying Data from Multiple Tables.“

What to Watch For Students will confuse which joins are Oracle proprietary joins and which are ANSI/ISO SQL: 1999 SQL. The important point is for students to be able to choose correct joins and apply them correctly.

Connections Continue the discussion about the importance of standards. Ask students what other standards they count on in everyday life? Possible answers include: time standard, weights and measures, international signs, Internet addresses.

Page 14: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 10

What Will I Learn?

What Will I Learn?

Page 15: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 11

Why Learn It?

Why Learn It?

Page 16: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 12

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Begin this lesson with a review question. Ask students to give an example of a query that would require a self-join. Possible answer: Hierarchical relationship data such as employees and managers appearing in the same table

Page 17: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 13

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

In the example cross-join, ask students to count the number of rows in both tables. The number of rows returned by the query will be the product of the number of rows in each table 2 * 6 = 12. Relate a cross-join to other examples: How many ways are there to arrange the following numbers: 1 2 3? Answer: 6 How many different combinations of outfits are possible with a red sweater and a blue t-shirt with blue jeans, hiking shorts, and a green hat? Answers will vary.

Page 18: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 14

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT last_name, department_name FROM employees CROSS JOIN departments; 2. SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations; 3. SELECT d.department_id, d.department_name, d.location_id, l.city FROM departments d, locations l WHERE d.location_id = l.location_id; 4. SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations WHERE department_id IN ( 20,50);

Page 19: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 15

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. SELECT type_code, description, title FROM d_songs, d_types WHERE d_songs.type_code = d_types.code AND type_code BETWEEN 70 and 80; 6. a. WHERE b. FROM

Page 20: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 16

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 7. alias or table name 8. FROM

Page 21: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 17

Lesson 3 - Join Clauses

Lesson 3 - Join Clauses

Lesson Preparation

None. What to Watch For

Joins and the different applications are challenging for students. Add practice whenever possible. Help students organize the Oracle proprietary joins and the ANSI SQL joins and the similarities and differences between them.

Connections Relate joins to other examples: For security reasons, usernames and passwords are stored in different tables. For a user to have computer access, the username and password must be checked. A join is a possible way to accomplish this task. Internet online shopping uses the "shopping cart" feature. The software is able to bring together information, such as customer name, payment method, and items ordered, from several tables to display a completed order on the screen.

Page 22: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 18

What Will I Learn?

What Will I Learn?

Page 23: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 19

Why Learn It?

Why Learn It?

Why Learn It?

Discuss the graphic. To join these two tables, there needs to be a column shared by each table. D_THEMES code column appears in D_EVENTS theme_code column.

Page 24: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 20

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Remind students of the restriction of using table names and aliases for the referenced columns in the USING clause. Ask students to rewrite the JOIN USING example as an equijoin. Note that qualifiers can be used. SELECT c.client_number, c.first_name, c.last_name, e.event_date FROM d_clients c, d_events e WHERE c.client_number = e.client_number;

Page 25: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 21

Tell Me / Show Me

Tell Me / Show Me

Page 26: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 22

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Discuss the graphic.

Page 27: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 23

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 1. SELECT l.city, d.department_name FROM locations l JOIN departments d USING (location_id) WHERE location_id = 1400; 2. SELECT song_id, cd_number, title, comments FROM d_play_list_items JOIN d_track_listings USING (song_id )JOIN d_cds USING (cd_number); 3. SELECT l.city, d.department_name, location_id, d.department_id FROM locations l JOIN departments d USING (location_id) WHERE city = 'Seattle' AND department_id IN (10, 20, 30); 4. SELECT country_name, region_id, region_name FROM regions JOIN countries USING (region_id) WHERE region_id = 2;

Page 28: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 24

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 5. SELECT e.first_name, e.last_name, e.hire_date, job_id, j.job_title, j.max_salary FROM employees e JOIN jobs j USING (job_id) WHERE max_salary > 12000; 6. SELECT job_title, first_name, last_name, email FROM employees e JOIN jobs j USING (job_id) WHERE job_id = 'ST_CLERK'; 7. SELECT e.employee_id, e.first_name, e.last_name, w.manager_id, w.first_name, w.last_name FROM employees e JOIN employees w ON (e.employee_id = w.manager_id);

Page 29: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 25

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 8. SELECT l.location_id, l.city,d.department_name FROM departments d JOIN locations l ON (d.location_id = l.location_id) WHERE l.country_id= 'CA'; 9. SELECT e.manager_id,d.department_id, d.department_name, e.first_name, e.last_name FROM employees e JOIN departments d ON (e.department_id =d.department_id) WHERE d.department_id IN (80, 90, 110, 190); 10. SELECT e.employee_id, e.last_name, d.department_id, d.department_name, e.hire_date FROM employees e JOIN departments d ON (e.department_id = d.department_id) WHERE hire_date = '07-JUN-94';

Page 30: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 26

The following websites have good tutorials on joins that can help students read other explanations for all the complicated join statements. http://www.databasejournal.com/, http://www.1keydata.com/sql/sql.html, http://otn.oracle.com/oramag/oracle/01-nov/o61sql.html

Page 31: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 27

Lesson 4 - Inner versus Outer Joins

Lesson 4 - Inner versus Outer Joins

Lesson Preparation

Review the Oracle proprietary outer join and compare it to the ANSI SQL left and right outer joins. Compare the location of the (+) sign versus the word left or right in the ANSI SQL joins. If you want all the data in the table on the right, use right outer join or use a (+) on the side of the join = sign that has the missing data. Draw a seesaw on the board, and add a (+) sign to the side of the board that is up in the air. It's missing something, so we need to add a (+) to it.

What to Watch For None.

Connections Summarize the purpose of joins and the basic premise of relational databases. Data in one place and the best place reduces redundancy. The tables are related through primary and foreign keys enabling users to query data from more than one table. SQL join syntax is used to access data from more than one table in a single query.

Page 32: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 28

What Will I Learn?

What Will I Learn?

Page 33: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 29

Why Learn It?

Why Learn It?

Page 34: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 30

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

Use the graphics to compare the output.

Page 35: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 31

Tell Me / Show Me

Tell Me / Show Me

Page 36: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 32

Tell Me / Show Me

Tell Me / Show Me

Tell Me / Show Me

SELECT c.first_name, c.last_name, o.order_number,o.order_date, o.order_total FROM f_customers c LEFT OUTER JOIN f_orders o ON (c.id = o.cust_id);

Ask students how to modify the join statement to include only orders from December 1, 2002, to December 1, 2004. SELECT c.first_name, c.last_name, o.order_number,o.order_date, o.order_total FROM f_customers c LEFT OUTER JOIN f_orders o ON (c.id = o.cust_id) WHERE o.order_date BETWEEN '01-JAN-02' AND '31-DEC-04';

Page 37: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 33

Try It / Solve It

Try It / Solve It

Tell Me / Show Me

Answers: 1. SELECT j.job_id, j.job_title, e.first_name, e.last_name, e.department_id FROM jobs j JOIN employees e ON( j.job_id = e.job_id) WHERE j.job_id = 'IT_PROG' 2. SELECT w.last_name as "Employee", w.employee_id as "Emp#", m.last_name as "Manager", m.employee_id as "Mgr#" FROM employees w JOIN employees m ON (w. manager_id = m.employee_id);

Page 38: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 34

3. SELECT w.last_name as "Employee", w.employee_id as "Emp#", m.last_name as "Manager", m.employee_id as "Mgr#" FROM employees w LEFT OUTER JOIN employees m ON (w. manager_id = m.employee_id) ORDER BY w.employee_id;

Page 39: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 35

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 4. SELECT c.first_name, c.last_name, e.event_date, e.description FROM d_clients c LEFT OUTER JOIN d_events e ON(c.client_number = e.client_number); 5. SELECT s.description, a.shift_assgn_date FROM f_shifts s LEFT OUTER JOIN f_shift_assignments a ON (s.code = a.code); Discuss with students, "Why is it important, from a business perspective, to be able to join data from multiple sources in so many different ways?" In the real world, you often count on outer joins for checking your data. A business rule may state, “You must enter all new employees and then assign them to a department.” What if the system allowed employees to be entered without departments, even though the business rule requires them to have departments? Well, first, the system would not be perfect. Someone didn’t design it properly with the business needs in mind. However, as the programmer, you may be asked to find those employees without departments and produce a report so that a manager can assign them to a department and fix the data!

Page 40: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 36

What if you were trying to find out if an advertisement was working? When you asked customers to fill out the form on your website that asked them, “How did you hear about our company?” they could provide a number of fixed responses. The query you might run against your database would say, show me all of the rows on the customer comments table and the “how did you hear about us” responses, but also show me all of the “how did you hear about us” responses that were not listed on the comments table.

Page 41: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 37

Lesson 5 - Practice Exercises

Lesson 5 - Practice Exercises

Lesson Preparation

None. What to Watch For

Students will have difficulty remembering the application and syntax for the different kinds of joins. Assist them in dissecting the problem to determine what is being asked and then to determine the appropriate syntax.

Connections None.

Page 42: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 38

What Will I Learn?

What Will I Learn?

Page 43: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 39

Why Learn It?

Why Learn It?

Page 44: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 40

Tell Me / Show Me

Tell Me / Show Me

Page 45: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 41

Try It / Solve It

Try It / Solve It

Try It / Solve It

As a review and to check for understanding, it may be best to ask students to do one problem at a time and then review it before going on to the next problem. Answers: 1. Cartesian product or cross join 2. SELF JOIN 3. OUTER JOIN 4. Deficient or is missing 5. Nonequijoin

Page 46: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 42

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 6. WHERE 7. table alias 8. FROM 9. will 10. 30

Page 47: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 43

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 11. + 12. LEFT OUTER JOIN 13. Self joins 14. 2 15. Oracle syntax that has been developed by the company 16. Join the tables with the USING clause.

Page 48: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 44

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 17. Equijoin and natural join 18. Cartesian product 19. What - SELECT Where - FROM How - WHERE 20. _T____cross-join _T__ equijoin _T____nonequijoin _____ natural join T____full outer join T____left outer join _____ USING clause

Page 49: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 45

21. a. Joins with the USING clause b. Natural join when the columns have the same name 22. Cross join or Cartesian product

Page 50: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 46

Try It / Solve It

Try It / Solve It

Try It / Solve It

Answers: 23. a. EQUIJOIN SELECT s.name, c.name FROM students s, course c, enrolled e WHERE (s.sid = e.sid) AND (e.cid = c.cid); b. FROM students NATURAL JOIN enrolled or FROM courses NATURAL JOIN enrolled; c. SELF JOIN SELECT s.name, m.name FROM students s, students m WHERE s.mid=m.sid;

Page 51: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 47

d. SELECT s.name, m.name FROM students s, students m WHERE s.mid=m.sid(+); SELECT s.name, m.name FROM students s LEFT OUTER JOIN students m ON (s.mid=m.sid);

Page 52: Instructor Guide Database Programming - Section 4 - Oracleacademy.oracle.com/pages/docs_pdfs_zip/.../instr_guide/Database... · Database Programming - Section 4 ... and interoperability

Copyright © Oracle, 2004. All rights reserved.

Database Programming - Section 4 Page 48