Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Chapter 1: The Database Environment
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
1 Chapter 40 - Physiology and Pathophysiology of Diuretic Action Copyright © 2013 Elsevier Inc. All rights reserved.
Relational Database and Data Modeling
Using the Set Operators
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Addition Facts
SQL: The Query Language Part 2
Haas MFE SAS Workshop Lecture 3:
Advanced SQL (part 1) CS263 Lecture 7.
© Abdou Illia MIS Spring 2014
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.
Chapter 4 Joining Multiple Tables
Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
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.
© Paradigm Publishing, Inc Access 2010 Level 1 Unit 1Creating Tables and Queries Chapter 2Creating Relationships between Tables.
Microsoft Office Illustrated Fundamentals Unit K: Working with Data.
Microsoft Access.
Displaying Data from Multiple Tables
15-1 Copyright © 2013 Pearson Education, Inc. publishing as Prentice Hall Chapter 15 Money and Banking.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 15 A Table with a View: Database Queries.
How Cells Obtain Energy from Food
1 © Prentice Hall, 2002 Chapter 4: The Enhanced E-R Model and Business Rules Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott,
© 2007 by Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
© 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 CHAPTER 7: ADVANCED SQL Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh,
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Modern Database Management
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
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.
Dr. Chen, Data Base Management Chapter 7: Advanced SQL Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane,
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7: Advanced SQL Modern Database Management 10 th Edition Jeffrey A. Hoffer, V. Ramesh,
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
1 Chapter 8: Advanced SQL Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Modern Database Management Chapter 8: Advanced SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer, Mary B.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 5 (Part a): Logical Database Design and the Relational Model Modern Database Management.
Chapter 4, Part A: Logical Database Design and the Relational Model
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Chapter 8 1 Lecture Advanced SQL. Chapter 8 2 Processing Multiple Tables–Joins Join – a relational operation that causes two or more tables with.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Chapter 4: Logical Database Design and the Relational Model
Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott,
David M. Kroenke and David J
CHAPTER 7: ADVANCED SQL.
Presentation transcript:

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 2 (Part I in bigger font) Define terms Write single and multiple table SQL queries Define and use three types of joins Write noncorrelated and correlated subqueries Establish referential integrity in SQL Understand triggers and stored procedures 2

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 3 Join – a relational operation that causes two or more tables with a common domain to be combined into a single table or view Equi-join– a join in which the joining condition is based on equality between values in the common columns; common columns appear redundantly in the result table Natural join– an equi-join in which one of the duplicate columns is eliminated in the result table The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1:M relationships

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 4 Outer join – a join in which rows that do not have matching values in common columns are nonetheless included in the result table (as opposed to inner join, in which rows must have matching values in order to appear in the result table) Union join – includes all columns from each table in the join, and an instance for each row of each table Illustration using abstract examples

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 5 Right Outer Join Left/right outer join: watch for the sequence of tables 5

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 6 (from Chapter 1, Figure 1-3) 6

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 7 Customer_T and Order_T are 1:M These tables are used in queries that follow Figure 7-1 Pine Valley Furniture Company Customer_T and Order_T tables with pointers from customers to their orders

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 8 Join may be specified in WHERE clause: FROM clause: In either case, each contains a column that shares a common domain with the other There should be one ON (or WHERE) specification for each pair of tables being joined

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall For each customer who placed an order, what is the customers name and order number? 9 Customer ID appears twice in the result For the purpose/level of our course, we can treat equi-join and inner join as roughly the same Those fields that appear in only one table does not need table indication

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID; SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T FROM CUSTOMER_T INNER JOIN ORDER_T ON CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID; SELECT CUSTOMER_T.CUSTOMER_ID, ORDER_T.CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T, ORDER_T FROM CUSTOMER_T INNER JOIN ORDER_T USING CUSTOMER_ID; 10

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 11 For each customer who placed an order, what is the customers name and order number? Join involves multiple tables in FROM clause ON clause performs the equality check for common columns of the two tables Note: from Fig. 7-1, you see that only 10 Customers have links with orders Only 10 rows will be returned from this INNER join

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 12 List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order. LEFT OUTER JOIN clause causes customer data to appear even if there is no corresponding order data Unlike INNER join, this will include customer rows with no matching order rows Why Customer data? Why not Order data? (--Fig 7-2)

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 13 Results Unlike INNER join, this will include customer rows with no matching order rows EVERY row in CUST table, PLUS Those matching rows in ORDER

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 14 CUSTOMER_T is the outer table – all records returned from it even there is no matching: those who did not place an order … Had we reversed the order the tables were listed, … Customer_T Order_T 14

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 15 Customer_T Order_T RIGHT OUTER JOIN Also see P. 295, first half Logically, A LEFT join B = B RIGHT join A: All rows in A, plus matching rows in B 15

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 16 Assemble all information necessary to create an invoice for order number 1006 Four tables involved in this join Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys Ref Slide #17 16

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 17 N tables, N-1 join conditions Join condi- tions from Slide #16

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 18 Figure 7-4 Results from a four-table join (edited for readability) From CUSTOMER_T table From ORDER_T table From PRODUCT_T table Derived from ORDER_T and PRODUCT_T table: OrderQuantity * ProductStandartdPrice 18

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 19 Self-joins are usually used on tables with unary relationships Note which one is which one! 19 The same table is used on both sides of the join (remember the ERD? – and think about tables); distinguished using table aliases

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 20 Subquery–placing an inner query (SELECT statement) inside an outer query Options: 1. In a condition of the WHERE clause 2. As a table of the FROM clause 3. Within the HAVING clause 4. (all the above because query results …) Subqueries can be: Noncorrelated–executed once for the entire outer query Correlated–executed once for each row returned by the outer query (in Chap 7-Part 2)

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 21 What are the name and address of the customer who placed order #1008? SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS FROM CUSTOMER_T WHERE CUSTOMER_T.CUSTOMER_ID = (SELECT CUSTOMER_ID FROM ORDER_T WHERE ORDER_ID = 1008); Note : the value for ORDER_ID does NOT appear in the query result; it is used as the selection criterion in the inner query Data from a subquery cannot be included in the final results

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 22

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 23

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 24 Show all customers who have placed an order SELECT CUSTOMER_NAME FROM CUSTOMER_T WHERE CUSTOMER_ID IN (SELECT DISTINCT CUSTOMER_ID FROM ORDER_T); Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery NOT/ANY/ALL may be used in front of IN; logical operators =, can be used

Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 25