Unit 3. Retrieving Data from Multiple Tables



Similar documents
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Oracle Database 10g: Introduction to SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle SQL. Course Summary. Duration. Objectives

Relational Division and SQL

Oracle 10g PL/SQL Training

Oracle Database: SQL and PL/SQL Fundamentals NEW

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

3. Relational Model and Relational Algebra

Oracle Database: SQL and PL/SQL Fundamentals

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

NATURAL LANGUAGE TO SQL CONVERSION SYSTEM

Programming with SQL

A basic create statement for a simple student table would look like the following.

Database CIS 340. lab#6. I.Arwa Najdi

Displaying Data from Multiple Tables

Information Systems (Informationssysteme)

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Information Systems SQL. Nikolaj Popov

ERserver. DB2 Universal Database for iseries SQL Programming with Host Languages. iseries. Version 5

Tune That SQL for Supercharged DB2 Performance! Craig S. Mullins, Corporate Technologist, NEON Enterprise Software, Inc.

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Using Temporary Tables to Improve Performance for SQL Data Services

Oracle Database 10g Express

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

Oracle Database: SQL and PL/SQL Fundamentals

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

Querying Microsoft SQL Server 2012

SQL SELECT Query: Intermediate

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Course 10774A: Querying Microsoft SQL Server 2012

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Intro to Embedded SQL Programming for ILE RPG Developers

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Effective Use of SQL in SAS Programming

Introduction to tuple calculus Tore Risch

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Natural Language Database Interface for the Community Based Monitoring System *

1. INTRODUCTION TO RDBMS

Querying Microsoft SQL Server

Oracle Database: Introduction to SQL

SQL Optimization & Access Paths: What s Old & New Part 1

Oracle Database 11g: SQL Tuning Workshop Release 2

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Oracle Database: Introduction to SQL

What's New in ADP Reporting?

Querying Microsoft SQL Server (20461) H8N61S

DB2 Developers Guide to Optimum SQL Performance

Introduction to Microsoft Jet SQL

Programming in Python V: Accessing a Database

Database SQL messages and codes

SQL Query Evaluation. Winter Lecture 23

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Scenario 2: Cognos SQL and Native SQL.

Course ID#: W 35 Hrs. Course Content

David M. Kroenke and David J. Auer Database Processing 11 th Edition Fundamentals, Design, and Implementation. Chapter Objectives

ERserver. iseries. DB2 Universal Database for iseries - Database Performance and Query Optimization

Introduction to Querying & Reporting with SQL Server

Analyze Database Optimization Techniques

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

IBM DB Introduction to SQL and database objects Hands-on Lab. Information Management Cloud Computing Center of Competence.

Querying Microsoft SQL Server 20461C; 5 days

Session: Archiving DB2 comes to the rescue (twice) Steve Thomas CA Technologies. Tuesday Nov 18th 10:00 Platform: z/os

Welcome to the topic on queries in SAP Business One.

TIM 50 - Business Information Systems

Oracle Database 11g: SQL Tuning Workshop

Querying Microsoft SQL Server 2012

Supported Platforms and Software Requirements Effective on 7 May HULFT-DataMagic for Windows Ver.2.2.0

Oracle Database: Introduction to SQL

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

6. SQL/XML. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. XML Databases 6. SQL/XML. Creating XML documents from a database

Generating XML from Relational Tables using ORACLE. by Selim Mimaroglu Supervisor: Betty O NeilO

Netezza Basics Class Outline

Saskatoon Business College Corporate Training Centre

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Oracle Database 11g SQL

Where? Originating Table Employees Departments

Unit 9: License Management

Copyright 2013 wolfssl Inc. All rights reserved. 2

DB2 V8 Performance Opportunities

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

XML Databases 6. SQL/XML

SQL Programming. Student Workbook

MOC QUERYING MICROSOFT SQL SERVER

Best Practices for DB2 on z/os Performance

Mini User's Guide for SQL*Plus T. J. Teorey

GET DATA FROM MULTIPLE TABLES QUESTIONS

Querying Microsoft SQL Server 2012

Accessing Your Database with JMP 10 JMP Discovery Conference 2012 Brian Corcoran SAS Institute

Performing Queries Using PROC SQL (1)

Instant SQL Programming

Transcription:

Unit 3. Retrieving Data from Multiple Tables What This Unit Is About How to retrieve columns from more than one table or view. What You Should Be Able to Do Retrieve data from more than one table or view. Specify JOIN predicates. Use correlation names in queries How You Will Check Your Progress Checkpoint questions Machine labs Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-1

