본문 바로가기

컴퓨터/데이터베이스

2008년 9월 4일 숙제

use pubs

select * from titles


1-1.
김대리는 1991.10.1부터 1994.12.31일까지 출판되고(pubdate), (royalty)를 받지 않은 모든 책의 정보를 보고싶다.
<title>
select * from titles where pubdate >= 1991-10-01 and pubdate <= 1994-12-31 and royalty is NULL

교수님 답 : select * from titles where pubdate between '1990/10/01' and '2000/12/31' and royalty is null

1-2.
김대리는 그의 판매처인 서점의 이름(stor_name)이 기억나지 않는다. 그 서점의 주소(stor_address)에 'first'라는
단어가 들어가는데 대문자, 소문자도 모른다. 그 서점의 서점번호(stor_id)와 서점이름(stor_name)을 보기위한
select문을 써라<stores>
select stor_id, stor_name from stores where stor_address like '%first%'

교수님 답 : select stor_id, stor_name from stores where stor_address like '%[fF][iI][rR][sS][tT]%'


select * from publishers

1-3
김대리는 출판사 이름(pub_name)이 'Books'로 끝나는 'Boston'이나 'Paris' 시(city)에 있는 출판사명단을
보고 싶다. (state)에 오름차순, (pub_name)에 내림차순으로 정렬하라 <publishers>
select * from publishers where pub_name like '%Books' and (city = 'Boston' or city = 'Paris')

교수님 답 : select * from publishers where pub_name like '%Books' and city in ('Boston', 'Paris')
                order by state asc, pub_name desc

use pubs


1-4
김대리는 주문일자(ord_date)가 오늘일자와 같은 달에 주문되고 주문 요일이 월요일이나 금요일인 주문에
대한 정보를 알고 싶다. <sales>
select * from sales
where month (ord_date) = month(getdate()) and DATENAME(dw,ord_date) = '월요일' or DATENAME(dw,ord_date) ='금요일'

select datepart(weekday, getdate()), dateadd(mm,6,getdate())

select getdate()

select dateformat 'ymd'

select DAY(date)

select DATENAME(dw,ord_date) from sales


교수님 답 : select *from sales
--where month(ord_date)= month(getdate())
where datepart(month,ord_date)=datepart(month,getdate())
      and datepart(weekday,ord_date) in (2, 6) 

1-5.
김대리는 책(title_id)별로 판매한 모든 수량(qty)의 합을 합의 역순으로 보고 싶다.<sales>
select sum(qty) from sales group by title_id order by sum(qty) desc

교수님 답 : select title_id, sum(qty) as '합계 수량'
                from sales
                group by title_id
                order by sum(qty) desc -- order by 2 desc

1-6
김대리는 각각의 서점별로 판매된 책의 수량(qty)의 합이 50을 넘는 서점의 번호(stor_id)와 수량의
합을 상위 3건만 보고 싶다. <sales>
select top 3 stor_id sum(qty) from sales
group by stor_id
having sum(qty) > 50

교수님 답 : select top 3 stor_id, sum(qty) as '합계수량'
                from sales
                group by stor_id
                having sum(qty) > 50
                order by 2 desc