Multiple Table Queries (Inner Joins, Equijoins) Week 3.

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.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should Be able to do the following: Write SELECT statements to accessWrite.
Displaying Data from Multiple Tables
Copyright  Oracle Corporation, All rights reserved. 3 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access.
After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and.
Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
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.
SQL Joins.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lab 4: Displaying Data from Multiple Tables CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
4 Displaying Data from Multiple Tables Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
SQL: Part 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
3d. Structured Query Language – JOIN Operations
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
CH3 Part2 Displaying Data from Multiple Tables (Join) Sub queries
Displaying Data from Multiple Tables Using Joins
Oracle Join Syntax.
(SQL) Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Multiple Table Queries (Inner Joins, Equijoins) Week 3

Objective –Write SELECT statements to display data from more than one table using equijoins (i.e. equality joins, also known as simple or inner joins)

EMPLOYEES DEPARTMENTS EMP_NOENAME.. DEPT_NO 101Cohen Huang Smith Miller DEPT_NO DEPT_NAMELOCN 10Accounting TORONTO 20ResearchOTTAWA 30SalesToronto 40Operations Markham 60 Service Toronto Related Tables Foreign key Primary key In queries data from several related tables may often have to be shown. These tables should be related through common data: the primary key of one table is often the foreign key of another table.

EMP_NO DEPT_NO LOCN Toronto Toronto OTTAWA TORONTO 14 rows selected. EMP_NO DEPT_NO LOCN Toronto Toronto OTTAWA TORONTO 14 rows selected. EMPLOYEES DEPARTMENTS EMP_NOENAME.. DEPT_NO 101Cohen Huang Smith Miller DEPT_NO DEPT_NAMELOCN 10Accounting TORONTO 20ResearchOTTAWA 30SalesToronto 40Operations Markham 60 Service Toronto Display Data from Related Tables 14 rows of output are to be displayed - one row for each employee

Equijoins Use an equijoin to display data from each row of one table with a related row in another table Joins are normally made between primary key columns and related foreign key columns Use = between related columns to join rows Join condition(s) specified in WHERE clause Good to prefix all column names with the table name for clarity and efficiency Must prefix the column name with the table name when the same column name appears in more than one of the tables

Equijoin Syntax SELECT table1.column1, table1.column3, table2.column4 FROM table1, table2 WHERE table1.column3 = table2.column1  Assuming that column1 is the primary key of table2 and column 3 in table 1 contains the same data and is a foreign key

Equijoin Example SELECT employees.emp_no, employees.dept_no, departments.locn FROM employees, departments WHERE employees.dept_no = departments.dept_no

What if you forget to use a Join Condition? If a join condition between tables is omitted in a multi-table query then every row from the first table will be joined with every row from the second table If table 1 has m rows and table 2 has n rows then result will have m*n rows Result is called a Cartesian Product Result is normally meaningless

Cartesian Product Example SELECT employees.emp_no, employees.dept_no, departments.locn FROM employees, departments ( this query is missing a join condition between dept_no of employees table and dept_no of departments table) WRONG…….WRONG………WRONG…….WRONG………

Cartesian Product Example Result EMP_NO DEPT_NO LOCN TORONTO TORONTO TORONTO Toronto 70 rows selected. EMP_NO DEPT_NO LOCN TORONTO TORONTO TORONTO Toronto 70 rows selected. EMPLOYEES (14 rows) DEPARTMENTS (5 rows) EMP_NOENAME.. DEPT_NO 101Cohen Huang Smith Miller DEPT_NO DEPT_NAMELOCN 10Accounting TORONTO 20ResearchOTTAWA 30SalesToronto 40Operations Markham 60 Service Toronto 70 rows of output are produced but there are only 14 employees, and each employee will be associated with all locations!

How to Avoid Cartesian Product –A Cartesian product is formed when: A join condition is omitted A join condition is invalid All rows in the first table are joined to all rows in the second table –To avoid a Cartesian product, always include a valid join condition in a WHERE clause.

NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. CUSTOMER CUSTID ORDID rows selected. CUSTID ORDID rows selected.ORD ORDID ITEMID rows selected. ORDID ITEMID rows selected.ITEM How many join conditions needed? How many rows of output could be produced? How many join conditions needed? How many rows of output could be produced? Joining More Than Two Tables

SELECT customer.name, ord.ordid, item.ordid, item.itemid FROM customer, ord, item WHERE customer.custid = ord.custid AND ord.ordid = item.ordid

Number of Equijoin Conditions Required  To join n tables together, you need a minimum of (n-1) join conditions.  Therefore, to join three tables, a minimum of two joins are required.  However if data is being related by a concatenated primary key then more than (n-1) joins will be required.

Table Aliases  Use table aliases instead of lengthy table names.  Table aliases are defined in the FROM clause: the table name is specified, followed by a space and then the table alias.  Table alias is only valid for the SELECT statement in which it is defined.  If a table alias is defined in the FROM clause then that table alias must be substituted for the table name throughout the SELECT statement.

Table Alias Example SELECT e.emp_no, e.dept_no, d.locn FROM employees e, departments d WHERE e.dept_no = d.dept_no