Chapter 4 Multiple-Table Queries

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.
© 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.
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
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
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
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.
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
Introduction to Databases Chapter 7: Data Access and Manipulation.
Concepts of Database Management Seventh Edition
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
ITBIS373 Database Development
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.
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
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
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.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
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.
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.
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.
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,
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Multiple Table Queries
References: Text Chapters 8 and 9
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
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
JOINS (Joinining multiple tables)
Unit Relational Algebra 1
Presentation transcript:

Chapter 4 Multiple-Table Queries

Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery within a subquery Use an alias Join a table to itself

Objectives Perform set operations (union, intersection, and difference) Use the ALL and ANY operators in a query Perform special operations (inner join, outer join, and product)

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

Joining Two Tables To retrieve data from more than one table, join the tables together Tables can be joined by finding rows in the two tables that have identical values in matching columns

Joining Two Tables To join (relate) two or more tables, the SQL command is constructed as follows: SELECT clause: list all columns you want to display FROM clause: list all tables involved in the query WHERE clause: list the condition that restricts the data to be retrieved to only those rows from the two tables that match

Joining Two Tables Qualifying column names is especially important when joining tables Precede column name with the table name, followed by a period

Comparing of JOIN, IN, and EXISTS Join tables by including a condition in the WHERE clause to ensure that matching columns contain equal values Similar results can be obtained by using either the IN operator or the EXISTS operator with a subquery Either approach obtains the same results

Comparing of JOIN, IN, and EXISTS Problem: 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';

Using IN Instead of a Join to Query Two Tables

Using EXISTS Problem: Find the order number and order date for each order that contains part number DR93

Using a Subquery Within a Subquery When a subquery is within another subquery, it is called a nested subquery SQL evaluates queries from the innermost to the outermost: Innermost subquery is evaluated first Next (intermediate) subquery is evaluated Outer query is evaluated last

A Comprehensive Example The example given in this section involves several of the features already discussed Problem: List the customer number, order number, order date, and order total for each order with a total that exceeds $1,000. Rename the order total as ORDER_TOTAL

A Comprehensive Example

Using an Alias Tables listed in the FROM clause can be given an alternative name An alias is created by: Typing the name of the table Pressing the space bar Typing the name of the alias One reason for using an alias is simplicity

Joining a Table to Itself A second reason for using an alias is that it is needed when joining a table to itself, called a self-join Problem: For each pair of customers located in the same city, display the customer number, customer name, and city

Joining a Table to Itself

Joining Several Tables It is possible to join several tables For each pair of tables, you must include a condition indicating how the columns are related The procedure for joining more than two tables is essentially the same as the one for joining two tables

Joining Several Tables In such a query: The condition in the WHERE clause will be a compound condition All the desired columns would be listed in the SELECT clause Any columns that appear in more than one table would be qualified In the FROM clause, the tables that are involved in the query would be listed

Set Operations The union of two tables is a table containing every row that is in either the first table, the second table, or both tables The intersection (intersect) of two tables is a table containing all rows that are in both tables The difference (minus) of two tables is the set of all rows that are in the first table but that are not in the second table

Set Operations The two tables in the union must be “union compatible” Two tables are union compatible if: They have the same number of columns Their corresponding columns have identical data types and lengths

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

Special Operations The special operations within SQL are: Self-join Inner join Outer join Product

Inner Join A join that compares the tables in the FROM clause and lists only those rows that satisfy the condition in the WHERE clause is called an inner join The joins mentioned in this text so far have been inner joins

Outer Join Sometimes, there is a need to list all the rows from one of the tables in a join, regardless of whether they match any rows in the other table This type of join is called an outer join

Outer Join There are three types of outer joins: Left outer join - all rows from the table on the left will be included Right outer join - all rows from the table on the right will be included Full outer join - all rows from both tables will be included

Product The product is formally called the Cartesian Product The product of two tables is the combination of all rows in the first table and all rows in the second table

Summary To retrieve data from more than one table, you must join the tables together The IN or EXISTS operators can be used as an alternate way of performing a join A subquery can contain another subquery The UNION command creates a union of two tables (the collection of rows that are in either or both tables) The INTERSECT command creates the intersection of two tables (the collection of rows that are in both tables)

Summary The MINUS command creates the difference of two tables In an inner join, only matching rows from both tables are included In a left outer join, all rows from the table on the left will be included In a right outer join, all rows from the table on the right will be included The product of two tables is the combination of all rows in the first table and all rows in the second table

SQL Project Four Completed Good Luck H. Zamanzadeh