[DB] 2. SELECT 기본

김주희's avatar
Feb 26, 2025
[DB] 2. SELECT 기본

1. 용어 정리

  • select (projection 도구)
  • from (HDD의 테이블을 메모리로 퍼올리는 것 / load)
  • where (행 고르기 - HDD에서 연산됨 / 메모리에 들고와서 처리X)
  • projection (특정한 열만 선택하는 것 =/= selction)
  • table (전체 껍데기)
  • column (컬럼 제목)
  • row = record (행)
  • cursor
  • fullscan
  • constraint (제약조건 → 컬럼에 제약을 준다.)
  • unique (유일하다)
  • index → random access (direct하게 찾을 수 있음. 유일하면 index 만드는 게 좋다.)
  • schema (테이블 구조 →모르면 projection할 수 없음)
 
notion image
 
 

2. Select 문법 기본

처음에 select * from emp;를 통해 스키마 확인 후 ~

1. 기본 select

-- 1. 기본 select select * from emp;
notion image
 

2. 상세보기

desc는 describe의 약어
-- 2. 상세보기 desc emp;
notion image
 

3. 별칭 주기

as 생략 가능
-- 3. 별칭 주기 select empno as '사원번호' from emp; select ename "이름" from emp;
notion image
notion image
 

4. 중복 제거 -> 서로 다른 데이터 집합 만들기

-- 4. 중복 제거 {1, 2, 3, 4} -> 서로 다른 데이터 집합 만들기 select distinct job from emp;
notion image
 

5. 연결 연산자

concat은 overloading 되어있기 때문에 매개변수의 개수가 유동적이다
-- 5. 연결 연산자 select concat(ename , '의 직업은 ', job) "직원 소개" from emp;
notion image
 

6. 연산자

-- 6. 연산자 select ename, sal*12 from emp; -- 가로 연산은 쉽다 (세로가 어려움 -> Group By) -- 설계 시에 default 값으로 설정해두는게 낫다 select ename, concat(sal*12 + ifnull(comm, 0), "$") "연봉" from emp;
notion image
 

7. 원하는 행 찾기

원하는 행만 걸러서 찾는 거니까 where에서 수행
-- 7. 원하는 행 찾기 select * from emp where ename = 'SMITH';
notion image
 
data 자료형은 ‘-’과 ‘/’를 동일하게 취급한다
select * from emp where hiredate = '1980-12-17'; select * from emp where hiredate = '1980/12/17';
notion image
 
select * from emp where sal > 2000;
notion image
 

+) is null & is not null

-- is null & is not null select * from emp where comm is null;
notion image
 
select * from emp where comm is not null;
notion image
 

8. 복잡한 where절 (and & or)

IN Query를 통해서도 or 표현이 가능하다.
-- 8. 복잡한 where절 (and & or) select * from emp where sal = 800 and deptno = 20;
notion image
 

+) IN query

select * from emp where sal = 800 or sal = 1600; -- in query select * from emp where sal in (800, 1600);
notion image
 

+) between

-- sal >= 500 and sal <= 3000 select * from emp where sal between 500 and 3000;
notion image
 

9. Like 연산자 %, _

-- 9. Like 연산자 %, _ select ename from emp where ename like 'S%';
notion image
 
select ename from emp where ename like '%M%';
notion image
 
select ename from emp where ename like '___t%,,,,
notion image
 

10. 정규표현식

GPT한테 물어보자~!
-- 10. 정규 표현식 (Regular Expression) -- 어떤 문자열에서 특수한 규칙을 찾아야 할 때! select * from professor where name regexp '조|형';
notion image
 
 
  • 정규표현식(REGEXP)으로 email 패턴 확인
  • @ 기호가 하나 포함되어 있어야 함
  • @ 앞뒤에 @가 없는 문자가 최소 한 개 이상 있어야 함
  • 이메일 도메인이 **".net"**으로 끝나야 함
  1. ^ → 문자열의 시작을 의미
  1. [^@]+
    1. [] 대괄호 안의 ^는 **"해당 문자 제외"**를 의미 (즉, @ 제외)
    2. +1개 이상 반복을 의미
    3. 따라서, @를 포함하지 않는 문자들이 하나 이상 나와야 함
  1. @ → 반드시 @ 기호가 있어야 함
  1. [^@]+@ 다음에도 @를 포함하지 않는 문자들이 하나 이상 나와야 함
  1. \..(닷) 자체를 의미 (.은 정규식에서 특별한 의미를 가지므로 \를 붙여 이스케이프해야 함)
  1. net → 문자열의 마지막이 **"net"**으로 끝나야 함
  1. $ → 문자열의 을 의미
SELECT * FROM professor WHERE email REGEXP '^[^@]+@[^@]+\.net$';
notion image
 
  • 정규표현식(REGEXP)으로 패스워드를 검증
  • 대문자(A-Z), 소문자(a-z), 숫자(0-9), 특수 문자가 모두 포함되었는지 확인
-- 정규표현식(REGEXP)으로 패스워드를 검증, 대문자(A-Z), 소문자(a-z), 숫자(0-9), 특수문자가 모두 포함되었는지 확인 SELECT 'Aa1@bcde' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[@$!%*?&]).{8,}$' AS is_valid from dual; SELECT 'Password1!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid from dual;
notion image
 

전체 코드

-- 1. 기본 select select * from emp; -- 2. 상세보기 desc emp; -- 3. 별칭 주기 select empno as '사원번호' from emp; select ename "이름" from emp; -- 4. 중복 제거 {1, 2, 3, 4} -> 서로 다른 데이터 집합 만들기 select distinct job from emp; -- 5. 연결 연산자 select concat(ename , '의 직업은 ', job) "직원 소개" from emp; -- 6. 연산자 select ename, sal*12 from emp; -- 가로 연산은 쉽다 (세로가 어려움 -> Group By) -- 설계 시에 default 값으로 설정해두는게 낫다 select ename, concat(sal*12 + ifnull(comm, 0), "$") "연봉" from emp; -- 7. 원하는 행 찾기 select * from emp where ename = 'SMITH'; select * from emp where hiredate = '1980-12-17'; select * from emp where hiredate = '1980/12/17'; -- 행을 거르는 거니까 -> where에서 select * from emp where sal > 2000; -- is null & is not null select * from emp where comm is null; select * from emp where comm is not null; -- 8. 복잡한 where절 (and & or) select * from emp where sal = 800 and deptno = 20; select * from emp where sal = 800 or sal = 1600; -- in query select * from emp where sal in (800, 1600); -- sal >= 500 and sal <= 3000 select * from emp where sal between 500 and 3000; -- 9. Like 연산자 %, _ select ename from emp where ename like 'S%'; select ename from emp where ename like '%M%'; select ename from emp where ename like '___t%,,,, '; -- 10. 정규 표현식 (Regular Expression) -- 어떤 문자열에서 특수한 규칙을 찾아야 할 때! select * from professor where name regexp '조|형'; SELECT * FROM professor WHERE email REGEXP '^[^@]+@[^@]+\.net$'; -- 정규표현식(REGEXP)으로 패스워드를 검증, 대문자(A-Z), 소문자(a-z), 숫자(0-9), 특수문자가 모두 포함되었는지 확인 SELECT 'Aa1@bcde' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[@$!%*?&]).{8,}$' AS is_valid from dual; SELECT 'Password1!' REGEXP '^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,}$' AS is_valid from dual;
Share article

jay0628