3. SQL
SQL Parts
- 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) ๋ฐ ๋ทฐ์ ๋ํ ์ ๊ทผ ๊ถํ ๋ช ์ ๋ช ๋ น์ด ํฌํจ
Data Definition Language
- SQL DDL์ ๊ด๊ณ์ ๋ํ ์ ๋ณด ๋ช
์ ํ์ฉ, ๋ค์ ํฌํจ:
- ๊ฐ ๊ด๊ณ์ ์คํค๋ง
- ๊ฐ ์์ฑ(attribute)๊ณผ ๊ด๋ จ๋ ๊ฐ์ ์ ํ
- ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด
- ๊ฐ ๊ด๊ณ์ ๋ํด ์ ์ง๋ ์ธ๋ฑ์ค(index) ์งํฉ
- ๊ฐ ๊ด๊ณ์ ๋ํ ๋ณด์ ๋ฐ ๊ถํ ์ ๋ณด
- ๋์คํฌ ์์ ๊ฐ ๊ด๊ณ์ ๋ํ ๋ฌผ๋ฆฌ์ ์ ์ฅ ๊ตฌ์กฐ
DDL: Domain Types in SQL
char(n): ์ฌ์ฉ์ ์ง์ ๊ธธ์ด ์ ๊ณ ์ ๊ธธ์ด ๋ฌธ์์ดvarchar(n): ์ฌ์ฉ์ ์ง์ ์ต๋ ๊ธธ์ด ์ ๊ฐ๋ณ ๊ธธ์ด ๋ฌธ์์ดint: ์ ์ (๊ธฐ๊ณ์ ์์กดํ๋ ์ ์์ ์ ํ ๋ถ๋ถ ์งํฉ)smallint: ์์ ์ ์ (์ ์ ๋๋ฉ์ธ ์ ํ์ ๊ธฐ๊ณ ์์กด์ ๋ถ๋ถ ์งํฉ)numeric(p,d): ์๋ฆฌ์ ์ฌ์ฉ์ ์ง์ ์ ๋ฐ๋์ ์์์ ์ดํ ์๋ฆฌ๋ฅผ ๊ฐ์ง ๊ณ ์ ์์์ ์ซ์ (์:numeric(3,1)์ 44.5๋ ์ ํํ ์ ์ฅ ๊ฐ๋ฅํ๋, 444.5๋ 0.32๋ ๋ถ๊ฐ)real,double precision: ๊ธฐ๊ณ ์์กด์ ์ ๋ฐ๋๋ฅผ ๊ฐ์ง ๋ถ๋ ์์์ ๋ฐ ๋ฐฐ์ ๋ฐ๋ ๋ถ๋ ์์์ ์ซ์float(n): ์ต์ ์๋ฆฌ์ ์ฌ์ฉ์ ์ง์ ์ ๋ฐ๋๋ฅผ ๊ฐ์ง ๋ถ๋ ์์์ ์ซ์- ์ถ๊ฐ์ ์ธ ๋ด์ฉ์ 4์ฅ์์ ๋ค๋ฃธ
DDL: Create Table Construct
- SQL ๊ด๊ณ๋
create table๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ ์:create table r (A1 D1, A2 D2, ..., An Dn, (integrity-constraint1), ..., (integrity-constraintk));
- ์ ๊ด๊ณ์ ์ด๋ฆ
- ๊ฐ ๋ ๊ด๊ณ ์ ์คํค๋ง์ ์๋ ์์ฑ ์ด๋ฆ
- ๋ ์์ฑ ์ ๋๋ฉ์ธ(domain)์ ์๋ ๊ฐ์ ๋ฐ์ดํฐ ์ ํ
- ์์:
create table instructor( ID char(5), name varchar(20), dept_name varchar(20), salary numeric(8,2) );
DDL: Integrity Constraints in Create Table
- ๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด์ ์ ํ
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 );
Updates to tables
- Insert
insert into instructor values('10211', 'Smith', 'Biology', 66000);
- Delete
student๊ด๊ณ์์ ๋ชจ๋ ํํ ์ ๊ฑฐdelete from student;
- Drop Table
drop table r;
- Alter
alter table r add A D;- ์ฌ๊ธฐ์ ๋ ๊ด๊ณ ์ ์ถ๊ฐ๋ ์์ฑ์ ์ด๋ฆ์ด๊ณ ๋ ์ ๋๋ฉ์ธ
- ๊ด๊ณ์ ๋ชจ๋ ๊ธฐ์กด ํํ์ ์ ์์ฑ์ ๋ํด
null๊ฐ์ผ๋ก ํ ๋น๋จ
alter table r drop A;- ์ฌ๊ธฐ์ ๋ ๊ด๊ณ ์ ์์ฑ ์ด๋ฆ
- ์์ฑ ์ญ์ ๋ ๋ง์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ง์ํ์ง ์์
Basic Query Structure
- ์ผ๋ฐ์ ์ธ SQL ์ง์ ํํ:
select A1, A2, ..., An from r1, r2, ..., rm where P;
- ๋ ์์ฑ์ ๋ํ๋
- ๋ ๊ด๊ณ๋ฅผ ๋ํ๋
- ๋ ์ ์ด(predicate)
- ๋์ํ๋ ๊ด๊ณ ๋์(relational algebra)
- SQL ์ง์์ ๊ฒฐ๊ณผ๋ ๊ด๊ณ
The select Clause
select์ ์ ์ง์ ๊ฒฐ๊ณผ์์ ์ํ๋ ์์ฑ์ ๋์ด- ๊ด๊ณ ๋์์ ํ๋ก์ ์ (projection) ์ฐ์ฐ์ ํด๋น
- ์์: ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ
select name from instructor;
- ์ฐธ๊ณ : SQL ์ด๋ฆ์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ง ์์ (์:
NameNAMEname) - 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" ๊ฐ์ ๊ฐ์ง ๋จ์ผ ํ๊ณผ ํ๋์ ์ด์ ๊ฐ์ง ํ ์ด๋ธ
- ๋ค์์ ์ฌ์ฉํ์ฌ ์ด์ ์ด๋ฆ ๋ถ์ฌ ๊ฐ๋ฅ:
select '437' as FOO;
- ์์ฑ์
from์ ๊ณผ ํจ๊ป ๋ฆฌํฐ๋ด์ด ๋ ์ ์์select 'A' from instructor;- ๊ฒฐ๊ณผ๋ ํ๋์ ์ด๊ณผ ๊ฐ์ ํ(
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;
The where Clause
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;
The from Clause
from์ ์ ์ง์์ ๊ด๋ จ๋ ๊ด๊ณ๋ฅผ ๋์ด- ๊ด๊ณ ๋์์ ์นดํฐ์ ํ๋ก๋ํธ(Cartesian product) ์ฐ์ฐ์ ํด๋น
instructorteaches์ ์นดํฐ์ ํ๋ก๋ํธ ์ฐพ๊ธฐselect * from instructor, teaches;
- ๊ฐ๋ฅํ ๋ชจ๋
instructor-teaches์์ ์์ฑํ๋ฉฐ, ๋ ๊ด๊ณ์ ๋ชจ๋ ์์ฑ์ ํฌํจ - ๊ณตํต ์์ฑ(์:
ID)์ ๊ฒฝ์ฐ, ๊ฒฐ๊ณผ ํ ์ด๋ธ์ ์์ฑ์ ๊ด๊ณ ์ด๋ฆ์ ์ฌ์ฉํ์ฌ ์ด๋ฆ์ด ๋ณ๊ฒฝ๋จ (์:instructor.ID) - ์นดํฐ์ ํ๋ก๋ํธ๋ ์ง์ ์ ์ผ๋ก๋ ๊ทธ๋ค์ง ์ ์ฉํ์ง ์์ง๋ง,
where์ ์กฐ๊ฑด(๊ด๊ณ ๋์์ ์ ํ ์ฐ์ฐ)๊ณผ ๊ฒฐํฉํ๋ฉด ์ ์ฉ
Examples
- ์ด๋ค ๊ณผ๋ชฉ์ ๊ฐ๋ฅด์น ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ๊ณผ
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์ ์ค๋ณต์ ํ์ฉํจ
The Rename Operation
- SQL์
as์ ์ ์ฌ์ฉํ์ฌ ๊ด๊ณ์ ์์ฑ์ ์ด๋ฆ ๋ณ๊ฒฝ์ ํ์ฉ:old-name as new-name
- ๋ง์ฝ
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 Tinstructor T)
String Operations
- SQL์ ๋ฌธ์์ด ๋น๊ต๋ฅผ ์ํ ๋ฌธ์์ด ๋งค์นญ ์ฐ์ฐ์
like๋ฅผ ํฌํจํ๋ฉฐ, ๋ ๊ฐ์ ํน์ ๋ฌธ์๋ฅผ ์ฌ์ฉํ ํจํด์ ์ด์ฉ:- ํผ์ผํธ (
%): ๋ชจ๋ ํ์ ๋ฌธ์์ด๊ณผ ์ผ์น - ๋ฐ์ค (
_): ๋ชจ๋ ๋จ์ผ ๋ฌธ์์ ์ผ์น
- ํผ์ผํธ (
- ์ด๋ฆ์ "dar" ํ์ ๋ฌธ์์ด์ ํฌํจํ๋ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ
select name from instructor where name like '%dar%';
- ๋ฌธ์์ด "100%"์ ์ผ์น์ํค๊ธฐ
like '100\%' escape '\'- ์ ์์์์ ๋ฐฑ์ฌ๋์(
\)๋ฅผ ์ด์ค์ผ์ดํ ๋ฌธ์๋ก ์ฌ์ฉ (๋ค๋ฅธ ์ด๋ค ๋ฌธ์๋ ์ด์ค์ผ์ดํ ๋ฌธ์๋ก ์ฌ์ฉ ๊ฐ๋ฅ)
- ํจํด์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถ
- ํจํด ๋งค์นญ ์์:
'Intro%': "Intro"๋ก ์์ํ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น'%Comp%': "Comp"๋ฅผ ํ์ ๋ฌธ์์ด๋ก ํฌํจํ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น'_ _ _': ์ ํํ ์ธ ๋ฌธ์๋ก ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น'_ _ _ %': ์ต์ ์ธ ๋ฌธ์๋ก ๋ ๋ชจ๋ ๋ฌธ์์ด๊ณผ ์ผ์น
- SQL์ ๋ค์ํ ๋ฌธ์์ด ์ฐ์ฐ ์ง์
- ์ฐ๊ฒฐ (
||์ฌ์ฉ) - ๋์๋ฌธ์ ๋ณํ
- ๋ฌธ์์ด ๊ธธ์ด ์ฐพ๊ธฐ, ํ์ ๋ฌธ์์ด ์ถ์ถ ๋ฑ
- ์ฐ๊ฒฐ (
Ordering the Display of Tuples
- ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ์ ์ํ๋ฒณ ์์ผ๋ก ๋์ด
select distinct name from instructor order by name;
- ๊ฐ ์์ฑ์ ๋ํด ๋ด๋ฆผ์ฐจ์์
desc, ์ค๋ฆ์ฐจ์์asc๋ฅผ ๋ช ์ ๊ฐ๋ฅํ๋ฉฐ, ์ค๋ฆ์ฐจ์์ด ๊ธฐ๋ณธ๊ฐ- ์์:
order by name desc
- ์์:
- ์ฌ๋ฌ ์์ฑ์ ๋ํด ์ ๋ ฌ ๊ฐ๋ฅ
- ์์:
order by dept_name desc, name asc
- ์์:
Where Clause Predicates
- SQL์
between๋น๊ต ์ฐ์ฐ์๋ฅผ ํฌํจ - ์์: ๊ธ์ฌ๊ฐ $90,000์์ $100,000 ์ฌ์ด์ธ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ (์ฆ, $90,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';
Set Operations
- ์งํฉ ์ฐ์ฐ
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);
Aggregate Functions
- ์ด ํจ์๋ค์ ๊ด๊ณ ๋ด ๊ฐ๋ค์ ์ปฌ๋ ์
(์งํฉ ๋๋ ๋ค์ค ์งํฉ)์ ๋ํด ์๋ํ๋ฉฐ ๋จ์ผ ๊ฐ์ ๋ฐํ
avg: ํ๊ท ๊ฐ (์ซ์ ๊ฐ์๋ง ํด๋น)min: ์ต์๊ฐ (์ซ์ ๋ฐ ๋น์ซ์ ๊ฐ ๋ชจ๋ ํด๋น)max: ์ต๋๊ฐ (์ซ์ ๋ฐ ๋น์ซ์ ๊ฐ ๋ชจ๋ ํด๋น)sum: ๊ฐ์ ํฉ๊ณ (์ซ์ ๊ฐ์๋ง ํด๋น)count: ๊ฐ์ ๊ฐ์ (์ซ์ ๋ฐ ๋น์ซ์ ๊ฐ ๋ชจ๋ ํด๋น)
- SQL์์
count๋ฅผ ์ ์ธํ ์ง๊ณ ํจ์(aggregate functions)๋null๊ฐ์ ๋ฌด์ null๊ฐ์ด ๋ฌด์๋ ๊ฒฐ๊ณผ๋ก ๊ฐ๋ค์ ์ปฌ๋ ์ ์ด ๋น์ด ์์ ์ ์์- ๋ชจ๋ ์ง๊ณ ํจ์(
count์ ์ธ)๋ ๋น ์ปฌ๋ ์ ์ ์ ์ฉ๋ ๋null์ ๋ฐํ (๋น ์ปฌ๋ ์ ์count๋ 0์ผ๋ก ์ ์๋จ) countํจ์๋ ๋ค๋ฅธnon-null๊ฐ์ฒ๋ผ ๋ชจ๋null๊ฐ์ ๊ณ์ฐ
Aggregate Functions Examples
- ์ปดํจํฐ ๊ณผํ ํ๊ณผ ๊ฐ์ฌ๋ค์ ํ๊ท ๊ธ์ฌ ์ฐพ๊ธฐ
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;
Aggregate Functions โ Group By
group by์ ์ ์ฌ์ฉํ์ฌ ๋จ์ผ ํํ ์งํฉ์ด ์๋, ๊ทธ๋ฃนํ๋ ์ฌ๋ฌ ํํ ์งํฉ์ ๊ฐ๋ณ์ ์ผ๋ก ์ง๊ณ ํจ์ ์ ์ฉ ๊ฐ๋ฅ- ๊ฐ ํ๊ณผ์ ๊ฐ์ฌ ํ๊ท ๊ธ์ฌ ์ฐพ๊ธฐ
select dept_name, avg(salary) as avg_salary from instructor group by dept_name;
group by์ ์ฌ์ฉ ์, ๊ฐ ๊ทธ๋ฃน๋น ํ๋์ ํํ๋ง ์ถ๋ ฅ๋จ
Aggregation
- ์ง๊ณ ํจ์ ์ธ์
select์ ์ ์๋ ์์ฑ๋ค์group by๋ชฉ๋ก์ ๋ํ๋์ผ ํจ/* erroneous query */select dept_name, ID, avg(salary) from instructor group by dept_name;
- ํน์ ๊ทธ๋ฃน(
dept_name์ผ๋ก ์ ์๋จ)์ ๊ฐ ๊ฐ์ฌ๋ ๋ค๋ฅธ ID๋ฅผ ๊ฐ์ง ์ ์์ง๋ง, ๊ฐ ๊ทธ๋ฃน์ ๋ํด ํ๋์ ํํ๋ง ์ถ๋ ฅ๋จ
Aggregate Functions โ Having Clause
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์ ์ ๋จ์ ๊ทธ๋ฃน๋ค์ ์ฌ์ฉํ์ฌ, ์ง๊ณ ํจ์(์กด์ฌ ์)๋ฅผ ์ ์ฉํ ํ ๊ฐ ๊ทธ๋ฃน์ ๋ํ ๋จ์ผ ๊ฒฐ๊ณผ ํํ์ ์ป์
Nested Subqueries
- SQL์ ์๋ธ์ฟผ๋ฆฌ(subquery)์ ์ค์ฒฉ ๋ฉ์ปค๋์ฆ ์ ๊ณต
- ์๋ธ์ฟผ๋ฆฌ๋ ๋ค๋ฅธ ์ฟผ๋ฆฌ ๋ด์ ์ค์ฒฉ๋
select-from-whereํํ์ - ์ค์ฒฉ์ ๋ค์ SQL ์ฟผ๋ฆฌ์์ ์ํ๋ ์ ์์:
select A1, A2, ..., An from r1, r2, ..., rm where P - ๋ค์๊ณผ ๊ฐ์ด:
from์ : ๋ ์ ํจํ ์๋ธ์ฟผ๋ฆฌ๋ก ๋์ฒด ๊ฐ๋ฅwhere์ : ๋B <operation> (subquery)ํํ์ ํํ์์ผ๋ก ๋์ฒด ๊ฐ๋ฅ. ์ฌ๊ธฐ์ ๋ ์์ฑselect์ : ๋ ๋จ์ผ ๊ฐ์ ์์ฑํ๋ ์๋ธ์ฟผ๋ฆฌ(์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ, scalar subquery)๋ก ๋์ฒด ๊ฐ๋ฅ
Nested Subqueries: Set Membership
- 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 ๊ธฐ๋ฅ ์ค๋ช ๋ชฉ์ ์
Nested Subqueries: Set comparison (โsomeโ)
- ์๋ฌผํ(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');
Definition of โsomeโ Clause
F <comp> some rุจุญูุซ ุฃู- ์ฌ๊ธฐ์
<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(โต )(= some)in(!= some)์not in๊ณผ ๋์น๊ฐ ์๋
Nested Subqueries: Set comparison (โallโ)
- ์๋ฌผํ ํ๊ณผ์ ๋ชจ๋ ๊ฐ์ฌ์ ๊ธ์ฌ๋ณด๋ค ๋ ๋ง์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ๋ชจ๋ ๊ฐ์ฌ์ ์ด๋ฆ ์ฐพ๊ธฐ
select name from instructor where salary > all (select salary from instructor where dept_name = 'Biology');
Definition of โallโ Clause
F <comp> all r(5 < all {0, 5, 6})=false(5 < all {6, 10, 4})=false(5 = all {4, 5, 6})=false(5 != all {4, 6})=true(โต ์ด๊ณ )(= all)์in๊ณผ ๋์น๊ฐ ์๋(!= all)not in
Nested Subqueries: Test for Empty Relations
exists๊ตฌ๋ฌธ์ ์ธ์ ์๋ธ์ฟผ๋ฆฌ๊ฐ ๋น์ด ์์ง ์์ผ๋ฉดtrue๋ฅผ ๋ฐํexists rnot exists 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)๋ผ๊ณ ํจ
Use of โnot existsโ Clause
- ์๋ฌผํ ํ๊ณผ์์ ์ ๊ณตํ๋ ๋ชจ๋ ๊ณผ๋ชฉ์ ์๊ฐํ ๋ชจ๋ ํ์ ์ฐพ๊ธฐ
- ์ฐธ๊ณ
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) ); - ์ฒซ ๋ฒ์งธ ์ค์ฒฉ ์ฟผ๋ฆฌ๋ ์๋ฌผํ์์ ์ ๊ณตํ๋ ๋ชจ๋ ๊ณผ๋ชฉ์ ๋์ด
- ๋ ๋ฒ์งธ ์ค์ฒฉ ์ฟผ๋ฆฌ๋ ํน์ ํ์์ด ์๊ฐํ ๋ชจ๋ ๊ณผ๋ชฉ์ ๋์ด
Nested Subqueries: Test for Absence of Duplicate Tuples
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);
Subqueries in the โfromโ Clause
- 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;
Nested Subqueries: โWithโ Clause
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;
Complex Queries using โWithโ Clause
- ์ด ๊ธ์ฌ๊ฐ ๋ชจ๋ ํ๊ณผ์ ์ด ๊ธ์ฌ ํ๊ท ๋ณด๋ค ํฐ ๋ชจ๋ ํ๊ณผ ์ฐพ๊ธฐ
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;
Nested Subqueries: Scalar Subquery
- ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ๋ ๋จ์ผ ๊ฐ์ด ์์๋๋ ๊ณณ์ ์ฌ์ฉ๋๋ ์๋ธ์ฟผ๋ฆฌ
- ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ๋ ์ธ๋ถ ์ฟผ๋ฆฌ์
select์ ์์ ์ฌ์ฉ๋ ์ ์์ - ๊ฐ ํ๊ณผ์ ๊ฐ์ฌ ์์ ํจ๊ป ๋ชจ๋ ํ๊ณผ ๋์ด
select dept_name, (select count(*) from instructor where department.dept_name = instructor.dept_name) as num_instructors from department; - ์๋ธ์ฟผ๋ฆฌ๊ฐ ํ๋ ์ด์์ ๊ฒฐ๊ณผ ํํ์ ๋ฐํํ๋ฉด ๋ฐํ์ ์ค๋ฅ ๋ฐ์
Null Values
- ํํ์ด ์ผ๋ถ ์์ฑ์ ๋ํด
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๋ก ์ฒ๋ฆฌ๋จ
Modification of the Database
- ์ฃผ์ด์ง ๊ด๊ณ์์ ํํ ์ญ์
- ์ฃผ์ด์ง ๊ด๊ณ์ ์ ํํ ์ฝ์
- ์ฃผ์ด์ง ๊ด๊ณ์ ์ผ๋ถ ํํ ๊ฐ ์ ๋ฐ์ดํธ
Deletion
- ๋ชจ๋ ๊ฐ์ฌ ์ญ์
delete from instructor;
- ์ฌ๋ฌด(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); - ๋ฌธ์ ์ :
Insertion
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๋ฌธ์ ๊ทธ ๊ฒฐ๊ณผ ์ค ์ด๋ค ๊ฒ์ด๋ผ๋ ๊ด๊ณ์ ์ฝ์ ๋๊ธฐ ์ ์ ์์ ํ ํ๊ฐ๋จ
Updates
- ๋ชจ๋ ๊ฐ์ฌ์๊ฒ 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;