3-1. Unit Objectives (CF123010) 3-2 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000

3.1 Accessing Data Stored in Multiple Tables Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-3

3-2. Retrieving Data from Multiple Tables (Principle) (CF123020) For each department number in table PROJECT, one row exists in table DEPARTMENT. By looking at the data within the two tables we can see how a row in one table is related to a row in the other table. The rows are related by the values in the DEPTNO columns. 3-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000

3-3. Retrieving Data from Multiple Tables (JOIN) (CF123030) You can use the SELECT statement to produce reports that contain information from two or more tables. This is commonly referred as a JOIN. To join two tables, specify the columns you want to be displayed in the SELECT clause, the table names in the FROM clause, and the join predicate in the WHERE clause. Because DEPTNO is a column of tables DEPARTMENT and PROJECT, the column name must be qualified. Furthermore, it is a good practice that all column names in a SELECT referencing more than one table be fully qualified to avoid potential error. When the join predicate is omitted each qualified row from the first table is combined with all qualifying rows in the second table. This is called a Cartesian Product and is usually an unwanted result. Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-5

3-4. JOIN Syntax 1 (CF123040) The tables EMPLOYEE and DEPARTMENT have common values for columns WORKDEPT and DEPTNO. WORKDEPT contains the department number to which the employee belongs. The join predicate tells DB2 UDB to combine the row for an employee with the row for the department to which the employee belongs. In most cases, when joining two tables we provide at least one JOIN condition. Fore three tables, we provide at least two JOIN conditions.. The general rule of thumb is: The number of tables minus one (n-1) is USUALLY the LEAST number of join predicates needed for the query, ensuring that there are no un-linked tables. Further conditions are allowed and may be added by means of AND or OR. 3-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000

3-5. JOIN Syntax 2 (JOIN Keyword) (CF123050) When the JOIN keyword is used in the FROM clause, the join predicates must be specified in an ON clause. Row conditions (local predicates) must be written in a WHERE clause which must follow the ON clause. Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-7

3-6. JOIN with Three Tables (CF123060) To find the name of the department that a project is assigned to, DB2 UDB can first read the PROJECT table to obtain the department number of the department. Next, read the corresponding row of the DEPARTMENT table to obtain the department name and the employee number of the responsible manager. Next, read the corresponding row of the EMPLOYEE table to obtain the manager's last name. The DB2 UDB optimizer determines the sequence of the above steps. 3-8 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000

3-7. JOIN with Three Tables (Cont) (CF123070) If multiple tables are specified, but no JOIN predicate and no search condition is specified, the intermediate result table consists of all possible combinations of the rows of the specified tables (the Cartesian product). The number of rows in the result is the product of the number of rows of all specified tables. Each row of the result is a row of the first table concatenated with a row of the second table, concatenated with a row of the third table, and so on. -- JP stands for Join Predicate -- LP stands for Local Predicate Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-9

3-8. Correlation Name (CF123080) Correlation names are defined in the FROM clause of any query. In this example, the correlation names are merely used as synonyms for the table names. Furthermore, a correlation name can be used to avoid column name ambiguity. 3-10 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000

3-9. Joining a Table with Itself (CF123090) To get the required data, the EMPLOYEE table must be accessed twice. Using the EMPLOYEE table, for each employee we read his/her department number. Then, from the department table we find his/her manager's employee number. Using the manager's employee number, we access the EMPLOYEE table again to retrieve the manager's date of birth. Then, the dates of birth are compared and for each employee that is older than his/her manager information is put into the result table. Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-11

3-10. Joining a Table with Itself (Cont) (CF123100) Table qualifiers (correlation names) are required because a table is referenced twice within the FROM clause of the query. 3-12 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000

Checkpoint Exercise Unit 3 Checkpoint T F 1. If you reference multiple tables in the FROM clause, you should use JOIN conditions to obtain the desired result. 2. Which of the following situations applies if you forget the JOIN conditions in a SELECT statement using multiple tables: a. You receive an error and the statement is not executed. b. The statement is executed and the result is the Cartesian product of the tables. 3. Why do we use correlation names in a SELECT? Copyright IBM Corp. 1999, 2000 Unit 3. Retrieving Data from Multiple Tables 3-13

3-11. Unit Summary (CF123110) 3-14 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000