Multiple Table Queries

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
© Abdou Illia MIS Spring 2014
Concepts of Database Management Seventh Edition
© 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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
MULTIPLE-TABLE QUERIES
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
Concepts of Database Management, 4th Edition, Pratt & Adamski
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Concepts of Database Management, 4th Edition, Pratt & Adamski
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
Chapter 3 Single-Table Queries
A Guide to SQL, Eighth Edition Chapter Two Database Design Fundamentals.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
SQL – Part II Yong Choi School of Business CSU, Bakersfield.
Chapter 9 Joining Data from Multiple Tables
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
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
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining 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.
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.
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.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
IFS180 Intro. to Data Management Chapter 10 - Unions.
A Guide to SQL, Eighth Edition
Multiple Table Queries
References: Text Chapters 8 and 9
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
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.
Yong Choi School of Business CSU, Bakersfield
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Yong Choi School of Business CSU, Bakersfield
Yong Choi School of Business CSU, Bakersfield
Contents Preface I Introduction Lesson Objectives I-2
JOINS (Joinining multiple tables)
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Multiple Table Queries A Guide to SQL – Chapter 5

Instructional Objectives Use joins to retrieve data from one than one table Use IN and EXISTS operators to query multiple tables Use a subquery within a subquery ALL ANY

Joining tables Used when dealing with multiple tables (rather than writing lengthy subqueries) Finds rows in two tables that have identical values in matching columns Example: List the number and name of each customer along with the number, last name, and first name of the sales rep who represents the customer. Sort the records by customer number.

Solution SELECT CustomerNum, CustomerName, Rep.RepNum, LastName, FirstName FROM Customer, Rep WHERE Customer.RepNum = Rep.RepNum ORDER BY CustomerNum;

Another example Lists the number and name of each customer whose credit limit is $7,500 together with the number, last name, and first name of the sales rep who represents the customer

Solution 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;

Example For every part on order, list the order number, part number, part description, number of units ordered, quoted price, and unit price Solution: SELECT Order_Num, Order_Line.Part_Num, Description, Num_Ordered, Quoted_Price, Price FROM Order_Line, Part WHERE Order_Line.Part_Num = Part.Part_Num;

Example Find the description of each part included in order number 21610 Solution: SELECT Description FROM Order_Line, Part WHERE Order_Line.Part_Num = Part.Part_Num AND Order_Num = ‘21610’;

Another way… IN Operator SELECT Description FROM Part WHERE Part_Num IN (SELECT Part_Num FROM Order_Line WHERE Order_Num = ‘21610’);

Your Turn List all the number and dates of those orders that include a part located in warehouse 3

Solution

Alternate solution… subquery within a subquery Find the order number and order date for each order that includes a part located in warehouse 3 Solution: SELECT Order_Num, Order_Date FROM Orders WHERE Order_Num IN (SELECTOrder_Num FROM Order_Line WHERE Part_Num IN (SELECT Part_Num FROM Part WHERE Warehouse = ‘3’));

Exists Can also be used to get data from multiple tables Checks for existence of rows that satisfy some criterion Example: Find the order number and order date for each order that contains part number DR93

Solution:

Your turn For every order, list the order number, order date, customer number, and customer name. In addition, for each order line within the order, list the part number, description, number ordered, and quoted price. Sort the records by order number.

Solution

Comprehensive Example… Your Turn List the customer number, order number, and order total for each order with a total that exceeds $1,000. Rename the order total as ORDER_TOTAL

Solution… SELECT Customer_Num, Orders.Order_Num, SUM(Num_Ordered * Quoted_Price) AS ORDER_TOTAL FROM Orders, Order_Line WHERE Orders.Order_Num = Order_Line.Order_Num GROUP BY Orders.Order_Num, Customer_Num HAVING Sum (Num_Ordered * Quoted_Price) > 1000 Order By Orders.Order_Num;

Joining a table to itself (self-join) Have to use an alias (in FROM clause) to treat a table as two tables. Example: For each pair of customers located in the same city, display the customer number, customer name, and city

Solution (See figures 5.12 & 5.13 on p. 147-148)

Set Operations Union Intersect (intersection) Minus (difference) Of two tables is a temp table containing every row that is in either the first table, the second table or both Intersect (intersection) Of two tables is a temp table containing all rows that are in both tables Tables must be union compatible – same number of columns with identical data types and lengths Minus (difference) Of two tables is (a temp table) the set of all rows that are in the first table but not in the second table Access does not support Intersect or Minus

Union example 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 Solution:

Intersect example List the number and name of each customer that is represented by sales rep 65 and that currently has orders on file Solution: Since Access does not support Intersect, use subquery

Minus Example List the number and name of each customer that is represented by sales rep 65 and that does not currently have orders on file Solution: Since Access does not support Intersect, use subquery

ALL Find the customer number, name, current balance, and rep number of each customer whose balance is greater than the individual balances of each customer of sales rep 65. Solution:

ANY Find the customer number, name, current balance, and rep number of each customer whose balance is greater than the balance of at least one customer of sales rep 65. Solution:

Special Operations Inner join Outer join Join that compares the tables in the FROM clause and lists only those rows that satisfy the condition All the examples we have looked at so far have used inner joins Outer join Used to list all rows from one of the tables in a join, regardless of whether they match any rows in the other table Types of outer join Left outer join Right outer join Full outer join

Outer joins Left outer join Right outer join Full outer join All rows from the table on the left will be included regardless of whether they match rows from the table on the right Right outer join All rows from the table on the right will be included regardless of whether they match rows from the table on the left Full outer join All rows from both tables will be included regardless of whether they match rows from the other table

Outer join example Display the customer number, customer name, order number, and order date for all orders. Include all customers in the results. For customer that do not have orders, omit the order number and order date.

Outer join example solution

Product Combination of all rows in the first table and all rows in the second table Example: Form the product of the Customer and Orders table. Display the customer number, name, order number and order date Solution: