sql: sub-queries single-value sub-queries single-column sub-queries sub-queries that produce tables...
DESCRIPTION
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 3TRANSCRIPT
SQL: Sub-queries
Single-value sub-queriesSingle-column sub-queries
Sub-queries that produce tablesCorrelated sub-queries
D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sub queries 1
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.TuparovINF 280 Database Systems:
SQL – Sub-queries 2
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.TuparovINF 280 Database Systems:
SQL – Sub-queries 3
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.TuparovINF 280 Database Systems:
SQL – Sub-queries 4
Fancy Fruits Database
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 5
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 OrdersWHERE Item_id = (SELECT Item_id FROM Stock WHERE descript = ‘KIWI’);
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 6
Single-valued sub-query (2)Example: Use of aggregate functions (Fancy Fruits DB)
SELECT *FROM VendorsWHERE cost < (SELECT avg(cost) FROM Vendors WHERE Item_id = ‘I02’);
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 7
Oracle 11g
MySQL 5.6
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.TuparovINF 280 Database Systems:
SQL – Sub-queries 8
Multi-valued sub-query (single column)• Multi-valued sub-query (continue)
– Use with list/set of values: IN and NOT INx 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.TuparovINF 280 Database Systems:
SQL – Sub-queries 9
Examples (Fancy Fruits DB/MySQL 5.6)Multi-valued (single-column) queries: ANY
SELECT DISTINCT Cust_idFROM OrdersWHERE quantity > ANY
(SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’);
SOMESELECT DISTINCT Cust_idFROM OrdersWHERE quantity > SOME
(SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’);
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 10
Examples (Fancy Fruits DB)Multi-valued (single-column) queries:
ALLSELECT DISTINCT Cust_idFROM OrdersWHERE quantity > ALL
(SELECT quantity FROM Orders WHERE Cust_id = ‘CCC’);
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 11
Oracle 11g
MySQL 5.6
Examples (Fancy Fruits DB)Multiple sub-queries -> On the same level
SELECT *FROM OrdersWHERE Cust_id =
(SELECT Cust_id FROM Customers WHERE cust_name = ‘Alice’ )
AND Item_id = (SELECT Item_id FROM Stock WHERE descript = ‘Oranges’ );
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 12
Oracle 11g
MySQL 5.6
Examples (Fancy Fruits DB)Multiple sub-queries -> Nested
SELECT Vendor_idFROM VendorsWHERE Item_id in ( SELECT Item_id FROM Orders
WHERE Cust_id = ( SELECT Cust_id
FROM Customers WHERE Cust_name = ‘Alice’ ) );
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 13
Oracle 11gMySQL 5.6
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.TuparovINF 280 Database Systems:
SQL – Sub-queries 14
Examples (Fancy Fruits DB/MySQL 5.6)Correlated sub-queries (1)SELECT *FROM Orders C1WHERE C1.quantity >
(SELECT AVG(quantity) FROM Orders C2 WHERE C2.Cust_id = C1.Cust_id );
D. Christozov / G.TuparovINF 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 C2 10 > 42.5 FALSEWHERE 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 C2 20 > 35 FALSEWHERE C2.Cust_id = ‘BBB’ the second row is not included into result
Examples (Fancy Fruits DB/MySQL 5.6)Correlated sub-queries (2)
SELECT *FROM Orders C1WHERE C1.quantity <
(SELECT AVG(quantity)FROM Orders C2WHERE C2.Cust_id = C1.Cust_id);
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 16
Examples (Fancy Fruits DB)Correlated sub-queries (3)
SELECT cust_nameFROM CustomersWHERE 100 <
(SELECT SUM(quantity)FROM OrdersWHERE Orders.Cust_id = Customers.Cust_id );
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 17
Examples (Fancy Fruits DB)Correlated sub-queries (4)
SELECT Item_idFROM OrdersGROUP BY Item_idHAVING sum(quantity) >
(SELECT 0.1*on_handFROM StockWHERE Orders.Item_id = Stock.Item_id);
D. Christozov / G.TuparovINF 280 Database Systems:
SQL – Sub-queries 18