728x90
1. basic select
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
-- 베이직 select --
--1. * col,col
SELECT * FROM EMP;
select empno, ename, job from emp;
select * from dept;
--2. 산술연산 (+-*/, null)
SELECT sal, SAL*100
FROM EMP;
SELECT SAL, COMM, SAL+COMM
from emp;
--3. alias as
SELECT * FROM EMP;
SELECT EMPNO, HIREDATE HDATE, DEPTNO FROM EMP;
SELECT EMPNO as "E NO", HIREDATE AS HDATE, DEPTNO as dno
FROM EMP;
SELECT * FROM EMP;
SELECT * FROM EMP E;
select * from emp AS e; -- 에러
--4. 연결 || SMITH7736 SMITH_7736
SELECT EMPNO, ENAME
, ENAME||EMPNO nano
, ENAME||'-'||EMPNO as nano2
FROM EMP;
--5. DISTINCT 유니크하게 딱 한번씩만 출력
SELECT DEPTNO FROM EMP;
SELECT DISTINCT DEPTNO FROM EMP;
SELECT JOB FROM EMP;
select distinct job from emp;
select distinct job, distinct deptno from emp; --에러
SELECT DISTINCT JOB, DEPTNO FROM EMP;
--6. desc describe
DESC EMP;
DESCRIBE EMP;
|
cs |
2. select 제한조건 (where)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
-- select : 제한(where) --
--1. =(문자열,날짜), <=, <> != BETWEEN, IN(NOT IN), LIKE, IS NULL(IS NOT NULL)
--10번 부서 사람만 출력
SELECT *
FROM EMP
WHERE DEPTNO=10;
--직업이 CLERK인 사람만 출력
SELECT *
FROM EMP
where job = 'CLERK';
--2. AND, OR, (우선순위) NOT
-- 직업(job)이 CLERK이면서 급여(sal)가 1000 이상인 사람
SELECT *
FROM EMP
WHERE SAL >= 1000 and
job = 'CLERK';
-- 직업(job)이 CLERK이면서 급여(sal)가 1000 이상이거나
-- 또는 직업이 MANAGER인 사람
SELECT *
FROM EMP
WHERE (JOB = 'CLERK' AND SAL >= 1000)
or (job = 'MANAGER');
--직업이 MANAGER가 아닌 사람
SELECT *
FROM EMP
WHERE
(JOB != 'MANAGER' AND
JOB !='CLERK' AND
job !='CLERK')
OR
(JOB !='CLERK'AND
JOB !='CLERK')
;
SELECT *
FROM EMP
where job <> 'MANAGER';
SELECT *
FROM EMP
WHERE job NOT IN ('MANAGER','CLERK');
--직업이 MANAGER 이거나 CLERK인 사람
SELECT *
FROM EMP
where job in ('MANAGER','CLERK');
--where job='MANAGER' or job='CLERK';
--직업이 MANAGER가 아니거나 CLERK이 아닌 사람
SELECT *
FROM EMP
where job not in ('MANAGER','CLERK');
--where job!='MANAGER' and job!='CLERK';
--where job<>'MANAGER' and job<>'CLERK';
-- 급여가 1000이상 2000이하인 사람
select *
FROM EMP
WHERE SAL BETWEEN 1000 AND 2000;
--where sal>=1000 and sal<=2000;
-- 커미션을 받는 사람
SELECT *
FROM EMP
WHERE COMM IS NOT NULL AND COMM > 0;
-- 커미션을 받지 않는 사람
SELECT *
FROM EMP
where comm is null or comm=0;
--이름이 A로 시작하는 사람
SELECT *
FROM EMP
WHERE ENAME LIKE 'A%';
--이름의 두번째 글자에 A가 들어간 사람
SELECT *
FROM EMP
where ename like '_A%';
--이름의 끝에 S가 들어간 사람
SELECT *
FROM EMP
WHERE ENAME LIKE '%S';
--이름에 LA가 들어간 사람
SELECT *
FROM EMP
WHERE ENAME LIKE '%LA%';
|
cs |
3. 정렬 order by (asc, desc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
--3. ORDER BY ASC DESC ALIAS, (SAL ASC, NAME DESC)
--급여를 많이 받는 사람 순으로 출력
SELECT * FROM EMP
order by sal desc;
--급여를 적게 받는 사람 순으로 출력
SELECT * FROM EMP
order by sal asc;
SELECT * FROM EMP
order by sal;
-- 부서별 오름차순, 급여가 높은순 출력
SELECT * FROM EMP
ORDER BY
DEPTNO ASC,
sal desc;
-- 연봉(sal*12)이 높은 순 출력
SELECT SAL*12 as ann
FROM EMP
ORDER BY SAL*12 DESC;
SELECT SAL*12 as ann
FROM EMP
order by ann desc;
|
cs |
728x90
'SQL 공부' 카테고리의 다른 글
SQL - DDL ( create, drop, alter) (0) | 2022.01.06 |
---|---|
SQL - DML ( select, insert, update, delete) (0) | 2022.01.06 |
SQL - 집합 연산 (0) | 2022.01.06 |
SQL - JOIN (0) | 2022.01.06 |
SQL - 단일행 함수 & 그룹 함수 (0) | 2022.01.06 |