




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























and other bits of SQL
University of Edinburgh
Fall 2016
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
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 );
Aggregate functions can only be used in SELECT and HAVING
WHERE term op ( subquery ) ; Allowed as long as subquery returns a single value
WHERE (term 1 ,... , termn) op ( subquery ) ; Allowed as long as subquery returns a single row with n columns
(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 , A 2 , A 3 ) <= (B 1 , B 2 , Bn) (generalizes to n elements) ⇐⇒ A 1 < B 1 ∨
(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
I (^3) < ANY({ 1 , 2 , 3 }) is false I (^3) < ANY({ 2 , 3 , 4 }) is true I (^) What about 3 < ANY({})?
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 );
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 );
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 );
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 );
All nested queries can refer to attributes in the parent queries
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
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);
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)
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