Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.

Similar presentations


Presentation on theme: "SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF."— Presentation transcript:

1 SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sub queries 1

2 General notes (1) Sub-query Queries used to produce values for processing within other statements (basically in filter-conditions WHERE or HAVING, and in FROM to join with other tables or as views for sequential query processing); Result of a sub-query are: – Tables may be used in FROM clause or with EXISTS / NOT EXISTS predicates; – Multivalued - the table has a single column; – Single row – may be used in row value constructor (with INSERT statement); – Scalar or single-valued - may be used in comparisons D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 2

3 General notes (2) -> General Rules The entire sub-query must always be enclosed in parentheses; The ORDER BY clause may not be used in a sub-query; Sub-queries may contain nested sub-queries: SQL evaluates them from inside out; When in doubt - qualify! Explicit qualification of column names is necessary only when referring to a table listed in FROM at a higher level query, but it is always permissible to qualify columns names to clarify the meaning; VISIBILITY: A sub-query may refer only to column names in tables listed in its FROM clause and the FROM clauses in outer (parent) queries. A sub-query may not access tables used only by a child query; D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 3

4 General notes (3) -> General rules (cont.) When a sub-query is one of the two operands involved in a comparison, the sub-query must be the second one -- it must follow the relational operator. The result of a sub-query can not be directly compared with the result of another sub-query; Sub-queries may not be used as operands in expressions; If you hesitate between using sub-query and join: – when the final result draws information from more than one table, use join; – when the final result involves information from only one of the tables -- use a sub-query (in general it is cheaper than join); – when necessary - combine. D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 4

5 Fancy Fruits Database D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 5

6 Single-valued sub-query (1) It may be used with relational operators just as if it is a constant. Example: Search test for equality on the unique attribute (Fancy Fruits DB/MySQL 5.6) SELECT * FROM Orders WHERE Item_id = (SELECT Item_id FROM Stock WHERE descript = ‘KIWI’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 6

7 Single-valued sub-query (2) Example: Use of aggregate functions (Fancy Fruits DB) SELECT * FROM Vendors WHERE cost < (SELECT avg(cost) FROM Vendors WHERE Item_id = ‘I02’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 7 Oracle 11g MySQL 5.6

8 Multi-valued sub-query (single column) Use with linking (quantified) predicates ANY, SOME, ALL – SOME and ANY are equivalent, they link a simple relational operator with the sub-query. The relational operator is applied to each row of the result of the sub- query and the logical expression is true if and only if one or more (at least one) rows in the sub-query result satisfy the comparison. – ALL is true if and only if the relevant comparison is true for each and every value in the sub-query result. Be careful when use ALL with relational operator equal! D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 8

9 Multi-valued sub-query (single column) Multi-valued sub-query (continue) – Use with list/set of values: IN and NOT IN x IN (a,b,c)  x = SOME (a, b, c) x NOT IN (a,b,c)  x <> ALL (a, b, c) – Use with EXISTS and NOT EXISTS  EXISTS is true if and only if there exists at least one row in the sub-query result and the result rows may have more than one column. Tables are used only with EXISTS and NOT EXISTS and are useful only as correlated sub-queries Multiple queries – Multiple sub-queries on the same level: used with compound logical expressions – Nested sub-queries D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 9

10 Examples (Fancy Fruits DB/MySQL 5.6) Multi-valued (single-column) queries: ANY SELECT DISTINCT Cust_id FROM Orders WHERE quantity > ANY (SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’); SOME SELECT DISTINCT Cust_id FROM Orders WHERE quantity > SOME (SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 10

11 Examples (Fancy Fruits DB) Multi-valued (single-column) queries: ALL SELECT DISTINCT Cust_id FROM Orders WHERE quantity > ALL (SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 11 Oracle 11g MySQL 5.6

12 Examples (Fancy Fruits DB) Multiple sub-queries -> On the same level SELECT * FROMOrders WHERE Cust_id = (SELECTCust_id FROMCustomers WHEREcust_name = ‘Alice’ ) AND Item_id = (SELECT Item_id FROMStock WHEREdescript = ‘Oranges’ ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 12 Oracle 11g MySQL 5.6

13 Examples (Fancy Fruits DB) Multiple sub-queries -> Nested SELECT Vendor_id FROM Vendors WHERE Item_id in ( SELECT Item_id FROM Orders WHERE Cust_id = ( SELECT Cust_id FROM Customers WHERE Cust_name = ‘Alice’ ) ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 13 Oracle 11g MySQL 5.6

14 Correlated sub-queries Some sub-queries are independent of their outer (parent) query: they return the same result any time they are evaluated. The result of other sub-queries, correlated, depends on the row being processed by the outer query: – the sub-query result varies with the row being processed by the outer query; – the sub-query must be re-evaluated for each row of the outer query; – processing time of correlated sub-queries is theoretically equal of joins, but practically much higher (when join combines tables according to fields, which are indexed). D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 14

15 Examples (Fancy Fruits DB/MySQL 5.6) Correlated sub-queries (1) SELECT * FROM Orders C1 WHERE C1.quantity > (SELECT AVG(quantity) FROM Orders C2 WHERE C2.Cust_id = C1.Cust_id ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 15 C1.Cust-id == ‘AAA’ Subquery: SELECT AVG(Quantity)produces:(10 + 30 + 60 + 70 )/ 4.0 = 42.5 FROM Orders C210 > 42.5  FALSE WHERE C2.Cust_id = ‘AAA’the first row is not included into result C1.Cust-id == ‘BBB’ Subquery: SELECT AVG(Quantity)produces:(20 + 50) / 2.0 = 35 FROM Orders C220 > 35  FALSE WHERE C2.Cust_id = ‘BBB’the second row is not included into result

16 Examples (Fancy Fruits DB/MySQL 5.6) Correlated sub-queries (2) SELECT * FROM Orders C1 WHERE C1.quantity < (SELECT AVG(quantity) FROM Orders C2 WHERE C2.Cust_id = C1.Cust_id); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 16

17 Examples (Fancy Fruits DB) Correlated sub-queries (3) SELECT cust_name FROM Customers WHERE 100 < (SELECT SUM(quantity) FROM Orders WHERE Orders.Cust_id = Customers.Cust_id ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 17

18 Examples (Fancy Fruits DB) Correlated sub-queries (4) SELECT Item_id FROM Orders GROUP BY Item_id HAVING sum(quantity) > (SELECT 0.1*on_hand FROM Stock WHERE Orders.Item_id = Stock.Item_id); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – Sub-queries 18


Download ppt "SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF."

Similar presentations


Ads by Google