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.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
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
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
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
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
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
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.
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.
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.
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.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
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.
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
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Multiple Table Queries
Oracle Join Syntax.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
David M. Kroenke and David J
Chapter Name SQL: Data Manipulation
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Oracle Join Syntax.
Oracle Join Syntax.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

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 Use a subquery within a subquery Use an alias A Guide to SQL, Seventh Edition

Objectives Join a table to itself 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) A Guide to SQL, Seventh Edition

Querying Multiple Tables When querying more than one table, the tables must be joined Join tables by finding columns with matching data Join tables by using a condition in the WHERE clause A Guide to SQL, Seventh Edition

Joining Two Tables In the SELECT clause, list all columns you want to display In the FROM clause, list all tables involved in the query In the WHERE clause, restrict to the rows that have common values in matching columns A Guide to SQL, Seventh Edition

Comparing JOIN, IN, and EXISTS Tables can be joined using IN or EXISTS clause Use IN operator with a subquery Use the EXISTS operator to retrieve data from more than one table A Guide to SQL, Seventh Edition

Correlated Subquery Subquery involves a table listed in the outer query In Figure 4.7 the ORDERS table, listed in the FROM clause of the outer query, is used in the subquery You need to qualify ORDER_NUM column in subquery as ORDERS.ORDER_NUM A Guide to SQL, Seventh Edition

Correlated Subquery For each row in the ORDERS table Subquery executed using the value of ORDERS.ORDER_NUM that appears in the row The inner query makes a list of rows in the ORDER_LINE table Where ORDER_LINE.ORDER_NUM matches this value and In which PART_NUM is equal to DR93 A Guide to SQL, Seventh Edition

Using a Subquery within a Subquery A nested subquery is a subquery within a subquery SQL evaluates the queries from the innermost query to the outermost It is possible that there is more than one approach to formulation of the queries Many DMBS have optimizers that analyze queries for efficiency A Guide to SQL, Seventh Edition

Using an Alias An alias is an alternate name for a table Used when tables are listed in the FROM clause Created by typing the name of the table, hitting a space, then typing the name of the alias Allows for simplicity A Guide to SQL, Seventh Edition

Joining a Table to Itself A self-join is when you are joining a table to itself A second use for using an alias Used when comparing records within one table Alias allows you to treat one table as two separate tables A Guide to SQL, Seventh Edition

Using a Self-Join on a Primary Key It is possible to create a self-join that involves the primary key of the table Just as in previous examples, you would list the table twice in the FROM clause with aliases A Guide to SQL, Seventh Edition

Joining Several Tables Condition shows how the columns are related for each pair of tables A Guide to SQL, Seventh Edition

Joining Several Tables Step-By-Step In the SELECT clause list all the columns to display Qualify the column name if needed In the FROM clause list all tables Include tables used in the WHERE clause, even if they are not in the SELECT clause A Guide to SQL, Seventh Edition

Joining Several Tables Step-By-Step Take one pair of related tables at a time Indicate in the WHERE clause the condition that relates the tables Join conditions with the AND operator Include any additional conditions in the WHERE clause Connect them with the AND operator A Guide to SQL, Seventh Edition

Set Operations Set operations are used for taking the union, intersection, and differences of two tables The union of two tables is a table containing every row that is in either the first table, the second table, or both tables A Guide to SQL, Seventh Edition

Set Operations 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 tables but are not in the second table A Guide to SQL, Seventh Edition

Restrictions to Set Operations Requirements for tables to be union compatible Have the same number of columns AND Their corresponding columns have identical data types and lengths A Guide to SQL, Seventh Edition

Special Operations Inner Join A join that compares the tables in the FROM clause and lists on those rows that satisfy the condition in the WHERE clause Outer Join A join that lists all the rows from one of the tables in a join, regardless of matching A Guide to SQL, Seventh Edition

Outer Joins Left outer join: all rows from the table on the left (listed first in the query) will be included; matching rows only from the table on the right will be included Right outer join: all rows from the table on the right will be included; matching rows only from the table on the left will be included Full outer join: all rows from both tables will be included regardless of matches A Guide to SQL, Seventh Edition

Product The product (Cartesian Product) of two tables is the combination of all rows in the first table and all rows in the second table Omit the WHERE clause to form a product A Guide to SQL, Seventh Edition

Summary Join tables with various methods A subquery can contain another subquery An alias can be used to simplify the SQL command as well to create self join UNION, INTERSECT, MINUS commands are introduced To form a product of two tables, include both tables in the FROM clause and omit the WHERE clause A Guide to SQL, Seventh Edition