select (조회)
insert (추가)   ┐                          (1줄_Row 증가)
delete (삭제)  │-> 실제 자료 변경 (n줄_Row 삭제)
update (수정) ┘                          (n줄(Row)정보수정)_수정/삭제)



-- count (What) : Null은 계산에 제외한다.
-- 집계 함수 -  갯수 : count(), 합계 : sum(), 평균 : avg(), 최대값 : max(), 최소값 : min()

select COUNT(sido) from DBpost.dbo.zipcode
select COUNT(distinct sido) from DBpost.dbo.zipcode

-- 제일비싼책 제목과 가격을 출력
select title, MAX(price) as 'price2' from titles -- 집계함수 + 일반함수 사용시
group by title  -- group by 사용

select SIDO, gugun, COUNT(zipcode)
 from DBpost.dbo.zipcode
 group by SIDO, GUGUN
 order by SIDO, GUGUN
 
 select COUNT(sido), COUNT(zipcode)
  from DBpost.dbo.zipcode

select MAX(price), MIN(price)
 from titles

-- 출판사별 출판한 책의 갯수
select distinct pub_id from titles

select pub_id, COUNT(title)
 from titles
 group by pub_id
 
select * from sales

-- 서점별 판매된 책 수량 (count : 거래 횟수)
select stor_id, sum(qty) as '수량' from sales
group by stor_id

-- 책별로 판매된 책 수량
select title_id, sum(qty) as '수량' from sales
group by title_id

-- '2008-01-10' ~ '2008-01-20' 사이 판매된 책수량

select ord_date, sum(qty) as '수량' from sales
 where '2008-01-10' <= ord_date
 and ord_date <= '2008-01-20'
 group by ord_date

-- '2008-01-10' ~ '2008-01-20' 사이 판매된 책수량합이 500개이상
select title_id, ord_date, sum(qty) as '수량' from sales
 where ord_date between '2008-01-10' and '2008-01-20'
                                         --and qty >= '500' -- where문에는 집계함수 사용금지
 group by title_id, ord_date
 having SUM(qty) >= 500      -- 대신 사용가능 동일 and qty >= '500'

-- 통계
select stor_id, title_id, SUM(qty)
 from sales
 group by stor_id, title_id
 order by stor_id, title_id


-- 나누어진 테이블의 내용을 합쳐서 결과만듬(조인Jion)
-- cross join 제목수 * 출판사수
 select * from titles
 select * from publishers

--join
select t.title, p.pub_name -- 합칠려는 테이블
 from titles t
 join publishers p --합칠려면 join 밑 on
  on  p.pub_id = t.pub_id
 order by t.title
 
 -- 책제목과 책을 출판한 출판사이름을 출력
 -- 조건 : 양족다 존재하는 데이터 inner join
 select titles.title, publishers.pub_name
  from titles, publishers
  where titles.pub_id = publishers.pub_id
  order by title
 
 -- 위와 동일 사용안함
 select t.title, p.pub_name
  from titles t, publishers p
  where t.pub_id = p.pub_id
  order by title
 
  -- inner join 젤중요함 (inner를 생략해도 됨)
  select       t.title, p.pub_name
  from         titles t
  inner join   publishers p -- join publishers p
  on           t.pub_id = p.pub_id
  order by     title

-- 조건 : 한쪽 테이블기준으로 하는 데이터 outer join(left, right)
-- 책을 기준으로 출판사 유무에 관계없이 join
-- 출판사 12개, 교재 목록 28권
-- - 출판사정보가 없는 비매품 2개
-- + 책을 출판한 적이 없는 출판사(말벗)
 select       t.title, p.pub_name
  from         titles t                    -- titles를 t로 단축
  left join   publishers p             -- publishers를 p로 단축
  on           t.pub_id = p.pub_id
  order by     title
 
-- left, right를 기준이 무엇이냐에 따라 달라짐
 select        p.pub_name, t.title
  from         titles t                     -- 기준
  right join   publishers p
  on           t.pub_id = p.pub_id
  order by     p.pub_name
 
  -- 한번도 출판한 적이 없는 출판사
 select            p.pub_name, t.title
  from             publishers p
  left outer join  titles t
  on               p.pub_id = t.pub_id
  where            t.title is null





말이 예쁜 아이


아이에게
좋은 옷을 입히고,
좋은 음식을 먹이는 것은
돈만 있으면 아무나 할 수 있는 일입니다.
그것이 아이의 겉모습을 빛나게 할지는 모르겠습니다.
하지만 아이에게 좋은 우리말을 쓰도록 도와주는
것은 돈 없이도 우리 아이를 빛나게 하는
훌륭한 자녀교육의 한 방법이
아닌가 합니다.


- 공규택의《말이 예쁜 아이 말이 거친 아이》중에서 -

'IT > [DB]' 카테고리의 다른 글

[DB] 11.11.23 - join, 하위질의  (0) 2011.11.23
[DB] 개념  (0) 2011.11.22
[DB] 11.11.21  (0) 2011.11.21
[DB] 11.11.18  (0) 2011.11.18
[DB] MSQL 11.11.17  (0) 2011.11.17

+ Recent posts