sql #5

MSSQL 2019. 12. 8. 23:48

* select 문

select * from table

테이블로부터 모든 칼럼을 출력

 

select a, b from table

테이블로부터 a와 b 칼럼을 출력

 

*  산술 연산자

+, -, *, /

select a, b, b*100 from table

테이블로 부터 a,b 칼럼 출력, 열 이름 없는 칼럼에 b 칼럼 * 100을 곱하여 출력

 

* null

널 값에 아무리 더하고 곱해도 값은 널이다.

select a, b, c+d from table (d 값은 null)

c+d는 무조건 널 값이 나오게 된다.

널 값이라면 0으로 주고 싶다면 이땐 isnull을 사용하면 된다.

select a, b, c + isnull(d, 0) from table

d가 널이라면 0으로 바꿔주는 문장이다. 널이 아니라면 그냥 d 값.

 

* 칼럼명을 지정해주기

1. 칼럼 뒤에 as 를 붙이거나 

2. 안붙이면 됨.

select a as b from table / select a b from table

a 칼럼의 이름은 b로 바뀐다.

 

* 칼럼명에 특수문자 / 띄어쓰기 등 사용시

별칭 쪽에 ' ' / " " / [ ] 를 붙여줌

select * from a as '$$' from table

 

* 중복된 데이터는 한 번씩만 출력하기

select distinct a from table

a칼럼에서 중복된 것을 제외하고 1개씩만 출력함.

 

* where 절

select * from table where a >= 100

테이블로부터 a가 100보다 큰 모든 칼럼 출력

 

* where 절에 사용 가능한 비교 연산자

= 같다

> 보다 크다

< 보다 작다

>= 크거나 같다

<= 크거나 작다

<> != ^= 다르다

 

문자 검색 시에는 '' 작은따옴표를 찍어야함.

select * from table where a = '아아아'

 

날짜 조회 시에도 '' 작은따옴표를 찍어야함.

select * from table where abc < '2019/01/01'

또는 '2019.01.01'

 

* 논리 연산자

 

and: 두 조건을 모두 만족해야함

or: 둘 중 하나만 만족해도 됨

not: 만족하지 않은 것만

 

select * from table where a=10 and b=20

// a와 b가 모두 조건에 만족해야 검색

select * from table where a=10 or b=20

// a와 b 중 하나만 조건에 만족하면 검색

select * from table where not a=10

// a가 10이 아닌 것만 검색

 

not 과 같은 연산자

select * from where a != 10, a <> 10, a ^=10

 

* between / not between 연산자

논리 연산자를 통해 100과 200 사이의 값을 얻어내려면?

select * from table where a >= 100 and a <= 200

 

between 연산자를 이용함.

100이상 200이하

select * from table where a between 100 and 200

select * from table where a between '2019/01/01' and '2019/05/05'

 

not beween 연산자를 이용함.

300미만 400초과

select * from table where a not between 300 and 400

 

* in 연산자

10이거나 20이거나 30인 것을 검색할 때?

select * from table where a = 10 or a = 20 or a = 30

select * from table where a in (10, 20, 30)

 

반대로 10 20 30이 아닌 것을 검색할 땐?

select * from table where a != 10 or a != 20 or a != 30

select * from table where a not in (10, 20, 30)

 

* like 연산자

와일드 카드

%: 문자가 없거나 하나이상 문자가 어떤 것이든 상관없음

_: 어느 것이든 하나의 문자

 

김으로 시작하는 이름 찾기

select * from table where name like '김%'

김뒤에 글자가 없든 있든 김이란 첫 번째 문자 뒤에 아무거나 오면 다 찾음

 

중간에 현이 포함된 이름 찾기

select * from table where name like '%현%'

중간에 현만 있으면 앞 뒤로 아무거나 와도 상관 없음

 

두번째 글자가 성인 사람 찾기

select * from table where name llike '_성%'

 

세번째 글자가 성인 사람 찾기

select * from table where name llike '__성%'

 

* not like 연산자

이름에 충이 포함되지 않은 사람 검색

select * from table where name not like '%충%'

 

* is null, is not null 연산자

null과 같은 값을 찾을 때

select * from table where a = null

// 이러면 안나옴. 산술 연산자로 비교할 수 없기 때문

 

select * from table where a is null

a 칼럼 중에 null인 것만 출력

select * from table where a is not null

a 칼럼 중에 null이 아닌 것만 출력

 

* ordery by 절

정렬 순서

ASC 오름차순

숫자: 작은 값부터

문자: ㄱ부터

날짜: 가장 오래된 날짜

NULL: 가장 먼저

 

DESC 내림차순

숫자: 큰 값부터

문자: ㅎ부터

날짜: 가장 최근 날짜

NULL: 가장 늦게

 

select * from table order by a

select * from table order by a desc

 

두 개를 동시에 정렬할 때

select * from table order by a desc, b asc

//먼저 a를 내림차순 후에 b를 오름차순

 

* top 연산자

// with ties, percent

 

이름이 사전 순으로 빠른 상위 10명 출력하기

select top (10) * from table order by name

 

* 위에 10명과 b, c 칼럼 출력하기

select top (10) a, b, c from table order by name

 

* 만약 top 10명 중에 동률이 있을 때 동률까지 출력

select top (10) with ties * from table order by name

 

* 이름이 빠른 상위 10%만 출력하기

select top(10) percent * from table order by name

'MSSQL' 카테고리의 다른 글

sql #7  (0) 2019.12.08
sql #6  (0) 2019.12.08
sql #4  (0) 2019.12.08
sql #3  (0) 2019.12.08
sql #2  (0) 2019.12.08
블로그 이미지

ryancha9

https://blog.naver.com/7246lsy

,