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.

Slides:



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

Multiple Table Queries
© 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.
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.
MULTIPLE-TABLE QUERIES
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
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
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
Concepts of Database Management, Fifth Edition
Chapter 3 Single-Table Queries
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 9 Joining Data from Multiple Tables
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
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.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
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.
1 Reports. 2 Objectives  Use concatenation in a query  Change column headings and formats  Add a title to a report  Group data in a report  Include.
Chapter 4 Multiple-Table Queries
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Chapter 12 Subqueries and Merge Statements
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
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.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
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.
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
A Guide to SQL, Seventh Edition
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Multiple Table Queries
References: Text Chapters 8 and 9
Database Systems: Design, Implementation, and Management Tenth Edition
Using the Set Operators
Chapter Name SQL: Data Manipulation
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Oracle Join Syntax.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Subquery.
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

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 Subqueries  Using aliases  Joining a table to itself  UNION, INTERSECT and MINUS  ALL and ANY operators

3 Querying Multiple Tables  To retrieve data from two or more tables Join the tables Formulate a query using the same commands as for single tables

4 Create a Join  Indicate in the SELECT clause all columns to display  List in the FROM clause all tables involved in the query  Give condition(s) in the WHERE clause to restrict the data to be retrieved to only those rows that have common values in matching columns

5 Qualify Columns  SLSREP_NUMBER is in both the SALES-REP table and the CUSTOMER table, which causes ambiguity among column names  Qualify columns by separating the table name and the column name with a period.

6 Examples  List the customer number, last name, and first name for every customer, together with the sales rep number, last name, and fist name for the sales rep who represents each customer  List the customer number, last name, and first name of every customer whose credit limit is $1,000 together with the sales rep number, last name, and first name of the sales rep who represents each customer

7 JOIN, IN, and EXISTS  Include the where condition to ensure that matching columns contain equal values Similar results are obtained using IN or EXISTS operator within a subquery

8 Nested Subquery  When a subquery is within another subquery, it is called a nested subquery  Find the order number and order date for every order that includes a part located in warehouse number 3  Rewrite above query using 2 table joins

9  Innermost subquery is evaluated first, producing a temporary table of part numbers located in warehouse 3  Intermediate subquery is evaluated, producing a second temporary table with a list of order numbers  Outer query is evaluated last, producing the desired list of order numbers and order dates using only those orders whose numbers are in the temporary table produced in step 2 Nested Subquery Evaluation

10 Using an Alias  Tables listed in the FROM clause can be given an alternative name  List the sales rep number, last name, and first name for every sales rep together with the customer number, last name, and first name for each customer the sales rep represents

11 Complex Joins  Joining a table to itself  Find every pair of customers who have the same first and last names  For each pair of tables joined, include a condition indicating how the columns are related  For every part on order, list the part number, number ordered, order number, order date, customer number, last name, and first name of the customer who placed the order, and the last name and first name of the sales rep who represents each customer

12 Set Operations  Union: a table containing every row that is in either the first table or the second table, or both (must be union-compatible: same number of columns and corresponding columns have identical data types and lengths)  Intersection (intersect): a table containing every row that is in both tables  Difference (minus): set of every row that is in the first table but not in the second table

13 Set Operations

14 Set Operations

15 UNION/INTERSECT/MINUS  List the customer number, last name, and first name for every customer who is either represented by sales rep number 12 or who currently has orders on file, or both  List the customer number, last name, and first name for every customer who is represented by sales rep number 12 and who currently has orders on file  List the customer number, last name, and first name for every customer who is either represented by sales rep number 12 or who does not have orders currently on file

16 ALL and ANY  ALL and ANY operators are used with subqueries to produce a single column of numbers ALL: condition is true only if it satisfies all values produced by the subquery ANY: condition is true if it satisfies any value (one or more) produced by the subquery

17 ALL and ANY  Find the customer number, last name, first name, current balance, and sales rep number for every customer whose balance is greater than the individual balances of every customer of sales rep 12  Find the customer number, last name, first name, current balance, and sales rep number of every customer whose balance is larger than the balances of at least one customer of sales rep number 12

18 Summary  To join tables together, indicate in the SELECT clause all columns to display, list in the FROM clause all tables to join, and then include in the WHERE clause any conditions requiring values in matching columns to be equal  When referring to matching columns in different tables, you must qualify the column names to avoid confusion You qualify column names using the following format: table name.column name  Use the IN operator or the EXISTS command with an appropriate subquery as an alternate way of performing a join

19 Summary  A subquery can contain another subquery. The innermost subquery is executed first  The name of a table in a FROM clause can be followed by an alias, which is an alternate name for the table The alias can be used in place of the table name throughout the SQL command  By using two different aliases for the same table in a single SQL command, you can join a table to itself

20 Summary  The UNION command creates a union of two tables; that is, the collection of rows that are in either or both tables The INTERSECT command creates the intersection of two tables; that is, the collection of rows that are in both tables The MINUS command creates the difference of two tables; that is, the collection of rows that are in the first table but not in the second table To perform any of these operation, the tables must be union- compatible

21 Summary  Two tables are union-compatible if they have the same number of columns, and if their corresponding columns have identical data types and lengths  If a subquery is preceded by the ALL command, the condition is true only if it is satisfied by all values produced by the subquery  If a subquery is preceded by the ANY command, the condition is true if it is satisfied by any value (one or more) produced by the subquery