Chapter 7: advanced SQL (Part 2 – Subqueries)

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.
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
© 2007 by Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Introduction to Structured Query Language (SQL)
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
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)
© 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 CHAPTER 7: ADVANCED SQL Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh,
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8 (Part b): Advanced SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer,
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Modern Database Management
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.
Dr. Chen, Data Base Management Chapter 7: Advanced SQL Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane,
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7: Advanced SQL Modern Database Management 10 th Edition Jeffrey A. Hoffer, V. Ramesh,
Concepts of Database Management Seventh Edition
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
1 Chapter 8: Advanced SQL Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer, Mary B.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
SQL Overview Structured Query Language
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
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.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Advanced SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Logical Database Design and the Rational Model
SQL Query Getting to the data ……..
Relational Database Design
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
02 | Advanced SELECT Statements
Database Systems: Design, Implementation, and Management Tenth Edition
Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott,
Lecture 2 The Relational Model
Using Subqueries to Solve Queries
David M. Kroenke and David J
CHAPTER 7: ADVANCED SQL.
CHAPTER 6: INTRODUCTION TO SQL
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Database Processing: David M. Kroenke’s Chapter Seven:
A Guide to SQL, Eighth Edition
Using Subqueries to Solve Queries
Contents Preface I Introduction Lesson Objectives I-2
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Subqueries Schedule: Timing Topic 25 minutes Lecture
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Chapter 7: advanced SQL (Part 2 – Subqueries) Check page numbers!! Chapter 7: advanced SQL (Part 2 – Subqueries) Modern Database Management 11th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi

Write noncorrelated and correlated subqueries Objectives Define terms Write single and multiple table SQL queries Note in SQL Handout 3 Define and use three types of joins – revisit OUTER JOIN Self-Join Write noncorrelated and correlated subqueries Establish referential integrity in SQL

Join with other operations After the first two tables are joined, the result set is joined to the next table, and so on WHERE/ WHERE/ GROUP HAVING ORDER BY JOIN1 JOIN2 BY T1 R1 R2 R3 R4 R5 T2 T3

Self-Join Example The same table is used on both sides of the join (remember the ERD? – and think about tables); distinguished using table aliases Note which one is which one! Self-joins are often used on tables with unary relationships

Fig 7-5 Example of a Self-Join

SET operation: minus Exclude the designated records Example: Find instructors who do not have the same last name as students SELECT instructor_name Name FROM instructor MINUS SELECT student_lname Name From student

Outer join revisit Want to list all customers and the orders placed, including customers who did not place orders. Which of the following is correct? cust LEFT JOIN [order] cust RIGHTT JOIN [order] [order] LEFT JOIN cust [order] RIGHTT JOIN cust

Outer join revisit LEFT JOIN RIGHT JOIN

Multiple table join revisit

Multiple table join revisit (Complex) SELECT FROM Product_T INNER JOIN ( (Customer_T INNER JOIN Order_T ON Customer_T.CustomerID = Order_T.CustomerID) INNER JOIN OrderLine_T ON Order_T.OrderID = OrderLine_T.OrderID) ON Product_T.ProductID = OrderLine_T.ProductID; NOT recommended

Multiple table join revisit – easiest: SELECT FROM Customer_T, Order_T, OrderLine_T, Product_T WHERE Customer_T.CustomerID = Order_T.CustomerID AND Order_T.OrderID =OrderLine.OrderID AND Product_T.ProductID = OrderLine_T.ProductID Recommended

