꾸준히 하고싶은 개발자

sql 본문

카테고리 없음

sql

프라우스 2023. 2. 13. 17:58
  • 구조화 질의어 (Structured Query Language)로서 관계형 데이터 베이스 관리 시스템(RDBMS) 데이터를 관리하기 위해 설계된 특수 목적의 프로그램밍 언어
  • 명령어 종류

1. 데이터 정의 언어(DDL: Data Definition Language) 

- 테이블과 인덱스 구조 관리

2. 데이터 조작 언어(DML: Data Manipulation Language)

-  CRUD (데이터 생성Create,읽기Read,갱신Update,삭제Delete)

3. 데이터 제어 언어(DCL: Data Control Language)

- 데이터 액세스 제어,트랜잭션 처리

  • 검색 명령어 SELECT

문법 ([]: 생략가능)

 SELECT [ALL | DISTINCT] 컬럼명 [,컬럼명...]

 FROM 테이블명 [,테이블명...]

 [WHERE 조건식]

 [GROUP BY 컬럼명 [HAVING 조건식]]

 [ORDER BY 컬럼명]

 

테이블 내의 모든 정보 검색

ex> student 테이블의 모든 정보(모든 ) 검색, * 모든 열을 의미

select *

from student;

 

student 테이블명

 

특정 열의 내용 검색

ex> 학번(stu_no) 학생이름(stu_name) 검색, 열의 순서 변경가능

select stu_no, stu_name

from student;

 

중복 제거

ex1> 학과(stu_dept) 중복값 제거후 검색, distinct 사용한다

select distinct stu_dept 

from student;

 

ex2> 학년별(stu_grade), 반별(stu_class) 중복값 제거후 검색

select distinct stu_grade,stu_class 

from student;

 

수식을 포함한 검색

ex> 이름과, 1미터를 신장 검색 결과 (산술연산이 가능한 열에 +,-,*,/ 같은 산술연산자 사용가능-원본에는 미반영)

select stu_name, stu_height-100 

from student;

 

결과 열에 별칭(alias)부여하기

ex1> 학번(stu_no) 이름(stu_name) 검색 이때 stu_no ID stu_name NAME으로 변경

select stu_no as ID, stu_name as NAME 

from student;

 

ex2> 모든 검색 이때 stu_name이름 입니다 변경 (별칭은 “”)

select stu_no,stu_name as "이름 입니다”,stu_dept,stu_grade,stu_class,stu_gender,stu_height,stu_weight 

from student;

 

연결 연산자 (2 이상의 열을 합쳐서 검색)

ex1> 학과와 이름을 합쳐서 출력 하고 열이름은 학과성명으로 변경

select stu_dept || stu_name as 학과성명

 from student;

 

ex2> 위의 결과 출력값에 학과와 이름 사이에 콤마와 끝에입니다 붙여서 출력

select stu_dept || ',' || stu_name || '입니다' as 학과성명

from student;

 

wher 사용하기 (=, <, >, <=, >=, <>)  <> !=,^= 사용 가능

ex1> 컴퓨터정보과 학생들의 데이터 검색

select * 

from student 

where stu_dept='컴퓨터정보';

 

ex2> 몸무게가 80 같거나 작은학생

select * 

from student 

where stu_weight <=80;

 

논리 연산자 (not, and, or)

ex> 학과(stu_dept) 컴퓨터정보과 학생이면서 학년(stu_grade) 2학년인 학생

select *

from student 

where stu_dept='컴퓨터정보' and stu_grade=2;

 

범위 조건 (between ~ and ~ )

ex1> 체중이 60부터 70사이의 데이터 검색

select * 

from student 

where stu_weight between 60 and 70;

 

ex2> 위의 질의문을 수식을 이용하여 표기

select * 

from student 

where stu_weight >= 60 and stu_weight<=70;

 

ex3> 2014 학번 학생정보를 모두 검색

select * 

from student 

where stu_no between '20140001' and '20149999';

 

LIKE 이용한 검색 (%: 0 이상의 문자를 의미, _: 1개의 문자를 의미)

ex1> 김씨 성을 가진 학생들의 정보 검색

select * from student where stu_name like '%';

 

ex2> 학생중 두번째 문자가 학생의 이름을 검색

select * from student where stu_name like '_%';

 

ex3> 2014년도 학생들의 정보 검색

select * from student where stu_no like '2014%';

 

(null) 처리 (null 값은 비교연산자와 비교될 없으며, 연산의 결과는 참이 없다.)

ex1> 신장을 피트로 변환 (피트변환 공식 cm/30.48)

select stu_name,stu_height/30.48 from student;

 

ex2> 신장이 널값인 데이터 확인

select * from student where stu_height is null;

 

ex3> 신장이 널값이 아닌 데이터 확인

 select * from student where stu_height is not null;

 

IN (여러 조건 하나만 만족하는 처리)

ex> 학과(stu_dept) 컴퓨터 정보나 기계과인 학생의 학번(stu_no) 이름(stu_name) 검색

select stu_no, stu_name

from student

where stu_dept in ('컴퓨터정보','기계');

 

검색결과를 오름차순(기본값) or 내림차순 순으로 정렬하는 것으로 특정 열을 기준으로 정렬하고자 할때 ORDER BY 절을 사용한다.

ex1> 학생들의 정보를 학번기준으로 오름차순(ascending) 정렬

select * 

from student 

order by stu_no;

 

ex2> 학생들의 정보를 학번기준으로 내림차순(descending) 정렬

select * 

from student 

order by stu_no desc;

 

ex3> 학생 정보중 학번, 이름, 학과, 몸무게를 출력하시오. 몸무게에서 5키로를 빼며, 이름은 target으로 target 기준 오름차순으로 정렬후 출력하시오.

select stu_no, stu_name, stu_dept,stu_weight-5 as target from student order by target;

또는 select stu_no, stu_name, stu_dept,stu_weight-5 as target from student order by 4;

(열의 순서번호를 이용하여 정렬)

또는  select stu_no, stu_name, stu_dept,stu_weight-5 as target from student order by stu_weight-5;

(산술식의 열의 이름을 이용하여 정렬)

 

ex4> 학생들의 정보를 학과 기준으로 정렬 동일시 몸무게 기준으로 정렬

select * from student order by stu_dept,stu_weight;

 

ex5> 학생들의 정보를 학과 기준 오름차순으로 정렬 동일시 몸무게 기준 내림차순으로 정렬

select * from student order by stu_dept, stu_weight desc;