[DB] 2. SELECT 기본

최재원's avatar
Feb 24, 2025
[DB] 2. SELECT 기본

1. 용어 정리

  • select → projection 도구
  • from → 하드디스크에 있는 table에 있는 where로 지정한 행을 메모리로 올리는 것(where이 없으면 전부)
  • where → 하드디스크에 있는 table에 있는 행을 고른다.
  • projection → 특정한 열값만 선택하는 것
  • table
  • column → 제목
  • row = record
  • cursor → 행을 가리키는 것
  • fullscan
  • constraint(제약) → column에 제약을 줄 수 있다.
  • unique
  • index(목차) → random access(바로 접근 가능)
  • schema → 테이블 구조, desc
notion image
notion image

2. Select문법

  • 영어 문법 같은 느낌

1. 기본

select * from emp;
notion image

2. 상세보기

desc emp;
notion image

3. 별칭 주기

select empno as '사원번호' from emp; select ename '이름' from emp;
  • as 생략 가능
notion image

4. 중복 제거 {1,2,3,4} -> distinct_서로 다른 집합을 만든다

select distinct job from emp;
notion image

5. 연결 연산자

select concat(ename,'의 직업은 ',job) "직원소개" from emp;
notion image

6. 연산자

select ename, sal * 12 from emp; select ename "이름", concat(sal*12 + ifnull(comm, 0), "$") "연봉" from emp;
  • (가로 연산은 쉽다. 세로 연산이 어려움 -> 그룹핑 필요)
notion image
notion image

7. 원하는 행 찾기

select * from emp where ename = 'SMITH'; select * from emp where hiredate = '1980-12-17'; select * from emp where hiredate = '1980/12/17'; select * from emp where sal > 2000; select * from emp where comm is null; select * from emp where comm is not null;
notion image

8. 복잡한 where

select * from emp where sal = 800 and deptno = 20; select * from emp where sal = 800 or sal = 1600; select * from emp where sal in (800, 1600); --같은 컬럼만 select * from emp where sal >= 500 and sal <= 3000; select * from emp where sal between 500 and 3000; --같은 컬럼만
notion image
 
Share article

jjack1