Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Multiple Table Queries
© Abdou Illia MIS Spring 2014
Populating Data Warehouse Structures Examining the Star Schema Dimension Tables Dimension Table Fact Table Sales Star Schema.
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.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Join Queries CS 146. Introduction: Join Queries So far, our SELECT queries have retrieved data from a single table Usually queries combine data from multiple.
Chapter 4 Joining Multiple Tables
Module 4: Joining Data from Multiple Tables. Querying Multiple Tables by Using Joins Applying Joins for Typical Reporting Needs Combining and Limiting.
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.
ZEIT2301 Design of Information Systems Multi-Table Queries in SQL School of Engineering and Information Technology Dr Kathryn Merrick.
4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Displaying Data from Multiple Tables
Module 6: Working with Subqueries. Overview Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
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.
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.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
Best Practices Transact-SQL Cont....  Combining Data from Multiple Tables Introduction to Joins Using Inner Joins Using Outer Joins Using Cross Joins.
Joins. Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Populating a Data Warehouse. Overview Process Overview Methods of Populating a Data Warehouse Tools for Populating a Data Warehouse Populating a Data.
© 2007 by Prentice Hall6-1 Introduction to Oracle 10g Chapter 6 Creating Multitable Queries and Views James Perry and Gerald Post.
Chapter 4 Multiple-Table Queries
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.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Module 7: Modifying Data. Overview Using Transactions Inserting Data Deleting Data Updating Data Performance Considerations.
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)
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
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.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Introduction to Structured Query Language (SQL) By Techandmate.comTechandmate.com Learn SQL Server With US.
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL – join.
3d. Structured Query Language – JOIN Operations
Advanced select statement Join Other DML commands
03 | Querying Multiple Tables with Joins
SQL – Column constraints
Querying Multiple Tables
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
JOINS (Joinining multiple tables)
JOINs JOINs can be used to combine tables A CROSS JOIN B
Introduction To Structured Query Language (SQL)
Introduction To Structured Query Language (SQL)
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

Module 5: Joining Multiple Tables

Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets

Using Aliases for Table Names Example 1 (without an alias name) Example 2 (with an alias name) USE joindb SELECT buyer_name, s.buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b.buyer_id = s.buyer_id GO USE joindb SELECT buyer_name, s.buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b.buyer_id = s.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO

Combining Data from Multiple Tables Introduction to Joins Using Inner Joins Using Outer Joins Using Cross Joins Joining More Than Two Tables Joining a Table to Itself

Introduction to Joins Selects Specific Columns from Multiple Tables JOIN keyword specifies that tables are joined and how to join them ON keyword specifies join condition Queries Two or More Tables to Produce a Result Set Use primary and foreign keys as join conditions Use columns common to specified tables to join tables

USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO salesbuyer_idbuyer_idprod_idprod_idqtyqty buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin OMelia buyer_idbuyer_id Resultbuyer_namebuyer_name Adam Barr Erin OMelia Eva Corets buyer_idbuyer_idqtyqty Erin OMelia Example 1 Using Inner Joins

USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers LEFT OUTER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers LEFT OUTER JOIN sales ON buyers.buyer_id = sales.buyer_id GO salesbuyer_idbuyer_idprod_idprod_idqtyqty buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin OMelia buyer_idbuyer_id Result buyer_namebuyer_name Adam Barr Erin OMelia Eva Corets buyer_idbuyer_idqtyqty Erin OMelia Sean Chai NULL Example 1 Using Outer Joins

USE joindb SELECT buyer_name, qty FROM buyers CROSS JOIN sales GO USE joindb SELECT buyer_name, qty FROM buyers CROSS JOIN sales GO Result buyer_namebuyer_name Adam Barr qtyqty Adam Barr 1003 Sean Chai 15 Sean Chai Sean Chai 11 Sean Chai 1003 Eva Corets sales buyer_idbuyer_idprod_idprod_idqtyqty buyers buyer_idbuyer_id buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin OMelia Example 1 Using Cross Joins

Joining More Than Two Tables SELECT buyer_name, prod_name, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id INNER JOIN produce ON sales.prod_id = produce.prod_id GO SELECT buyer_name, prod_name, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id INNER JOIN produce ON sales.prod_id = produce.prod_id GO produceprod_idprod_idprod_nameprod_name Apples Pears Oranges Bananas 5 5 Peaches buyersbuyer_idbuyer_id buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin OMelia salesbuyer_idbuyer_id prod_idprod_id qtyqty Result buyer_namebuyer_name Erin OMelia Adam Barr Erin OMelia Adam Barr Eva Corets prod_nameprod_name Apples Pears Oranges Peaches qtyqty Example 1

Joining a Table to Itself USE joindb SELECT a.buyer_id AS buyer1, a.prod_id,b.buyer_id AS buyer2 FROM sales AS a JOIN sales AS b ON a.prod_id = b.prod_id WHERE a.buyer_id > b.buyer_id GO USE joindb SELECT a.buyer_id AS buyer1, a.prod_id,b.buyer_id AS buyer2 FROM sales AS a JOIN sales AS b ON a.prod_id = b.prod_id WHERE a.buyer_id > b.buyer_id GO sales b buyer_idbuyer_idprod_idprod_idqtyqty sales a buyer_idbuyer_idprod_idprod_idqtyqty Result buyer1buyer1 4 4 prod_idprod_idbuyer2buyer Example 3

Combining Multiple Result Sets Use the UNION Operator to Create a Single Result Set from Multiple Queries Each Query Must Have: Similar data types Same number of columns Same column order in select list USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees UNION SELECT companyname, city, postalcode FROM customers GO USE northwind SELECT (firstname + ' ' + lastname) AS name,city, postalcode FROM employees UNION SELECT companyname, city, postalcode FROM customers GO

Recommended Practices Join Tables on Primary and Foreign Keys Reference All Columns of Composite Primary Key in the ON Clause When Composite Key Relates Tables Limit the Number of Tables in a Join

Lab A: Querying Multiple Tables

Review Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets