Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.

Slides:



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

© 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.
Relational Database Operators
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.
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 Seventh Edition
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.
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.
Introduction to SQL J.-S. Chou Assistant Professor.
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.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
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.
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Modern Database Management Chapter 8: Advanced SQL.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
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.
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.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
3 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Relational Algebra Operators (continued) Difference –Yields all.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
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.
Chapter 8 1 Lecture Advanced SQL. Chapter 8 2 Processing Multiple Tables–Joins Join – a relational operation that causes two or more tables with.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
David M. Kroenke and David J
Chapter Name SQL: Data Manipulation
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
COP 4610L: Applications in the Enterprise Spring 2005
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
Subqueries Schedule: Timing Topic 25 minutes Lecture
Relational Database Operators
Presentation transcript:

Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL

Database Systems, 8 th Edition 2 Objectives 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 –How to use SQL functions to manipulate dates, strings, and other data –How to create and use updatable views –How to create and use triggers and stored procedures –How to create embedded SQL

Database Systems, 8 th Edition 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

Database Systems, 8 th Edition 4 UNION Combines rows from two or more queries without including duplicate rows –Example: SELECTCUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROMCUSTOMER UNION SELECTCUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROMCUSTOMER_2 Can be used to unite more than two queries

Database Systems, 8 th Edition 5 UNION ALL Produces a relation that retains duplicate rows –Example query: SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROMCUSTOMER UNION ALL SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROM CUSTOMER_2; Can be used to unite more than two queries

Database Systems, 8 th Edition 6 Intersect Combines rows from two queries, returning only the rows that appear in both sets Syntax: query INTERSECT query –Example query: SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROMCUSTOMER INTERSECT SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROM CUSTOMER_2

Database Systems, 8 th Edition 7

8 Minus (SQL Server does not support) Combines rows from two queries –Returns only the rows that appear in the first set but not in the second Syntax: query MINUS query –Example: SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROMCUSTOMER MINUS SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE FROM CUSTOMER_2

Database Systems, 8 th Edition 9 Syntax Alternatives IN and NOT IN subqueries can be used in place of INTERSECT Example: SELECTCUS_CODE FROM CUSTOMER WHERECUS_AREACODE = ‘615’ AND CUS_CODE IN (SELECT DISTINCT CUS_CODE FROM INVOICE);

Database Systems, 8 th Edition SQL Join Operators Join operation merges rows from two tables and returns the rows with one of the following: –Have common values in common columns Natural join –Meet a given join condition Equality or inequality –Have common values in common columns or have no matching values Outer join Inner join: traditional join, only return rows meeting criteria

Database Systems, 8 th Edition 11

Database Systems, 8 th Edition 12 Cross Join Performs relational product of two tables –Also called Cartesian product Syntax: –SELECT column-list FROM table1 CROSS JOIN table2 Perform a cross join that yields specified attributes

Database Systems, 8 th Edition 13 Natural Join(SQL Server does not support) Returns all rows with matching values in the matching columns –Eliminates duplicate columns Used when tables share one or more common attributes with common names Syntax: SELECT column-list FROM table1 NATURAL JOIN table2

Database Systems, 8 th Edition 14 Join USING Clause (SQL Server does not support USING) Returns only rows with matching values in the column indicated in the USING clause Syntax: SELECT column-list FROM table1 JOIN table2 USING (common-column) JOIN USING operand does not require table qualifiers –Oracle returns error if table name specified

Database Systems, 8 th Edition 15 JOIN ON Clause Used when tables have no common attributes Returns only rows that meet the join condition –Typically includes equality comparison expression of two columns Syntax: SELECT column-list FROM table1 JOIN table2 ON join-condition

Database Systems, 8 th Edition 16 Outer Joins Returns rows matching the join condition Also returns rows with unmatched attribute values for tables to be joined Three types –Left –Right –Full Left and right designate order in which tables are processed

Database Systems, 8 th Edition 17 Outer Joins (continued) Left outer join –Returns rows matching the join condition –Returns rows in left side table with unmatched values –Syntax: SELECT column-list FROM table1 LEFT [OUTER] JOIN table2 ON join-condition Right outer join –Returns rows matching join condition –Returns rows in right side table with unmatched values

Database Systems, 8 th Edition 18

Database Systems, 8 th Edition 19 Outer Joins (continued) Full outer join –Returns rows matching join condition –Returns all rows with unmatched values in either side table –Syntax: SELECTcolumn-list FROM table1 FULL [OUTER] JOIN table2 ON join-condition

Database Systems, 8 th Edition 20

Database Systems, 8 th Edition Subqueries and Correlated Queries Often necessary to process data based on other processed data Subquery is a query inside a query, normally inside parentheses First query is the outer query –Inside query is the inner query Inner query executed first Output of inner query used as input for outer query Sometimes referred to as a nested query

Database Systems, 8 th Edition 22

Database Systems, 8 th Edition 23 WHERE Subqueries Most common type uses inner SELECT subquery on right side of WHERE comparison –Requires a subquery that returns only one single value Value generated by subquery must be of comparable data type Can be used in combination with joins Example: SELECT P_CODE, P_PRICE FROM PRODUCT WHERE P_PRICE >= (SELECT AVG(P_PRICE) FROM PRODUCT);

Database Systems, 8 th Edition 24 IN Subqueries Used when comparing a single attribute to a list of values

Database Systems, 8 th Edition 25

Database Systems, 8 th Edition 26

Database Systems, 8 th Edition 27 HAVING Subqueries HAVING clause restricts the output of a GROUP BY query –Applies conditional criterion to the grouped rows

Database Systems, 8 th Edition 28 Multirow Subquery Operators: ANY and ALL Allows comparison of single value with a list of values using inequality comparison “Greater than ALL” equivalent to “greater than the highest in list” “Less than ALL” equivalent to “less than lowest” Using equal to ANY operator equivalent to IN operator

Database Systems, 8 th Edition 29

Database Systems, 8 th Edition 30 FROM Subqueries Specifies the tables from which the data will be drawn Can use SELECT subquery in the FROM clause –View name can be used anywhere a table is expected

Database Systems, 8 th Edition 31 Attribute List Subqueries SELECT statement uses attribute list to indicate columns to project resulting set –Columns can be attributes of base tables –Result of aggregate function Attribute list can also include subquery expression: inline subquery –Must return one single value Cannot use an alias in the attribute list

Database Systems, 8 th Edition 32

Database Systems, 8 th Edition 33

Database Systems, 8 th Edition 34 Correlated Subqueries Subquery that executes once for each row in the outer query Correlated because inner query is related to the outer query –Inner query references column of outer subquery Can also be used with the EXISTS special operator

Database Systems, 8 th Edition 35

Database Systems, 8 th Edition 36