ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [6주차] join, aggregate 실습
    25 - 1/데이터베이스 2025. 4. 23. 13:23

    - Inner join

    1. '성남지점'에서 계좌를 개설한 고객의 이름과 주소, 그리고 예금 잔액 검색

    select  c.name, c.address, d.balance
    from  client c inner join deposit d on (c.ssn=d.ssn)
    where  d.branch_name = '성남지점'
    order by   c.name;

     

    2. 예금 잔액이 10만원 이상인 계좌가 개설된 지점의 이름과 지점장, 잔액 검색

    select  distinct b.branch_name, b. branch_head
    from  branch b inner join deposit d on (b.branch_name = d.branch_name)
    where  d.balance >= 100000;

     

    3. 이름이 '김기식'인 고객이 소유한 예금의 계좌번호, 개설지점, 지점장, 잔액 검색

    select  d.deposit_num, d.branch_name, b.branch_head, d.balance
    from  deposit d inner join branch b on (d.branch_name=b.branch_name)
     inner join client c on (c.ssn = d.ssn)
    where  c.name = '김기식';

     

     

    - Outer join

    1. 계좌가 없는 고객의 이름과 ssn, 전화번호 검색

    select  c.name, c.ssn, c.phone
    from  client c left outer join deposit d on (c.ssn = d.ssn)
    where  d.deposit_num is null;

     

     

    - 집계함수

    1. 고객의 총 수를 계산

    select count(*)
    from client;

     

    2. 지역별 고객 수 출력

    select  count(*)
    from  client
    group by  address;

     

    3. '김기식' 고객의 계좌 수와 잔액의 총액 계산

    select  count(deposit_num), sum(balance)
    from  client c inner join deposit d on (c.ssn = d.ssn)
    where    c.name = '김기식';

     

    4. 각 고객의 계좌 수와 잔액의 총액 계산

    select  c.ssn, c.name, count(deposit_num), sum(balance)
    from  client c inner join deposit d on (c.ssn = d.ssn)
    group by  c.ssn, c.name;

     

    5. 보유계좌들의 잔액의 총액이 200,000 이상인 고객의 이름, ssn, 보유 계좌 수와 잔액의 총액 계산

    select    c.name, c.ssn, count(deposit_num), sum(balance)
    from   client c inner join deposit d on (c.ssn=d.ssn)
    group by  c.name, c.ssn
    having  sum(balance) >= 200000;

     

Designed by Tistory.