Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: advanced SQL (Part 2 – Subqueries)

Similar presentations


Presentation on theme: "Chapter 7: advanced SQL (Part 2 – Subqueries)"— Presentation transcript:

1 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

2 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

3 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 JOIN JOIN BY T1 R1 R2 R3 R4 R5 T2 T3

4 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

5 Fig 7-5 Example of a Self-Join

6 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

7 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

8 Outer join revisit LEFT JOIN RIGHT JOIN

9 Multiple table join revisit

10 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

11 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

12 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

13 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? 

14 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

15 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 ______

16 Subquery Example (cont) – Join tables vs subquery

17 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

18 Subquery Example (cont) – Join tables vs subquery

19 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

20 Fig 7-7 Subquery – “NOT IN”

21 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

22 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’ ”

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

24 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 *”…

25 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

26 © 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

27 (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

28 (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

29 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?

30 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”

31 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?

32 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

33 (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);

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

35 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?

36 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

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

38 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

39 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

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

41 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”

42 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.

43 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.

44 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

45 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’.

46 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

47 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

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

49 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

50 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

51 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


Download ppt "Chapter 7: advanced SQL (Part 2 – Subqueries)"

Similar presentations


Ads by Google