MULTIPLE-TABLE QUERIES

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Multiple Table Queries
© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Class of ‘55 Room, Van Pelt Library - October 21, 2011.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
ACCESS PART 2. Objectives Database Tables Table Parts Key Field Query and Reports Import from Excel Link to Excel.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Concepts of Database Management Sixth Edition
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
Concepts of Database Management, Fifth Edition
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 3 Single-Table Queries
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
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.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
© 2007 by Prentice Hall6-1 Introduction to Oracle 10g Chapter 6 Creating Multitable Queries and Views James Perry and Gerald Post.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
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.
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.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 2 Concepts of Database Management, 4 th Edition, Pratt & Adamski Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
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.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
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.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Multiple Table Queries
Database Systems: Design, Implementation, and Management Tenth Edition
The Relational Algebra and Relational Calculus
Chapter Name SQL: Data Manipulation
JOINS (Joinining multiple tables)
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

MULTIPLE-TABLE QUERIES Chapter 5 in MySQL

Retrieving data from more than one table (joining tables) SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER, REP WHERE CUSTOMER.REP_NUM = REP.REP_NUM;

Setting restrictions on queries SELECT CUSTOMER_NUM, CUSTOMER_NAME, REP.REP_NUM, LAST_NAME, FIRST_NAME FROM CUSTOMER, REP WHERE CUSTOMER.REP_NUM = REP.REP_NUM AND CREDIT_LIMIT = 7500;

There is more than one way to write a query and get the same result SELECT DESCRIPTION FROM ORDER_LINE, PART WHERE ORDER_LINE.PART_NUM = PART.PART_NUM AND ORDER_NUM = ‘21610’; Or if you prefer you may use a different format:

Another example of the same query result SELECT DESCRIPTION FROM PART WHERE PART_NUM IN (SELECT PART_NUM FROM ORDER_LINE WHERE ORDER_NUM = ‘21610’); The subquery is considered first.

A Shortcut to writing out long table names in queries SELECT R.REP_NUM, LAST_NAME, FIRST_NAME, C.CUSTOMER_NUM, CUSTOMER_NAME FROM REP R, CUSTOMER C WHERE R.REP_NUM = C.REP_NUM;

Steps to writing a complex query List in the SELECT clause all of the columns/attributes you want to display. If the name of a column is in more than one table, qualify the name by stating which table it is from List in the FROM class all the tables involved in the query. These are usually the tables that contain the data in the SELECT clause, but be aware if you need to restrict the data by using data in ANOTHER table in a WHERE clause, you will need to include it in the FROM clause.

Steps to writing a complex query Take ONE pair of related tables at a time and indicate in the WHERE clause the condition that relates the tables. Join these conditions with the AND operator. If there are any conditions, include them in the WHERE clause and connect them to the other conditions with the AND operator.

Example: For each part on order, list the part number, number ordered, order number, order date, customer number and name, and the last name of the sales rep. SELECT PART_NUM, NUM_ORDERED, ORDER_LINE.ORDER_NUM, ORDER_DATE, CUSTOMER.CUSTOMER_NUM, CUSTOMER, LAST_NAME HERE WE STATE ORDER_LINE BEFORE ORDER_NUM BECAUSE ORDER_NUM IS IN BOTH THE ORDERS AND ORDER_LINE TABLE. WE WANT TO USE THE VALUE IN THE ORDER_LINE TABLE. THE SAME SITUATION APPLIES TO CUSTOMER_NUM. IT EXISTS IN THE CUSTOMER AND ORDERS TABLE.

FROM ORDER_LINE, ORDERS, CUSTOMER, REP Example: For each part on order, list the part number, number ordered, order number, order data, customer number and name, and the last name of the sales rep. FROM ORDER_LINE, ORDERS, CUSTOMER, REP HERE ARE ALL THE TABLES NEEDED TO EXTRACT THE DATA I WANT. WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM AND CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM AND REP.REP_NUM = CUSTOMER.REP_NUM HERE YOU ARE CONNECTING THE DATA IN THE TABLES

The finished query SELECT PART_NUM, NUM_ORDERED, ORDER_LINE.ORDER_NUM, ORDER_DATE, CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME, LAST_NAME FROM ORDER_LINE, ORDERS, CUSTOMER, REP WHERE ORDERS.ORDER_NUM = ORDER_LINE.ORDER_NUM AND CUSTOMER.CUSTOMER_NUM = ORDERS. CUSTOMER_NUM AND REP.REP_NUM = CUSTOMER.REP_NUM;

SET OPERATIONS -UNION You can combine two tables if they have the identical structure. This is usually done by creating selected data from a table based on a condition using two queries and then combining the results.

First, design the query for the first restriction: List the number and name of each customer that either is represented by sales rep 65 or that currently has orders on file, or both. First, design the query for the first restriction: SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE REP_NUM = ‘65’ Secondly, design the query for the second restriction using the same structure SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER, ORDERS WHERE CUSTOMER_NUM = ORDERS.CUSTOMER_NUM

Now put them together with the UNION operator SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER WHERE REP_NUM = ‘65 UNION SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER, ORDERS WHERE CUSTOMER.CUSTOMER_NUM = ORDERS.CUSTOMER_NUM;

The UNION operator MySQL will delete duplicates when you use the UNION operator. Note: I do not use INTERSECT, MINUS, or ALL and ANY. They tend to be “shortcuts” that cause more confusion than they are worth! : )

SPECIAL OPERATIONS Inner Join – These are the queries you are already familiar with. They retrieve only the data that you request that satisfies any WHERE clause restrictions.

SPECIAL OPERATIONS Outer Join: These queries list all of the rows from one of the tables in the join regardless of whether they match the restriction criteria in the WHERE clause. A LEFT outer join includes all the rows from the table on the left (always the first table) of the tables listed. In a RIGHT outer join, all rows from the table on the right (the last table listed) are listed. In a FULL outer join, all rows from both tables are included regardless of whether they match rows from another table. I have NEVER used such a query!!!

SPECIAL OPERATIONS Product – A combination of all rows in the first table and second table. You only select TWO tables. List the attributes you wish to retrieve and the tables that contain them. Leave off the WHERE clause. BEWARE: You can mistakenly make a product by accidently omitting the WHERE clause! SELECT CUSTOMER.CUSTOMER_NUM, CUSTOMER_NAME, ORDER_NUM, ORDER_DATE FROM CUSTOMER, ORDERS; See result on page 155