Chapter 9 Joining Data from Multiple Tables

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
MULTIPLE-TABLE QUERIES
Displaying Data from Multiple Tables
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
Dr. Chen, Oracle Database System (Oracle) 1 Database Development DDL DML DCL JL_D.B. ORACLE (SQL Components) (Retrieve Data and Produce Information from.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
ITBIS373 Database Development
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Using Special Operators (LIKE and IN)
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4 Multiple-Table Queries
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 CONACT UC:  Magnific training   
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL – join.
Oracle Join Syntax.
Displaying Data from Multiple Tables
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
David M. Kroenke and David J
JOINS (Joinining multiple tables)
Oracle Join Syntax.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Contents Preface I Introduction Lesson Objectives I-2
Oracle Join Syntax.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

Chapter 9 Joining Data from Multiple Tables Oracle 11g: SQL Chapter 9 Joining Data from Multiple Tables

Objectives Identify a Cartesian join Create an equality join using the WHERE clause Create an equality join using the JOIN keyword Create a non-equality join using the WHERE clause Create a non-equality join using the JOIN…ON approach Oracle 11g: SQL 2

Objectives (continued) Create a self-join using the WHERE clause Create a self-join using the JOIN keyword Distinguish an inner join from an outer join Create an outer join using the WHERE clause Create an outer join using the OUTER keyword Use set operators to combine the results of multiple queries Oracle 11g: SQL 3

Purpose of Joins Joins are used to link tables and reconstruct data in a relational database Joins can be created through: Conditions in a WHERE clause Use of JOIN keywords in FROM clause Oracle 11g: SQL 4

Cartesian Joins Created by omitting joining condition in the WHERE clause or through CROSS JOIN keywords in the FROM clause Results in every possible row combination (m * n) Oracle 11g: SQL 5

Cartesian Join Example: Omitted Condition Oracle 11g: SQL 6

Cartesian Join Example: CROSS JOIN Keywords Oracle 11g: SQL 7

Equality Joins Link rows through equivalent data that exists in both tables Created by: Creating equivalency condition in the WHERE clause Using NATURAL JOIN, JOIN…USING, or JOIN…ON keywords in the FROM clause Oracle 11g: SQL 8

Equality Joins: WHERE Clause Example Oracle 11g: SQL 9

Qualifying Column Names Columns in both tables must be qualified Oracle 11g: SQL 10

WHERE Clause Supports Join and Other Conditions Oracle 11g: SQL 11

Joining More Than Two Tables Joining four tables requires three join conditions Oracle 11g: SQL 12

Equality Joins: NATURAL JOIN Oracle 11g: SQL 13

No Qualifiers with a NATURAL JOIN Oracle 11g: SQL 14

Equality Joins: JOIN…USING Oracle 11g: SQL 15

Equality Joins: JOIN…ON Required if column names are different Oracle 11g: SQL 16

JOIN Keyword Overview Use JOIN…USING when tables have one or more columns in common Use JOIN…ON when same named columns are not involved or a condition is needed to specify a relationship other than equivalency (next section) Using the JOIN keyword frees the WHERE clause for exclusive use in restricting rows Oracle 11g: SQL 17

Non-Equality Joins In WHERE clause, use any comparison operator other than the equal sign In FROM clause, use JOIN…ON keywords with a non-equivalent condition Oracle 11g: SQL 18

Non-Equality Joins: WHERE Clause Example Oracle 11g: SQL 19

Non-Equality Joins: JOIN…ON Example Oracle 11g: SQL 20

Self-Joins Used to link a table to itself Requires the use of table aliases Requires the use of a column qualifier Oracle 11g: SQL 21

Customer Table Example Oracle 11g: SQL 22

Self-Joins: WHERE Clause Example Oracle 11g: SQL 23

Self-Joins: JOIN…ON Example Oracle 11g: SQL 24

Outer Joins Use outer joins to include rows that do not have a match in the other table In WHERE clause, include outer join operator (+) immediately after the column name of the table with missing rows to add NULL rows In FROM clause, use FULL, LEFT, or RIGHT with OUTER JOIN keywords Oracle 11g: SQL 25

Outer Joins: WHERE Clause Example Oracle 11g: SQL 26

Outer Joins: OUTER JOIN Keyword Example Oracle 11g: SQL 27

Outer Joins (continued) If multiple join conditions are used, the outer join condition may be required in all of the join conditions to retain nonmatching rows Oracle 11g: SQL 28

Set Operators Used to combine the results of two or more SELECT statements Oracle 11g: SQL 29

Set Operators: UNION Example Oracle 11g: SQL 30

Set Operators: INTERSECT Example Oracle 11g: SQL 31

Set Operators: MINUS Example Oracle 11g: SQL 32

Summary Data stored in multiple tables regarding a single entity can be linked together through the use of joins A Cartesian join between two tables returns every possible combination of rows from the tables; the resulting number of rows is always m * n An equality join is created when the data joining the records from two different tables are an exact match A non-equality join establishes a relationship based upon anything other than an equal condition Self-joins are used when a table must be joined to itself to retrieve needed data Oracle 11g: SQL 33

Summary (continued) Inner joins are categorized as being equality, non-equality, or self-joins An outer join is created when records need to be included in the results without having corresponding records in the join tables The record is matched with a NULL record so it will be included in the output Set operators such as UNION, UNION ALL, INTERSECT, and MINUS can be used to combine the results of multiple queries Oracle 11g: SQL 34