์์ฑ 2026. 6. 12.ยท์์ 2026. 6. 12.
- DDL (Data Definition Language, ๋ฐ์ดํฐ ์ ์์ด)
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง(schema) ์ ์ ์๋จ ์ ๊ณต
- DML (Data Manipulation Language, ๋ฐ์ดํฐ ์กฐ์์ด)
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๋ณด ์ง์ ๋ฐ ํํ(tuple) ์ฝ์
, ์ญ์ , ์์ ๊ธฐ๋ฅ ์ ๊ณต
- Integrity (๋ฌด๊ฒฐ์ฑ)
- DDL์ ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด(integrity constraints) ๋ช
์ ๋ช
๋ น์ด ํฌํจ
- View definition (๋ทฐ ์ ์)
- DDL์ ๋ทฐ(view) ์ ์ ๋ช
๋ น์ด ํฌํจ
- Transaction control (ํธ๋์ญ์
์ ์ด)
- ํธ๋์ญ์
(transaction)์ ์์๊ณผ ๋์ ๋ช
์ํ๋ ๋ช
๋ น์ด ํฌํจ
- Embedded SQL and dynamic SQL (embedded SQL ๋ฐ ๋์ SQL)
- ๋ฒ์ฉ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๋ด์ SQL ๋ฌธ์ฅ์ ์ฝ์
ํ๋ ๋ฐฉ๋ฒ ์ ์
- Authorization (๊ถํ ๋ถ์ฌ)
- ๊ด๊ณ(relation) ๋ฐ ๋ทฐ์ ๋ํ ์ ๊ทผ ๊ถํ ๋ช
์ ๋ช
๋ น์ด ํฌํจ
- SQL DDL์ ๊ด๊ณ์ ๋ํ ์ ๋ณด ๋ช
์ ํ์ฉ, ๋ค์ ํฌํจ:
- ๊ฐ ๊ด๊ณ์ ์คํค๋ง
- ๊ฐ ์์ฑ(attribute)๊ณผ ๊ด๋ จ๋ ๊ฐ์ ์ ํ
- ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด
- ๊ฐ ๊ด๊ณ์ ๋ํด ์ ์ง๋ ์ธ๋ฑ์ค(index) ์งํฉ
- ๊ฐ ๊ด๊ณ์ ๋ํ ๋ณด์ ๋ฐ ๊ถํ ์ ๋ณด
- ๋์คํฌ ์์ ๊ฐ ๊ด๊ณ์ ๋ํ ๋ฌผ๋ฆฌ์ ์ ์ฅ ๊ตฌ์กฐ
char(n): ์ฌ์ฉ์ ์ง์ ๊ธธ์ด n์ ๊ณ ์ ๊ธธ์ด ๋ฌธ์์ดvarchar(n): ์ฌ์ฉ์ ์ง์ ์ต๋ ๊ธธ์ด n์ ๊ฐ๋ณ ๊ธธ์ด ๋ฌธ์์ดint: ์ ์ (๊ธฐ๊ณ์ ์์กดํ๋ ์ ์์ ์ ํ ๋ถ๋ถ ์งํฉ)smallint: ์์ ์ ์ (์ ์ ๋๋ฉ์ธ ์ ํ์ ๊ธฐ๊ณ ์์กด์ ๋ถ๋ถ ์งํฉ)numeric(p,d): p์๋ฆฌ์ ์ฌ์ฉ์ ์ง์ ์ ๋ฐ๋์ ์์์ ์ดํ d์๋ฆฌ๋ฅผ ๊ฐ์ง ๊ณ ์ ์์์ ์ซ์ (์: numeric(3,1)์ 44.5๋ ์ ํํ ์ ์ฅ ๊ฐ๋ฅํ๋, 444.5๋ 0.32๋ ๋ถ๊ฐ)real, double precision: ๊ธฐ๊ณ ์์กด์ ์ ๋ฐ๋๋ฅผ ๊ฐ์ง ๋ถ๋ ์์์ ๋ฐ ๋ฐฐ์ ๋ฐ๋ ๋ถ๋ ์์์ ์ซ์float(n): ์ต์ n์๋ฆฌ์ ์ฌ์ฉ์ ์ง์ ์ ๋ฐ๋๋ฅผ ๊ฐ์ง ๋ถ๋ ์์์ ์ซ์- ์ถ๊ฐ์ ์ธ ๋ด์ฉ์ 4์ฅ์์ ๋ค๋ฃธ
- SQL ๊ด๊ณ๋
create table ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ ์: create table r (A1 D1, A2 D2, ..., An Dn, (integrity-constraint1), ..., (integrity-constraintk));
- r์ ๊ด๊ณ์ ์ด๋ฆ
- ๊ฐ Aiโ๋ ๊ด๊ณ r์ ์คํค๋ง์ ์๋ ์์ฑ ์ด๋ฆ
- Diโ๋ ์์ฑ Aiโ์ ๋๋ฉ์ธ(domain)์ ์๋ ๊ฐ์ ๋ฐ์ดํฐ ์ ํ
- ์์:
create table instructor(
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2)
);
- ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด์ ์ ํ
primary key(A1, ..., An)- ์ด ๊ด๊ณ์ ๋ชจ๋ ํํ์ ์๋ก ๋ค๋ฅธ ๊ธฐ๋ณธ ํค ๊ฐ์ ๊ฐ์ ธ์ผ ํจ
- ์์ Primary-key constraint (๊ธฐ๋ณธ ํค ์ ์ฝ ์กฐ๊ฑด) ๊ฐ์
foreign key (Am, ..., An) references r- Foreign-key constraint (์ธ๋ ํค ์ ์ฝ ์กฐ๊ฑด) ๊ฐ์
not null- ์ด ์์ฑ์ด NULL ๊ฐ์ ๊ฐ์ง ์ ์๋๋ก ๊ฐ์
- SQL์ ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด์ ์๋ฐํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์
๋ฐ์ดํธ ๋ฐฉ์ง
- ์์:
create table instructor(
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department
);
- Insert
insert into instructor values('10211', 'Smith', 'Biology', 66000);
- Delete
student ๊ด๊ณ์์ ๋ชจ๋ ํํ ์ ๊ฑฐdelete from student;
- Drop Table
- Alter
alter table r add A D;- ์ฌ๊ธฐ์ A๋ ๊ด๊ณ r์ ์ถ๊ฐ๋ ์์ฑ์ ์ด๋ฆ์ด๊ณ D๋ A์ ๋๋ฉ์ธ
- ๊ด๊ณ์ ๋ชจ๋ ๊ธฐ์กด ํํ์ ์ ์์ฑ์ ๋ํด
null ๊ฐ์ผ๋ก ํ ๋น๋จ
alter table r drop A;- ์ฌ๊ธฐ์ A๋ ๊ด๊ณ r์ ์์ฑ ์ด๋ฆ
- ์์ฑ ์ญ์ ๋ ๋ง์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ง์ํ์ง ์์
- ์ผ๋ฐ์ ์ธ SQL ์ง์ ํํ:
select A1, A2, ..., An from r1, r2, ..., rm where P;
- Aiโ๋ ์์ฑ์ ๋ํ๋
- riโ๋ ๊ด๊ณ๋ฅผ ๋ํ๋
- P๋ ์ ์ด(predicate)
- ๋์ํ๋ ๊ด๊ณ ๋์(relational algebra)
ฮ A1โ,A2โ,โฆ,Anโโ(ฯ_P(r1โรr2โรโฏรrmโ))
- SQL ์ง์์ ๊ฒฐ๊ณผ๋ ๊ด๊ณ
select ์ ์ ์ง์ ๊ฒฐ๊ณผ์์ ์ํ๋ ์์ฑ์ ๋์ด- ๊ด๊ณ ๋์์ ํ๋ก์ ์
(projection) ์ฐ์ฐ์ ํด๋น
- ์์: ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ
select name from instructor;
- ์ฐธ๊ณ : SQL ์ด๋ฆ์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ง ์์ (์:
Name โก NAME โก name) - SQL์ ๊ด๊ณ๋ฟ๋ง ์๋๋ผ ์ง์ ๊ฒฐ๊ณผ์์๋ ์ค๋ณต์ ํ์ฉ
- ์ค๋ณต ์ ๊ฑฐ๋ฅผ ๊ฐ์ ํ๋ ค๋ฉด
select ๋ค์ distinct ํค์๋ ์ฝ์
- ๋ชจ๋ ๊ฐ์ฌ์ ํ๊ณผ ์ด๋ฆ์ ์ฐพ๊ณ ์ค๋ณต ์ ๊ฑฐ
select distinct dept_name from instructor;
all ํค์๋๋ ์ค๋ณต์ด ์ ๊ฑฐ๋์ง ์์์ผ ํจ์ ๋ช
์ select all dept_name from instructor;
select ์ ์ ๋ณํ(*)๋ "๋ชจ๋ ์์ฑ"์ ์๋ฏธ select * from instructor;select instructor.* from instructor, teaches;
- ์์ฑ์
from ์ ์์ด ๋ฆฌํฐ๋ด(literal)์ด ๋ ์ ์์ select '437';- ๊ฒฐ๊ณผ๋ "437" ๊ฐ์ ๊ฐ์ง ๋จ์ผ ํ๊ณผ ํ๋์ ์ด์ ๊ฐ์ง ํ
์ด๋ธ
- ๋ค์์ ์ฌ์ฉํ์ฌ ์ด์ ์ด๋ฆ ๋ถ์ฌ ๊ฐ๋ฅ:
- ์์ฑ์
from ์ ๊ณผ ํจ๊ป ๋ฆฌํฐ๋ด์ด ๋ ์ ์์ select 'A' from instructor;- ๊ฒฐ๊ณผ๋ ํ๋์ ์ด๊ณผ N๊ฐ์ ํ(
instructor ํ
์ด๋ธ์ ํํ ์)์ ๊ฐ์ง ํ
์ด๋ธ์ด๋ฉฐ, ๊ฐ ํ์ ๊ฐ์ "A"
select ์ ์ +, -, *, / ์ฐ์ฐ์ ํฌํจํ๋ ์ฐ์ ํํ์์ ํฌํจํ ์ ์์ผ๋ฉฐ, ์์ ๋๋ ํํ์ ์์ฑ์ ๋ํด ์ฐ์ฐ- ์ง์:
select ID, name, dept_name, salary/12 from instructor;instructor ๊ด๊ณ์ ๋์ผํ์ง๋ง salary ์์ฑ ๊ฐ์ด 12๋ก ๋๋์ด์ง ๊ด๊ณ๋ฅผ ๋ฐํ- ์ผ๋ฐํ๋ ํ๋ก์ ์
์ฐ์ฐ์ ํด๋น
as ์ ์ ์ฌ์ฉํ์ฌ "salary/12"์ ์ด๋ฆ ๋ณ๊ฒฝ ๊ฐ๋ฅ: select ID, name, dept_name, salary/12 as monthly_salary;
where ์ ์ ๊ฒฐ๊ณผ๊ฐ ๋ง์กฑํด์ผ ํ๋ ์กฐ๊ฑด์ ๋ช
์- ๊ด๊ณ ๋์์ ์ ํ(selection) ์ ์ด์ ํด๋น
- ์ปดํจํฐ ๊ณตํ(Comp. Sci.)๊ณผ์ ๋ชจ๋ ๊ฐ์ฌ ์ฐพ๊ธฐ
select name from instructor where dept_name = 'Comp. Sci.';
- SQL์ ๋
ผ๋ฆฌ์ ์ฐ๊ฒฐ์ฌ
and, or, not ์ฌ์ฉ์ ํ์ฉ - ๋
ผ๋ฆฌ์ ์ฐ๊ฒฐ์ฌ์ ํผ์ฐ์ฐ์๋ ๋น๊ต ์ฐ์ฐ์
<, <=, >, >=, =, <>๋ฅผ ํฌํจํ๋ ํํ์์ด ๋ ์ ์์ - ๋น๊ต๋ ์ฐ์ ํํ์์ ๊ฒฐ๊ณผ์ ์ ์ฉ ๊ฐ๋ฅ
- ๊ธ์ฌ๊ฐ 70000๋ณด๋ค ํฐ ์ปดํจํฐ ๊ณตํ๊ณผ์ ๋ชจ๋ ๊ฐ์ฌ ์ฐพ๊ธฐ
select name from instructor where dept_name = 'Comp. Sci.' and salary > 70000;
from ์ ์ ์ง์์ ๊ด๋ จ๋ ๊ด๊ณ๋ฅผ ๋์ด- ๊ด๊ณ ๋์์ ์นดํฐ์ ํ๋ก๋ํธ(Cartesian product) ์ฐ์ฐ์ ํด๋น
instructor ร teaches์ ์นดํฐ์ ํ๋ก๋ํธ ์ฐพ๊ธฐ select * from instructor, teaches;
- ๊ฐ๋ฅํ ๋ชจ๋
instructor-teaches ์์ ์์ฑํ๋ฉฐ, ๋ ๊ด๊ณ์ ๋ชจ๋ ์์ฑ์ ํฌํจ - ๊ณตํต ์์ฑ(์:
ID)์ ๊ฒฝ์ฐ, ๊ฒฐ๊ณผ ํ
์ด๋ธ์ ์์ฑ์ ๊ด๊ณ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ์ด๋ฆ์ด ๋ณ๊ฒฝ๋จ (์: instructor.ID) - ์นดํฐ์ ํ๋ก๋ํธ๋ ์ง์ ์ ์ผ๋ก๋ ๊ทธ๋ค์ง ์ ์ฉํ์ง ์์ง๋ง,
where ์ ์กฐ๊ฑด(๊ด๊ณ ๋์์ ์ ํ ์ฐ์ฐ)๊ณผ ๊ฒฐํฉํ๋ฉด ์ ์ฉ
- ์ด๋ค ๊ณผ๋ชฉ์ ๊ฐ๋ฅด์น ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ๊ณผ
course_id ์ฐพ๊ธฐ select name, course_id from instructor, teaches where instructor.ID = teaches.ID;
- ์์ (Art) ํ๊ณผ์์ ์ด๋ค ๊ณผ๋ชฉ์ ๊ฐ๋ฅด์น ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ๊ณผ
course_id ์ฐพ๊ธฐ select name, course_id from instructor, teaches where instructor.ID = teaches.ID and instructor.dept_name = 'Art';
- SQL์ ์ค๋ณต์ ํ์ฉํจ
- SQL์
as ์ ์ ์ฌ์ฉํ์ฌ ๊ด๊ณ์ ์์ฑ์ ์ด๋ฆ ๋ณ๊ฒฝ์ ํ์ฉ: - ๋ง์ฝ
new-name์ด ์์ฑ์ด ์๋ ๊ด๊ณ๋ฅผ ์ํ ๊ฒ์ด๋ผ๋ฉด, '์๊ด ์ด๋ฆ(correlation name)'(SQL ํ์ค), 'ํ
์ด๋ธ ๋ณ์นญ(table alias)', '์๊ด ๋ณ์(correlation variable)' ๋๋ 'ํํ ๋ณ์(tuple variable)'๋ผ๊ณ ๋ถ๋ฆผ - 'Comp. Sci.'์ ์๋ ์ด๋ค ๊ฐ์ฌ๋ณด๋ค ๋์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ:
select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Comp. Sci.';
as ํค์๋๋ ์ ํ ์ฌํญ์ด๋ฉฐ ์๋ต ๊ฐ๋ฅ (instructor as T โก instructor T)
- SQL์ ๋ฌธ์์ด ๋น๊ต๋ฅผ ์ํ ๋ฌธ์์ด ๋งค์นญ ์ฐ์ฐ์
like๋ฅผ ํฌํจํ๋ฉฐ, ๋ ๊ฐ์ ํน์ ๋ฌธ์๋ฅผ ์ฌ์ฉํ ํจํด์ ์ด์ฉ: - ํผ์ผํธ (
%): ๋ชจ๋ ํ์ ๋ฌธ์์ด๊ณผ ์ผ์น - ๋ฐ์ค (
_): ๋ชจ๋ ๋จ์ผ ๋ฌธ์์ ์ผ์น
- ์ด๋ฆ์ "dar" ํ์ ๋ฌธ์์ด์ ํฌํจํ๋ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ
select name from instructor where name like '%dar%';
- ๋ฌธ์์ด "100%"์ ์ผ์น์ํค๊ธฐ
like '100\%' escape '\'- ์ ์์์์ ๋ฐฑ์ฌ๋์(
\)๋ฅผ ์ด์ค์ผ์ดํ ๋ฌธ์๋ก ์ฌ์ฉ (๋ค๋ฅธ ์ด๋ค ๋ฌธ์๋ ์ด์ค์ผ์ดํ ๋ฌธ์๋ก ์ฌ์ฉ ๊ฐ๋ฅ)
- ํจํด์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถ
- ํจํด ๋งค์นญ ์์:
'Intro%': "Intro"๋ก ์์ํ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น'%Comp%': "Comp"๋ฅผ ํ์ ๋ฌธ์์ด๋ก ํฌํจํ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น'_ _ _': ์ ํํ ์ธ ๋ฌธ์๋ก ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น'_ _ _ %': ์ต์ ์ธ ๋ฌธ์๋ก ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น
- SQL์ ๋ค์ํ ๋ฌธ์์ด ์ฐ์ฐ ์ง์
- ์ฐ๊ฒฐ (
|| ์ฌ์ฉ) - ๋์๋ฌธ์ ๋ณํ
- ๋ฌธ์์ด ๊ธธ์ด ์ฐพ๊ธฐ, ํ์ ๋ฌธ์์ด ์ถ์ถ ๋ฑ
- ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ์ ์ํ๋ฒณ ์์ผ๋ก ๋์ด
select distinct name from instructor order by name;
- ๊ฐ ์์ฑ์ ๋ํด ๋ด๋ฆผ์ฐจ์์
desc, ์ค๋ฆ์ฐจ์์ asc๋ฅผ ๋ช
์ ๊ฐ๋ฅํ๋ฉฐ, ์ค๋ฆ์ฐจ์์ด ๊ธฐ๋ณธ๊ฐ - ์์:
order by name desc
- ์ฌ๋ฌ ์์ฑ์ ๋ํด ์ ๋ ฌ ๊ฐ๋ฅ
- ์์:
order by dept_name desc, name asc
- SQL์
between ๋น๊ต ์ฐ์ฐ์๋ฅผ ํฌํจ - ์์: ๊ธ์ฌ๊ฐ $90,000์์ $100,000 ์ฌ์ด์ธ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ (์ฆ, โฅ $90,000 ์ด๊ณ โค 100,000)
select name from instructor where salary between 90000 and 100000;
- ํํ ๋น๊ต
select name, course_id from instructor, teaches where (instructor.ID, dept_name) = (teaches.ID, 'Biology');
- ์๋ ์๋์ ๋์ผ
select name, course_id from instructor, teaches where instructor.ID = teaches.ID and dept_name = 'Biology';
- ์งํฉ ์ฐ์ฐ
union, intersect, except - ๊ฐ ์ฐ์ฐ์ ์๋์ผ๋ก ์ค๋ณต์ ์ ๊ฑฐ
- ๋ชจ๋ ์ค๋ณต์ ์ ์งํ๋ ค๋ฉด
union all (๋๋ intersect all ํน์ except all) ์ฌ์ฉ - 2017๋
๊ฐ์ ๋๋ 2018๋
๋ด์ ๊ฐ์ค๋ ๊ณผ๋ชฉ ์ฐพ๊ธฐ
(select course_id from section where sem = 'Fall' and year = 2017) union (select course_id from section where sem = 'Spring' and year = 2018);
- 2017๋
๊ฐ์๊ณผ 2018๋
๋ด์ ๋ชจ๋ ๊ฐ์ค๋ ๊ณผ๋ชฉ ์ฐพ๊ธฐ
(select course_id from section where sem = 'Fall' and year = 2017) intersect (select course_id from section where sem = 'Spring' and year = 2018);
- 2017๋
๊ฐ์์๋ ๊ฐ์ค๋์์ง๋ง 2018๋
๋ด์๋ ๊ฐ์ค๋์ง ์์ ๊ณผ๋ชฉ ์ฐพ๊ธฐ
(select course_id from section where sem = 'Fall' and year = 2017) except (select course_id from section where sem = 'Spring' and year = 2018);
- ์ด ํจ์๋ค์ ๊ด๊ณ ๋ด ๊ฐ๋ค์ ์ปฌ๋ ์
(์งํฉ ๋๋ ๋ค์ค ์งํฉ)์ ๋ํด ์๋ํ๋ฉฐ ๋จ์ผ ๊ฐ์ ๋ฐํ
avg: ํ๊ท ๊ฐ (์ซ์ ๊ฐ์๋ง ํด๋น)min: ์ต์๊ฐ (์ซ์ ๋ฐ ๋น์ซ์ ๊ฐ ๋ชจ๋ ํด๋น)max: ์ต๋๊ฐ (์ซ์ ๋ฐ ๋น์ซ์ ๊ฐ ๋ชจ๋ ํด๋น)sum: ๊ฐ์ ํฉ๊ณ (์ซ์ ๊ฐ์๋ง ํด๋น)count: ๊ฐ์ ๊ฐ์ (์ซ์ ๋ฐ ๋น์ซ์ ๊ฐ ๋ชจ๋ ํด๋น)
- SQL์์
count๋ฅผ ์ ์ธํ ์ง๊ณ ํจ์(aggregate functions)๋ null ๊ฐ์ ๋ฌด์ null ๊ฐ์ด ๋ฌด์๋ ๊ฒฐ๊ณผ๋ก ๊ฐ๋ค์ ์ปฌ๋ ์
์ด ๋น์ด ์์ ์ ์์- ๋ชจ๋ ์ง๊ณ ํจ์(
count ์ ์ธ)๋ ๋น ์ปฌ๋ ์
์ ์ ์ฉ๋ ๋ null์ ๋ฐํ (๋น ์ปฌ๋ ์
์ count๋ 0์ผ๋ก ์ ์๋จ) count ํจ์๋ ๋ค๋ฅธ non-null ๊ฐ์ฒ๋ผ ๋ชจ๋ null ๊ฐ์ ๊ณ์ฐ
- ์ปดํจํฐ ๊ณผํ ํ๊ณผ ๊ฐ์ฌ๋ค์ ํ๊ท ๊ธ์ฌ ์ฐพ๊ธฐ
select avg(salary) from instructor where dept_name = 'Comp. Sci.';
- 2018๋
๋ด ํ๊ธฐ์ ๊ณผ๋ชฉ์ ๊ฐ๋ฅด์น๋ ์ด ๊ฐ์ฌ ์ ์ฐพ๊ธฐ
select count(distinct ID) from teaches where semester = 'Spring' and year = 2018;
course ๊ด๊ณ์ ํํ ์ ์ฐพ๊ธฐ select count(*) from course;
group by ์ ์ ์ฌ์ฉํ์ฌ ๋จ์ผ ํํ ์งํฉ์ด ์๋, ๊ทธ๋ฃนํ๋ ์ฌ๋ฌ ํํ ์งํฉ์ ๊ฐ๋ณ์ ์ผ๋ก ์ง๊ณ ํจ์ ์ ์ฉ ๊ฐ๋ฅ- ๊ฐ ํ๊ณผ์ ๊ฐ์ฌ ํ๊ท ๊ธ์ฌ ์ฐพ๊ธฐ
select dept_name, avg(salary) as avg_salary from instructor group by dept_name;
group by ์ ์ฌ์ฉ ์, ๊ฐ ๊ทธ๋ฃน๋น ํ๋์ ํํ๋ง ์ถ๋ ฅ๋จ
- ์ง๊ณ ํจ์ ์ธ์
select ์ ์ ์๋ ์์ฑ๋ค์ group by ๋ชฉ๋ก์ ๋ํ๋์ผ ํจ /* erroneous query */select dept_name, ID, avg(salary) from instructor group by dept_name;
- ํน์ ๊ทธ๋ฃน(
dept_name์ผ๋ก ์ ์๋จ)์ ๊ฐ ๊ฐ์ฌ๋ ๋ค๋ฅธ ID๋ฅผ ๊ฐ์ง ์ ์์ง๋ง, ๊ฐ ๊ทธ๋ฃน์ ๋ํด ํ๋์ ํํ๋ง ์ถ๋ ฅ๋จ
having ์ ์ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ๋ ๊ฐ ๊ทธ๋ฃน์ ๋ํ ์กฐ๊ฑด ๋ช
์ ๊ฐ๋ฅhaving ์ ์ ์ ์ด๋ ๊ทธ๋ฃน ํ์ฑ ํ์ ์ ์ฉ๋๋ ๋ฐ๋ฉด, where ์ ์ ์ ์ด๋ ๊ทธ๋ฃน ํ์ฑ ์ ์ ์ ์ฉ๋จ- ํ๊ท ๊ธ์ฌ๊ฐ 42000๋ณด๋ค ํฐ ๋ชจ๋ ํ๊ณผ์ ์ด๋ฆ๊ณผ ํ๊ท ๊ธ์ฌ ์ฐพ๊ธฐ
select dept_name, avg(salary) as avg_salary
from instructor
group by dept_name
having avg(salary) > 42000;
- ํ๊ฐ ์์
from ์ ์ด ๋จผ์ ํ๊ฐ๋จwhere ์ ์ ์ ์ด(์กด์ฌ ์)๊ฐ from ์ ์ ๊ฒฐ๊ณผ์ ์ ์ฉ๋จwhere ์ ์ด๋ฅผ ๋ง์กฑํ๋ ํํ๋ค์ด group by ์ ์ ์ํด ๊ทธ๋ฃน์ผ๋ก ๋ฐฐ์น๋จhaving ์ (์กด์ฌ ์)์ด ๊ฐ ๊ทธ๋ฃน์ ์ ์ฉ๋จ; having ์ ์ ์ด๋ฅผ ๋ง์กฑํ์ง ์๋ ๊ทธ๋ฃน๋ค์ ์ ๊ฑฐ๋จselect ์ ์ ๋จ์ ๊ทธ๋ฃน๋ค์ ์ฌ์ฉํ์ฌ, ์ง๊ณ ํจ์(์กด์ฌ ์)๋ฅผ ์ ์ฉํ ํ ๊ฐ ๊ทธ๋ฃน์ ๋ํ ๋จ์ผ ๊ฒฐ๊ณผ ํํ์ ์ป์
- SQL์ ์๋ธ์ฟผ๋ฆฌ(subquery)์ ์ค์ฒฉ ๋ฉ์ปค๋์ฆ ์ ๊ณต
- ์๋ธ์ฟผ๋ฆฌ๋ ๋ค๋ฅธ ์ฟผ๋ฆฌ ๋ด์ ์ค์ฒฉ๋
select-from-where ํํ์ - ์ค์ฒฉ์ ๋ค์ SQL ์ฟผ๋ฆฌ์์ ์ํ๋ ์ ์์:
select A1, A2, ..., An from r1, r2, ..., rm where P - ๋ค์๊ณผ ๊ฐ์ด:
from ์ : riโ๋ ์ ํจํ ์๋ธ์ฟผ๋ฆฌ๋ก ๋์ฒด ๊ฐ๋ฅwhere ์ : P๋ B <operation> (subquery) ํํ์ ํํ์์ผ๋ก ๋์ฒด ๊ฐ๋ฅ. ์ฌ๊ธฐ์ B๋ ์์ฑselect ์ : Aiโ๋ ๋จ์ผ ๊ฐ์ ์์ฑํ๋ ์๋ธ์ฟผ๋ฆฌ(์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ, scalar subquery)๋ก ๋์ฒด ๊ฐ๋ฅ
- 2017๋
๊ฐ์๊ณผ 2018๋
๋ด์ ์ ๊ณต๋ ๊ณผ๋ชฉ ์ฐพ๊ธฐ
select distinct course_id
from section
where semester = 'Fall' and year = 2017 and
course_id in (select course_id
from section
where semester = 'Spring' and year = 2018);
- 2017๋
๊ฐ์์๋ ์ ๊ณต๋์์ง๋ง 2018๋
๋ด์๋ ์ ๊ณต๋์ง ์์ ๊ณผ๋ชฉ ์ฐพ๊ธฐ
select distinct course_id
from section
where semester = 'Fall' and year = 2017 and
course_id not in (select course_id
from section
where semester = 'Spring' and year = 2018);
- ์ด๋ฆ์ด "Mozart"๋ "Einstein"๋ ์๋ ๋ชจ๋ ๊ฐ์ฌ ์ด๋ฆ ์ง์
select distinct name from instructor where name not in ('Mozart', 'Einstein');
- ID๊ฐ 10101์ธ ๊ฐ์ฌ๊ฐ ๊ฐ๋ฅด์น ๊ณผ๋ชฉ ์น์
์ ์๊ฐํ (๊ณ ์ ํ) ํ์๋ค์ ์ด ์ ์ฐพ๊ธฐ
select count(distinct ID)
from takes
where (course_id, sec_id, semester, year) in (select course_id, sec_id, semester, year
from teaches
where teaches.ID = '10101');
- ์ฐธ๊ณ : ์ ์ฟผ๋ฆฌ๋ ํจ์ฌ ๊ฐ๋จํ ๋ฐฉ์์ผ๋ก ์์ฑ ๊ฐ๋ฅํ๋ฉฐ, ์ ๊ณต์์ SQL ๊ธฐ๋ฅ ์ค๋ช
๋ชฉ์ ์
- ์๋ฌผํ(Biology) ํ๊ณผ์ ์๋ ์ด๋ค (์ ์ด๋ ํ ๋ช
์) ๊ฐ์ฌ๋ณด๋ค ๊ธ์ฌ๊ฐ ๋์ ๊ฐ์ฌ๋ค์ ์ด๋ฆ ์ฐพ๊ธฐ
select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Biology';
some ์ ์ ์ฌ์ฉํ ๋์ผํ ์ฟผ๋ฆฌ select name from instructor where salary > some (select salary from instructor where dept_name = 'Biology');
F <comp> some r โโtโr ุจุญูุซ ุฃู (F<comp>t)- ์ฌ๊ธฐ์
<comp>๋ <, <=, >, =, !=๊ฐ ๋ ์ ์์ (5 < some {0, 5, 6}) = true (5 < some {0, 5, 0}) = false (5 = some {0, 5, 0}) = true (5 != some {0, 5, 5}) = true (โต 0๎ =5)(= some) โก in(!= some)์ not in๊ณผ ๋์น๊ฐ ์๋
- ์๋ฌผํ ํ๊ณผ์ ๋ชจ๋ ๊ฐ์ฌ์ ๊ธ์ฌ๋ณด๋ค ๋ ๋ง์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ
select name from instructor where salary > all (select salary from instructor where dept_name = 'Biology');
F <comp> all r โโtโr(F<comp>t) (5 < all {0, 5, 6}) = false (5 < all {6, 10, 4}) = false (5 = all {4, 5, 6}) = false (5 != all {4, 6}) = true (โต 5๎ =4 ์ด๊ณ 5๎ =6)(= all)์ in๊ณผ ๋์น๊ฐ ์๋(!= all) โก not in
exists ๊ตฌ๋ฌธ์ ์ธ์ ์๋ธ์ฟผ๋ฆฌ๊ฐ ๋น์ด ์์ง ์์ผ๋ฉด true๋ฅผ ๋ฐํexists r โr๎ =โ
not exists r โr=โ
- "2017๋
๊ฐ์ ํ๊ธฐ์ 2018๋
๋ด ํ๊ธฐ ๋ชจ๋์ ๊ฐ๋ฅด์น ๋ชจ๋ ๊ณผ๋ชฉ ์ฐพ๊ธฐ" ์ฟผ๋ฆฌ๋ฅผ ์ง์ ํ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ
select course_id
from section as S
where semester = 'Fall' and year = 2017 and
exists (select *
from section as T
where semester = 'Spring' and year = 2018 and
S.course_id = T.course_id);
- SQL์์, ์ธ๋ถ ์ฟผ๋ฆฌ์ ์๊ด ์ด๋ฆ(๋ณ์
S)์ where ์ ์ ์๋ธ์ฟผ๋ฆฌ์์ ์ฌ์ฉ๋ ์ ์์ - ์ธ๋ถ ์ฟผ๋ฆฌ์ ์๊ด ์ด๋ฆ์ ์ฌ์ฉํ๋ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ์๊ด ์๋ธ์ฟผ๋ฆฌ(correlated subquery)๋ผ๊ณ ํจ
- ์๋ฌผํ ํ๊ณผ์์ ์ ๊ณตํ๋ ๋ชจ๋ ๊ณผ๋ชฉ์ ์๊ฐํ ๋ชจ๋ ํ์ ์ฐพ๊ธฐ
- XโY=โ
โXโY ์ฐธ๊ณ
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
from course
where dept_name = 'Biology')
except
(select T.course_id
from takes as T
where S.ID = T.ID) );
- ์ฒซ ๋ฒ์งธ ์ค์ฒฉ ์ฟผ๋ฆฌ๋ ์๋ฌผํ์์ ์ ๊ณตํ๋ ๋ชจ๋ ๊ณผ๋ชฉ์ ๋์ด
- ๋ ๋ฒ์งธ ์ค์ฒฉ ์ฟผ๋ฆฌ๋ ํน์ ํ์์ด ์๊ฐํ ๋ชจ๋ ๊ณผ๋ชฉ์ ๋์ด
unique ๊ตฌ๋ฌธ์ ์๋ธ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์ ์ค๋ณต ํํ์ด ์๋์ง ํ
์คํธ- ์ฃผ์ด์ง ์๋ธ์ฟผ๋ฆฌ์ ์ค๋ณต์ด ์์ผ๋ฉด
true๋ก ํ๊ฐ๋จ - 2017๋
์ ์ต๋ ํ ๋ฒ ์ ๊ณต๋ ๋ชจ๋ ๊ณผ๋ชฉ ์ฐพ๊ธฐ
select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id = R.course_id and R.year = 2017);
- SQL์
from ์ ์์ ์๋ธ์ฟผ๋ฆฌ ํํ์์ ์ฌ์ฉํ๋๋ก ํ์ฉ - ํ๊ท ๊ธ์ฌ๊ฐ $42,000 ์ด์์ธ ํ๊ณผ๋ค์ ๊ฐ์ฌ ํ๊ท ๊ธ์ฌ ์ฐพ๊ธฐ
select dept_name, avg_salary
from (select dept_name, avg(salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;
having ์ ์ ์ฌ์ฉํ ํ์๊ฐ ์๋ค๋ ์ ์ ์ ์- ์ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ
select dept_name, avg_salary
from (select dept_name, avg(salary)
from instructor
group by dept_name)
as dept_avg(dept_name, avg_salary)
where avg_salary > 42000;
with ์ ์ with ์ ์ด ๋ฐ์ํ๋ ์ฟผ๋ฆฌ์์๋ง ์ ์๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ์์ ๊ด๊ณ(์์ ๋ทฐ)๋ฅผ ์ ์ํ๋ ๋ฐฉ๋ฒ ์ ๊ณต- ํ๊ท ์์ฐ๋ณด๋ค ๋ ํฐ ์์ฐ์ ๊ฐ์ง ๋ชจ๋ ํ๊ณผ ์ฐพ๊ธฐ
with avg_budget(value) as
(select avg(budget) from department)
select department.name
from department, avg_budget
where department.budget > avg_budget.value;
- ์ด ๊ธ์ฌ๊ฐ ๋ชจ๋ ํ๊ณผ์ ์ด ๊ธ์ฌ ํ๊ท ๋ณด๋ค ํฐ ๋ชจ๋ ํ๊ณผ ์ฐพ๊ธฐ
with dept_total (dept_name, value) as
(select dept_name, sum(salary)
from instructor
group by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.value > dept_total_avg.value;
- ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ๋ ๋จ์ผ ๊ฐ์ด ์์๋๋ ๊ณณ์ ์ฌ์ฉ๋๋ ์๋ธ์ฟผ๋ฆฌ
- ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ๋ ์ธ๋ถ ์ฟผ๋ฆฌ์
select ์ ์์ ์ฌ์ฉ๋ ์ ์์ - ๊ฐ ํ๊ณผ์ ๊ฐ์ฌ ์์ ํจ๊ป ๋ชจ๋ ํ๊ณผ ๋์ด
select dept_name,
(select count(*)
from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;
- ์๋ธ์ฟผ๋ฆฌ๊ฐ ํ๋ ์ด์์ ๊ฒฐ๊ณผ ํํ์ ๋ฐํํ๋ฉด ๋ฐํ์ ์ค๋ฅ ๋ฐ์
- ํํ์ด ์ผ๋ถ ์์ฑ์ ๋ํด
null๋ก ํ์๋๋ null ๊ฐ์ ๊ฐ์ง ์ ์์ null์ ์ ์ ์๋ ๊ฐ์ด๊ฑฐ๋ ๊ฐ์ด ์กด์ฌํ์ง ์์์ ์๋ฏธnull์ ํฌํจํ๋ ๋ชจ๋ ์ฐ์ ํํ์์ ๊ฒฐ๊ณผ๋ null- ์ง๊ณ ํจ์๋
null ๊ฐ์ ๋ฌด์ - ์ค๋ณต ์ ๊ฑฐ ๋ฐ ๊ทธ๋ฃนํ์ ๊ฒฝ์ฐ,
null์ ๋ค๋ฅธ ๊ฐ์ฒ๋ผ ์ทจ๊ธ๋๋ฉฐ, ๋ null์ ๋์ผํ ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋จ is null ์ ์ด๋ null ๊ฐ์ ํ์ธํ๋ ๋ฐ ์ฌ์ฉ๋ ์ ์์- ์์: ๊ธ์ฌ๊ฐ
null์ธ ๋ชจ๋ ๊ฐ์ฌ ์ฐพ๊ธฐ select name from instructor where salary is null;
is not null ์ ์ด๋ ์ ์ฉ๋ ๊ฐ์ด null์ด ์๋ ๊ฒฝ์ฐ ์ฑ๊ณต- SQL์
null ๊ฐ์ ํฌํจํ๋ ๋น๊ต์ ๊ฒฐ๊ณผ๋ฅผ unknown์ผ๋ก ์ฒ๋ฆฌ (is null ๋ฐ is not null ์ ์ด ์ ์ธ) - ์์:
5 < null ๋๋ null <> null ๋๋ null = null where ์ ์ ์ ์ด๋ ๋ถ๋ฆฌ์ธ(boolean) ์ฐ์ฐ(and, or, not)์ ํฌํจํ ์ ์์ผ๋ฏ๋ก, unknown ๊ฐ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๋ถ๋ฆฌ์ธ ์ฐ์ฐ์ ์ ์๋ฅผ ํ์ฅํด์ผ ํจ and: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknownor: (unknown or true) = true, (unknown or false) = unknown, (unknown or unknown) = unknownnot: (not unknown) = unknown
- SQL์์
P is unknown์ ์ ์ด P๊ฐ unknown์ผ๋ก ํ๊ฐ๋๋ฉด true๋ก ํ๊ฐ๋จ where ์ ์ ์ด์ ๊ฒฐ๊ณผ๋ unknown์ผ๋ก ํ๊ฐ๋๋ฉด false๋ก ์ฒ๋ฆฌ๋จ
- ์ฃผ์ด์ง ๊ด๊ณ์์ ํํ ์ญ์
- ์ฃผ์ด์ง ๊ด๊ณ์ ์ ํํ ์ฝ์
- ์ฃผ์ด์ง ๊ด๊ณ์ ์ผ๋ถ ํํ ๊ฐ ์
๋ฐ์ดํธ
- ๋ชจ๋ ๊ฐ์ฌ ์ญ์
- ์ฌ๋ฌด(Finance) ํ๊ณผ์ ๋ชจ๋ ๊ฐ์ฌ ์ญ์
delete from instructor where dept_name = 'Finance';
'ITBT' ๊ฑด๋ฌผ์ ์์นํ ํ๊ณผ์ ๊ด๋ จ๋ instructor ๊ด๊ณ์ ๋ชจ๋ ํํ ์ญ์ delete from instructor where dept_name in (select dept_name from department where building = 'ITBT');
- ๊ธ์ฌ๊ฐ ๊ฐ์ฌ ํ๊ท ๊ธ์ฌ๋ณด๋ค ์ ์ ๋ชจ๋ ๊ฐ์ฌ ์ญ์
- ๋ฌธ์ ์ :
instructor์์ ํํ์ ์ญ์ ํจ์ ๋ฐ๋ผ ํ๊ท ๊ธ์ฌ๊ฐ ๋ณ๊ฒฝ๋จ - SQL์ ํด๊ฒฐ์ฑ
:
- ๋จผ์
avg(salary)๋ฅผ ๊ณ์ฐํ๊ณ ์ญ์ ํ ๋ชจ๋ ํํ์ ์ฐพ์ - ์์์ ์ฐพ์ ๋ชจ๋ ํํ์ ์ญ์ (ํ๊ท ์ ์ฌ๊ณ์ฐํ๊ฑฐ๋ ํํ์ ๋ค์ ํ
์คํธํ์ง ์์)
delete from instructor
where salary < (select avg(salary) from instructor);
course์ ์ ํํ ์ถ๊ฐ insert into course values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
- ๋๋ ๋๋ฑํ๊ฒ
insert into course (course_id, title, dept_name, credits) values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
tot_creds๊ฐ null๋ก ์ค์ ๋ ์ ํํ์ student์ ์ถ๊ฐ insert into student values ('3003', 'Green', 'Finance', null);
- 144 ํ์ ์ด์์ ์ด์ํ ์์
(Music) ํ๊ณผ์ ๊ฐ ํ์์ ๊ธ์ฌ $18,000์ ์์
ํ๊ณผ ๊ฐ์ฌ๋ก ๋ง๋ฆ
insert into instructor select ID, name, dept_name, 18000 from student where dept_name = 'Music' and total_cred > 144;
student ๊ด๊ณ์ ์คํค๋ง: (ID, name, dept_name, tot_cred)instructor ๊ด๊ณ์ ์คํค๋ง: (ID, name, dept_name, salary)select from where ๋ฌธ์ ๊ทธ ๊ฒฐ๊ณผ ์ค ์ด๋ค ๊ฒ์ด๋ผ๋ ๊ด๊ณ์ ์ฝ์
๋๊ธฐ ์ ์ ์์ ํ ํ๊ฐ๋จ
- ๋ชจ๋ ๊ฐ์ฌ์๊ฒ 5% ๊ธ์ฌ ์ธ์
update instructor set salary = salary * 1.05;
- 70000 ๋ฏธ๋ง์ ๋ฒ๋ ๊ฐ์ฌ์๊ฒ 5% ๊ธ์ฌ ์ธ์
update instructor set salary = salary * 1.05 where salary < 70000;
- ํ๊ท ๋ณด๋ค ๊ธ์ฌ๊ฐ ์ ์ ๊ฐ์ฌ์๊ฒ 5% ๊ธ์ฌ ์ธ์
update instructor set salary = salary * 1.05 where salary < (select avg(salary) from instructor);
- ๊ธ์ฌ๊ฐ $100,000 ์ด์์ธ ๊ฐ์ฌ์ ๊ธ์ฌ๋ฅผ 3% ์ธ์ํ๊ณ , ๋๋จธ์ง๋ 5% ์ธ์
- ๋ ๊ฐ์
update ๋ฌธ ์์ฑ (์์๊ฐ ์ค์ํจ):update instructor set salary = salary * 1.03 where salary > 100000;
update instructor set salary = salary * 1.05 where salary <= 100000;
case ๋ฌธ์ ์ฌ์ฉํ ๋ ๋์ ๋ฐฉ๋ฒ:update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end;