Nested Subqueries in SQL - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Nested Subqueries in SQL

Description:

... of all branches that have assets greater than those of at least one in Brooklyn ... S.assets and S.branch_city = Brooklyn' ... where branch_city = Brooklyn' ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 13
Provided by: kenneth193
Category:

less

Transcript and Presenter's Notes

Title: Nested Subqueries in SQL


1
Nested Subqueries in SQL
  • By Kenneth Cheung
  • CS 157A Section 2
  • Professor Lee

2
Terms
  • For a query,
  • in- tests for set membership
  • select- chooses values
  • not in - absence of set membership

3
Example 1 Find all customers who have both a
loan and an account at a bank.
  • Start with the bank account
  • (select customer_name
  • from depositor)

4
Example 1
  • Find the list of customers who also have a loan
    account. combine using the outer select.
  • select distinct customer_name
  • from borrower
  • where customer_name in (select customer_name
  • from depositor)

5
Example 2 Find all customers who have a loan
from the bank but not an account.
  • select distinct customer_name
  • from borrower
  • where customer_name not in (select customer_name
  • from depositor)

6
Example 2 Find the names of all customers who
are not Smith nor Jones
  • not in can also be used for enumerated sets.
  • select distinct customer_name
  • from borrower
  • where customer_name not in (Smith, Jones)

7
Example 3 Find the names of all branches that
have assets greater than those of at least one in
Brooklyn
  • Set comparison using comparing operators
  • Use the rename operator as to compare elements in
    the same table
  • select distinct T.branch_name
  • from branch as T, branch as S
  • where T.assets gt S.assets and S.branch_city
    Brooklyn

8
Example 4 Another way using the some operator
  • some- at least one
  • select branch_name
  • from branch
  • where assets gt some (select assets
  • from branch
  • where branch_city Brooklyn)

9
Example 5 Find all branches that have an asset
value greater than that of each branch in
Brooklyn.
  • some - the same as in
  • ltgt all - the same as not in
  • select branch_name
  • from branch
  • where assets gt all (select assets
  • from branch
  • where branch_city Brooklyn)

10
Example 6 Find the branch with the highest
average balance.
  • SQL cannot compose of aggregate functions
    (function of a function), so it must be written
    in a certain way.
  • select branch_name
  • from account
  • group by branch_name
  • having avg (balance) gt all (select avg (balance)
  • from account
  • group by branch_name)

11
Example 7 find all customers who have both a
loan and an account at the bank.
  • exists - returns true if the argument in it is
    not empty
  • select customer_name
  • from borrower
  • where exists (select
  • from depositor
  • where depositor.customer_name
    borrower.customer_name)

12
Example 8
  • not exists- superset operation.
  • To show that relation A contains relation B, use
    not exists (B except A)
Write a Comment
User Comments (0)
About PowerShow.com