8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

© 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.
Relational Database Operators
MULTIPLE-TABLE QUERIES
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)
The Relational Database Model
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
The Relational Database Model
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
14 1 Chapter 14 Database Connectivity and Web Development Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Introduction to Structured Query Language (SQL)
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.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 3 Section 3.4 Relational Database Operators
Introduction to Databases Chapter 7: Data Access and Manipulation.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Lecture8:Data Manipulation in SQL Advanced SQL queries Ref. Chapter5 Lecture8 1.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
The Relational Database Model
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Advanced Relational Algebra & SQL (Part1 )
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
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.
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.
3 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Relational Algebra Operators (continued) Difference –Yields all.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
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.
IFS180 Intro. to Data Management Chapter 10 - Unions.
The Relational Database Model
Database Systems: Design, Implementation, and Management Tenth Edition
SQL Query Joins ASP.Net 2.0 – Visual Studio 2005
SQL – Subqueries.
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.
Joins CSC 240 (Blum).
Database Connectivity and Web Development
COP 4610L: Applications in the Enterprise Spring 2005
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Geo-Databases: lecture 4 Complex Queries in SQL
Manipulating Data Lesson 3.
Relational Database Operators
Presentation transcript:

8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

8 2 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel In this chapter, you will learn: About the relational set operators UNION, UNION ALL, INTERSECT, and MINUS How to use the advanced SQL JOIN operator syntax About the different types of subqueries and correlated queries

8 3 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel

8 4 Relational Set Operators UNION INTERSECT MINUS Work properly if relations are union- compatible –Names of relation attributes must be the same and their data types must be identical

8 5 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel

8 6 UNION Example -1: –SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER UNION SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2; –This example generates a combined listing of customers—one that excludes duplicate records Example -2 –SELECT column-list FROM T1 UNION SELECT column-list FROM T2 UNION SELECT column-list FROM T3;

8 7 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel UNION (continued)

8 8 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel UNION ALL UNION ALL query can be used to produce a relation that retains the duplicate rows UNION ALL statement can be used to unite more than just two queries Example query: –SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER UNION ALL SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2;

8 9 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel UNION ALL (continued)

8 10 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel INTERSECT The NTERSECT statement can be used to combine rows from two queries, returning only the rows that appear in both sets

8 11 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel MINUS The MINUS statement in SQL combines rows from two queries and returns only the rows that appear in the first set but not in the second

8 12 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Syntax Alternatives For example, the following query returns the customer codes for all customers who are located in area code 615 and who have made purchases. (If a customer has made a purchase, there must be an invoice record for that customer.) SELECT CUS_CODE FROM CUSTOMER WHERE CUS_AREACODE = '615' INTERSECT SELECT DISTINCT CUS_CODE FROM INVOICE;

8 13 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Syntax Alternatives (continued) For example, the following query returns the customer codes for all customers located in area code 615 minus the ones who have made purchases, leaving the customers in area code 615 who have not made purchases. SELECT CUS_CODE FROM CUSTOMER WHERE CUS_AREACODE = '615' MINUS SELECT DISTINCT CUS_CODE FROM INVOICE;

8 14 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Tue 18-6 SQL Join Operators

8 15 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Cross Join Syntax: –SELECT column-list FROM table1 CROSS JOIN table2 –Returns the Cartesian product of table1 and table2(old style). EXAMPLE : SELECT* FROM GameScores CROSS JOIN Departments PlayerNameDepartmentIdScores Jason13000 Irene11500 Jane21000 David22500 Paul32000 James32000 DepartmentI d DepartmentNam e 1IT 2Marketing 3HR PlayerNam e DepartmentI d Score s DepartmentI d DepartmentNam e Jason130001IT Irene115001IT Jane210001IT David225001IT Paul320001IT James320001IT Jason130002Marketing Irene115002Marketing Jane210002Marketing David225002Marketing Paul320002Marketing James330002Marketing Jason130003HR Irene115003HR Jane210003HR David225003HR Paul320003HR James330003HR

8 16 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Natural Join

8 17 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel JOIN USING Clause

8 18 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel JOIN ON Clause

8 19 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins Returns not only matching rows, but also rows with unmatched attribute values for one table or both tables to be joined Three types –Left –Right –Full

8 20 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins (continued)

8 21 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins (continued)

8 22 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Outer Joins (continued)

8 23 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Subqueries and Correlated Queries

8 24 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Correlated Subqueries (continued) Example (1): you want to know all customers who have placed an order lately Example (2): you want to know the vendor code and name of vendors for products having a quantity on hand that is less than double the minimum quantity