Docsity

Nested Queries, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Nested Queries and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Nested Queries

and other bits of SQL

Dr Paolo Guagliardo

University of Edinburgh

Fall 2016

Aggregate results in WHERE

The right way

Account Number Branch CustID Balance 111 London 1 1330. 222 London 2 1756. 333 Edinburgh 1 450.

Accounts with a higher balance than the average of all accounts

Aggregate results in WHERE

The wrong way

Accounts with a higher balance than the average of all accounts

SELECT A.number FROM Account A WHERE A.balance > AVG( SELECT A1.balance FROM Account A1 );

ERROR

Aggregate functions can only be used in SELECT and HAVING

Comparisons with subquery results

SELECT...

FROM...

WHERE term op ( subquery ) ; Allowed as long as subquery returns a single value

SELECT...

FROM...

WHERE (term 1 ,... , termn) op ( subquery ) ; Allowed as long as subquery returns a single row with n columns

Comparisons between tuples

(A 1 ,... An) = (B 1 ,... , Bn) ⇐⇒ A 1 = B 1 ∧ · · · ∧ An = Bn

(A 1 ,... An) <> (B 1 ,... , Bn) ⇐⇒ A 1 6 = B 1 ∨ · · · ∨ An 6 = Bn

(A 1 , A 2 , A 3 ) < (B 1 , B 2 , Bn) (generalizes to n elements) ⇐⇒ A 1 < B 1 ∨

A 1 = B 1 ∧

A 2 < B 2 ∨ (A 2 = B 2 ∧ A 3 < A 3 )

(A 1 , A 2 , A 3 ) <= (B 1 , B 2 , Bn) (generalizes to n elements) ⇐⇒ A 1 < B 1 ∨

A 1 = B 1 ∧

A 2 < B 2 ∨ (A 2 = B 2 ∧ A 3 ≤ A 3 )

ANY

(term,... , term) op ANY ( query )

True if there exists a row ¯r in the results of query such that (term,... , term) op ¯r is true

Examples:

I (^3) < ANY({ 1 , 2 , 3 }) is false I (^3) < ANY({ 2 , 3 , 4 }) is true I (^) What about 3 < ANY({})?

Examples with ANY / ALL

ID of customers from London who own an account SELECT C.custid FROM customer C WHERE C.city = ’London’ AND C.custid = ANY( SELECT A.custid FROM Account A );

Examples with ANY / ALL

ID of customers from London who own an account SELECT C.custid FROM customer C WHERE C.city = ’London’ AND C.custid = ANY( SELECT A.custid FROM Account A );

Customers living in cities without a branch SELECT (^) * FROM customer C WHERE C.city <> ALL ( SELECT A.branch FROM Account A );

Examples with IN / NOT IN

ID of customers from London who own an account SELECT C.custid FROM customer C WHERE C.city = ’London’ AND C.custid IN ( SELECT A.custid FROM Account A );

Examples with IN / NOT IN

ID of customers from London who own an account SELECT C.custid FROM customer C WHERE C.city = ’London’ AND C.custid IN ( SELECT A.custid FROM Account A );

Customers living in cities without a branch SELECT (^) * FROM customer C WHERE C.city NOT IN ( SELECT A.branch FROM Account A );

Correlated subqueries

All nested queries can refer to attributes in the parent queries

(Smarter) Example

Return customers who have an account in London SELECT (^) * FROM Customer C WHERE EXISTS ( SELECT 1 FROM Account A WHERE branch=’London’ AND A.custid = C.id );

parameters = attributes of a subquery that refer to outer queries

Examples with EXISTS / NOT EXISTS

ID of customers from London who own an account SELECT C.custid FROM customer C WHERE C.city = ’London’ AND EXISTS ( SELECT (^) * FROM Account A WHERE A.custid = C.id);

Scoping

A subquery has I (^) a local scope (its FROM clause) I (^) n outer scopes (where n is the level of nesting) (these are the FROM clauses of the parent queries)

Scoping

A subquery has I (^) a local scope (its FROM clause) I (^) n outer scopes (where n is the level of nesting) (these are the FROM clauses of the parent queries)

For each reference to an attribute

  1. Look for a binding in the local scope
  2. If no binding is found, look in the closest outer scope
  3. If no binding is found, look in the next closest outer scope
  4. ...
  5. If no binding is found, give error