예제노트

MS-SQL 예제- 1 문제 및 답

박은주 2008. 4. 21. 21:32

정원혁 교제 MS-SQL 예제

# pubs database의 titles table 내에서 검색한다는 전제로 아래의 문항들을 완성해본다.

 

1. 모든 컬럼을(내용) 검색/출력한다.

 

USE pubs  /*pubs 데이터베이스를 이용하라*/

go

select *

from titles;

 

2. 테이블에서 모든 title과 title_id 만을 볼 수 있도록 검색/출력한다.

select title,title_id from titles;

 

3. 테이블의 title을 한글로 “타이틀”이라고 변경하여 출력한다.

(한글문자열은 작은 따옴표로 처리할 것)

 

select title as '타이틀' from titles;

 

 

4. 테이블의 title_id 는 TITLE_ID 로 변경 출력하고 나머지는 그대로 출력한다. (단, 주의사항은 변경할 문자열 컬럼은 띄워쓰기할 수 없다는 것이다)

 

select title as 'TITLE_ID' ,type,pub_id,price,advance,royalty,ytd_sales,notes,pubdate

from titles;

 

5. 테이블의 title_id 를 중심으로 오름차순으로 정렬하여 모든 컬럼을 출력한다.

 

select * from titles

order by  title_id ASC;

 

6. 테이블의 price(가격) 컬럼을 가격이 낮은 순서부터 우선적으로 정렬하여 품목(title)과 price만을 출력한다.

(오름차순(ascending)은 저->고, 내림차순(descending)은 고->저)

 

select  title,price from titles

order by price ASC;

 

 

7. 테이블의 pirce(가격)을 가격이 높은 순서부터 우선적으로 정렬하되 가격이 같을 경우 royalty(고객충성도:레벨) 우선으로 오름차순으로 정렬하여 모든 컬럼을 출력한다.

 

(유의할 사항은 desc은 “,” 앞에 있는 컬럼을 우선적으로 적용하게 된다)

select  title,price from titles

order by price desc ,royalty ASC;

 

8. 다음의 경우에는 어떠한 차이가 있는지 점검해보자.

 

Select title_id, price, royalty from titles

order by price desc, royalty desc;

 

Select title_id, price, royalty from titles

order by price desc, royalty asc;

 

--차이점: 가격이 같을때 전자의 경우는  royalty 높은 것부터 후자의 경우 royalty가 낮은 것부터 나타난다.

 

 

9. 데이터베이스의 테이블 리스트를 출력한다.

select * from sysobjects;

 

 

10. titles 테이블의 스키마 구조를 출력한다.

select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'titles';

 

11. 가격이 10(달러) 이상인 품목을 title, title_id, price 순으로 출력한다.

select title, title_id, price

from titles

where price >=10;

 

12. 가격이 10달러에서 15달러 미만인 제품을 오름차순으로 title, price 만출력한다.

select title, title_id, price

from titles

where price >10 and price<15;

order by title ASC;

 

 

13. title에 “Is”가 포함되어 있는 레코드를 출력하되 가격 오름차순으로 title_id, title, price 를 출력한다.

 

select  title_id,title, price

from titles

where title like '%Is%'

order by price ASC; 

 

14. title이 “Straight”로 시작하는 레코드를 검색하여 title, royalty만 출력한다.

 

select  title, royalty

from titles

where title like 'Straight%'; 

 

 

 

 

15. notes 컬럼 필드에 “A must”로 시작하는 레코드를 출력하되 notes 필드를 notation 이라고 가상 필드명을 사용하여 title 과 더불어 출력한다.

 

select title,notes as notation

from titles

where notes like 'A must%';

 

16. 위의 결과를 title과 note의 내용을 붙여서 하나의 great_title 이라는 가상 필드로써 출력해 보자.

 

select title+notes as 'great_title'

from titles;

 

 17. 15번의 결과에서 다시 great_title 이라는 가상 필드로 출력하되 title과 notes 사이에 ‘연결된’이라는 말을 삽입하여 필드로 출력해보자.

 

select title+'   ' +'연결된' +'    '+notes as 'great_title'

from titles;

 

18. 가격이 없는(null) 필드를 찾아서 title과 가격을 출력한다.

select title,price

from titles

where price is null;

 

19. 가격이 매겨져 있지 않은(not NULL) 필드를 제외하고 모든 레코드를 가격 오름차순으로 품목과 가격만을 출력한다.

 

select title,price

from titles

where price is not null

order by price ASC;

 

20. 다음의 표현과 똑 같은 쿼리(query)를 비교 연산자(등호, 부등호 등)를 이용하여 표현하여 보자.

 

(보기) Select title, price from titles where price between 10 and 20;

 

Select title, price

from titles

where price>=10 and price<=20;

[출처] select 예제|작성자 kkimch007