Learning SQL by Alan Beaulieu

Learning SQL by Alan Beaulieu

Author:Alan Beaulieu [Alan Beaulieu]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Database Management / Data Mining
ISBN: 9780596555580
Publisher: O'Reilly Media
Published: 2009-04-10T16:00:00+00:00


In some cases, the all operator is a bit more natural. The next example uses all to find accounts having an available balance smaller than all of Frank Tucker’s accounts:

mysql> SELECT account_id, cust_id, product_cd, avail_balance -> FROM account -> WHERE avail_balance < ALL (SELECT a.avail_balance -> FROM account a INNER JOIN individual i -> ON a.cust_id = i.cust_id -> WHERE i.fname = 'Frank' AND i.lname = 'Tucker'); +------------+---------+------------+---------------+ | account_id | cust_id | product_cd | avail_balance | +------------+---------+------------+---------------+ | 2 | 1 | SAV | 500.00 | | 5 | 2 | SAV | 200.00 | | 10 | 4 | CHK | 534.12 | | 11 | 4 | SAV | 767.77 | | 14 | 6 | CHK | 122.37 | | 19 | 8 | SAV | 387.99 | | 21 | 9 | CHK | 125.67 | | 25 | 10 | BUS | 0.00 | +------------+---------+------------+---------------+ 8 rows in set (0.17 sec)

Here’s the data returned by the subquery, which consists of the available balance from each of Frank’s accounts:

mysql> SELECT a.avail_balance -> FROM account a INNER JOIN individual i -> ON a.cust_id = i.cust_id -> WHERE i.fname = 'Frank' AND i.lname = 'Tucker'; +---------------+ | avail_balance | +---------------+ | 1057.75 | | 2212.50 | +---------------+ 2 rows in set (0.01 sec)

Frank has two accounts, with the lowest balance being $1,057.75. The containing query finds all accounts having a balance smaller than any of Frank’s accounts, so the result set includes all accounts having a balance less than $1,057.75.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.