Subqueries – 1 Subquery–placing an inner query (SELECT statement) inside an outer query As a “field” to be output, in the SELECT clause “Treat” the “no row value and set value can be together” problem As a “table” of the FROM clause In this case a view is created in the subquery and the view is then used in the main query In a condition of the WHERE clause When part of the condition is not a ready value (that needs to be found from a query In a condition of HAVING clause Similar to “3” except that it’s now for a group 3 and 4 logically the same

Subquery/inner query and main/outer query are “in two worlds”: Subqueries - 2 Subquery/inner query and main/outer query are “in two worlds”: Can use different tables The same table can have different table aliases Fields selected in subquery are never for display in the main/outer query  makes no sense to use alias for FIELDS inside the subquery “What happens here stays here” Re “2” above: what if we do need to relate A SPECIFIC value in main query w subquery? 

Subqueries - 3 Subqueries can be: Noncorrelated–executed once for the entire outer query The inner query does not rely on the outer query – as if they are two totally independent queries The outer query uses the resulted data passed from the inner query only ONCE Correlated–executed once for each row returned by the outer query The inner query needs data/parameter passed INTO it from the outer query The inner query often executed multiple times, whenever the outer query passes in a different data/parameter value

Subquery Example What are the name and address of the customer who placed order #1008? SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS FROM CUSTOMER_T WHERE CUSTOMER_T.CUSTOMER_ID = (SELECT CUSTOMER_ID FROM ORDER_T WHERE ORDER_ID = 1008); Note: the value for ORDER_ID does NOT appear in the query result; it is used as the selection criterion in the inner query Data from a WHERE-subquery cannot be included in the final results They are only used as ______

Subquery Example (cont) – Join tables vs subquery

Join vs. Subquery Some queries could be accomplished by either a join or a subquery [Examine the query on last slide] Join version Subquery version

Subquery Example (cont) – Join tables vs subquery

In this case, the subquery returns MANY values, Subquery – “IN” In this case, the subquery returns MANY values, not just one. Show all customers who have placed an order SELECT CUSTOMER_NAME FROM CUSTOMER_T WHERE CUSTOMER_ID IN (SELECT DISTINCT CUSTOMER_ID FROM ORDER_T); The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query NOT/ANY/ALL may be used in front of IN; logical operators =,<, > can be used

Fig 7-7 Subquery – “NOT IN”

Correlated vs. Non-correlated Subqueries Do not depend on data from the outer query Execute once for the entire outer query i.e. the subquery “first runs by itself”, “then pass the result to outer query” Correlated subqueries: Make use of data from the outer query Execute once for each row of the outer query Can use the EXISTS operator

About inner query and outer query In the non-correlated subqueries, the inner query only provides its output(s) as the Input(s) for the outer query; The field names/alias are unrelated with those in the outer query; The tables used can be (and often are) totally different from those in the outer query; In short: they, are, in, two, different, worlds!  “Las Vegas: ‘What happens here stays here’ ”

Figure 7-8a Processing a noncorrelated subquery A noncorrelated subquery processes completely before the outer query begins

Subquery – “EXISTS” EXISTS take a value of true if the subquery returns an intermediate results table that contains one or more rows (a nonempty set), false if no rows are returned (empty set) Query: What are the order IDs for all orders that have included furniture finished in natural ash? SELECT DISTINCT ORDER_ID FROM ORDER_LINE_T WHERE EXISTS (SELECT * FROM PRODUCT_T WHERE PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID AND PRODUCT_FINISH = ‘Natural Ash’); Note “select *”…

Correlated Subquery Example Show all orders that include furniture finished in natural ash SELECT DISTINCT OrderID FROM OrderLine_T WHERE EXISTS (SELECT * FROM Product_T WHERE ProductID = OrderLine_T.ProductID AND Productfinish = ‘Natural ash’); The EXISTS operator will return a TRUE value if the subquery resulted in a non-empty set, otherwise it returns a FALSE The subquery is testing for a value that comes from the outer query  A correlated subquery always refers to an attribute from a table referenced in the outer query  passing arguments from outter query

© 2013 Pearson Education, Inc. Publishing as Prentice Hall Figure 7-8b Processing a correlated subquery Subquery refers to outer-query data, so executes once for each row of outer query Note: Only the orders that involve products with Natural Ash will be included in the final results. © 2013 Pearson Education, Inc.  Publishing as Prentice Hall Chapter 7 26

(Summary) Places for a subquery Clause Use subQ Remark SELECT Yes SubQ generate column FROM SubQ generate a “dynaset” WHERE SubQ participate in condition GROUP BY No GROUP BY works w field only HAVING Same/similar with in WHERE ORDER BY No? ORDER BY works with field Subquery produces value(s); they are NOT fields

(Summary) SubQ in … SELECT: Break the “ban” of “no row values with set values” You want to list the individual restaurants’ sales as well as the average sales, for a comparison FROM: Provide a “temp table” Restaurants whose cities are not the same as the cities of their franchisees WHERE: Provide conditions that involve parts that are too complex for one single query Restaurants whose sales greater than AVG Accommodate the passing of arguments into the subquery >>> More complex; to demo this case

Subquery summary – 1: simple subq Evaluate once and result is compared w EACH row of the parent query. SELECT field_list FROM table_list (w possible joins) WHERE field {=,<,>,IN} (Subquery) With GROUP BY: GROUP BY field_list HAVING field {=,<,>,IN} (Subquery) Will the subquery results be displayed?

Subquery summary – 1: simple subq Typical scenarios: Rows/Groups with a field value > average of {whole table; a specific group} Groups having a field value >= AVG/COUNT… of{whole table; a specific group} Rows/groups whose certain field value is among a list of valid values (use IN) Note: in most cases (especially bullet #3), the values from the right-hand-side of the Boolean operators are NOT from the same table of the parent/main query – “reach different table thru SubQ”

Subq summary – 2: create subset for from SELECT field_list FROM (Subquery) WHERE … Example: display the names and home city of franchisees who currently owns restaurants in California Example: display the names and home city of franchisees who currently owns restaurants in California, whose credit rating is A or AA Will the subquery results be displayed?

Another Subquery Example (Derived Table) Show all products whose standard price is higher than the average price One column of the subquery is an aggregate function that has an alias name. That alias can then be referred to in the outer query Subquery forms the derived table [view] used in the FROM clause of the outer query The WHERE clause normally cannot include aggregate functions, but because the aggregate is performed in the subquery its result can be used in the outer query’s WHERE clause Differentiate: column name (header) vs variable

(Example) SubQ in FROM - dynaset If I want to see the average sales of the top-sale restaurants from each city, the “top-sale restaurants from each city” must be obtained in a subquery: SELECT AVG(AnnualSales) FROM (SELECT MAX(AnnualSales) FROM Restaurant GROUP BY City);

Subq summary – 3: create subset for select SELECT field_list, (Subquery) FROM table_list WHERE … Example: Will the subquery results be displayed?

Subq summary – 4: correlated subquery Pass argument from the main Q INTO subQ Special case: WHERE EXIST… i.e.: “*if* there’s a corresponding row” In addition to the book’s example,    Will the subquery results be displayed?

SubQ in WHERE – passing arguments INTO subquery If I want to see the restaurants whose sales are above average in its own city SELECT RestaurantID, AnnualSales, City FROM Restaurants R WHERE AnnualSales > (SELECT AVG(AnnualSales) FROM Restaurants WHERE City = R.City GROUP BY City); Passing argument in

Union Queries Combine the output (union of multiple queries) together into a single result table First query Combine Second query

strategy for writing subq Lay out the “big-picture” logic; Then figure out the “complex” part of logic that may need a subQ Write the main query Write the subquery Assemble the two Watch out: “border penetration” of Arguments Tables Query results

Figure 7-8 Combining queries using UNION Note: with UNION queries, the quantity and data types of the attributes in the SELECT clauses of both queries must be identical

Conditional Expressions Using Case Syntax This is available with newer versions of SQL, previously not part of the standard Figure 7-9

Tips for Developing Queries Be familiar with the data model (entities and relationships) Understand the desired results Know the attributes desired in result Identify the entities that contain desired attributes Review ERD Construct a WHERE equality for each link Fine tune with GROUP BY and HAVING clauses if needed Consider the effect on unusual data “Integrity” of logic – do NOT “translate”

Example of Query Using a View The view is called TSales, and consists of an aggregate query involving a join of three tables. TSales, returns the total sales of each product sold by each salesperson. The query below it uses TSales as if it were a table. Note that this query involves a correlated subquery. What this query does is show the biggest selling product for each salesperson.

Tips for Developing Queries Be familiar with the data model (entities and relationships) Understand the desired results Know the attributes desired in results Identify the entities that contain desired attributes Review ERD Construct a WHERE equality for each link Fine tune with GROUP BY and HAVING clauses if needed Consider the effect on unusual data It is important to test your query against a set of test data that includes unusual data, missing data, impossible values, etc.

Query Efficiency Considerations Instead of SELECT *, identify the specific attributes in the SELECT clause; this helps reduce network traffic of result set Limit the number of subqueries; try to make everything done in a single query if possible If data is to be used many times, make a separate query and store its results rather than performing the query repeatedly

Guidelines for Better Query Design Understand how indexes are used in query processing Keep optimizer statistics up-to-date Use compatible data types for fields and literals Write simple queries Break complex queries into multiple simple parts Don’t nest one query inside another query Don’t combine a query with itself (if possible avoid self-joins) Note that if you have indexed fields and want to take advantage of them, the WHERE clause should test for equality. For example, in the Product_T table, suppose Finish is indexed. If so, the query will run more efficiently if you are doing something like this: WHERE Finish = ‘Birch’ OR ‘Walnut’ instead of this: WHERE Finish NOT = ‘Walnut’.

Guidelines for Better Query Design (cont.) Create temporary tables for groups of queries Combine update operations Retrieve only the data you need Don’t have the DBMS sort without an index Learn! Consider the total query processing time for ad hoc queries

Ensuring Transaction Integrity Transaction = A discrete unit of work that must be completely processed or not processed at all May involve multiple updates If any update fails, then all other updates must be cancelled SQL commands for transactions BEGIN TRANSACTION/END TRANSACTION Marks boundaries of a transaction COMMIT Makes all updates permanent ROLLBACK Cancels updates since the last COMMIT

Our lecture may end with this slide Figure 7-12 An SQL Transaction sequence (in pseudocode) Our lecture may end with this slide

Data Dictionary Facilities System tables that store metadata Users usually can view some of these tables Users are restricted from updating them Some examples in Oracle 11g DBA_TABLES – descriptions of tables DBA_CONSTRAINTS – description of constraints DBA_USERS – information about the users of the system Examples in Microsoft SQL Server 2008 sys.columns – table and column definitions sys.indexes – table index information sys.foreign_key_columns – details about columns in foreign key constraints

SQL:2008 Enhancements/Extensions User-defined data types (UDT) Subclasses of standard types or an object type Analytical functions (for OLAP) CEILING, FLOOR, SQRT, RANK, DENSE_RANK, ROLLUP, CUBE, SAMPLE, WINDOW–improved numerical analysis capabilities New Data Types BIGINT, MULTISET (collection), XML CREATE TABLE LIKE–create a new table similar to an existing one MERGE

SQL:2008 Enhancements (cont) Programming extensions Persistent Stored Modules (SQL/PSM) Capability to create and drop code modules New statements: CASE, IF, LOOP, FOR, WHILE, etc. Makes SQL into a procedural language Oracle has propriety version called PL/SQL, and Microsoft SQL Server has Transact/SQL