Structure of SQL Queries: 두 판 사이의 차이

youngwiki
새 문서: 상위 문서: SQL ==개요== ==각주== 분류:데이터베이스 시스템
 
편집 요약 없음
 
(같은 사용자의 중간 판 3개는 보이지 않습니다)
2번째 줄: 2번째 줄:


==개요==
==개요==
일반적인 SQL 질의는 다음 형식을 갖는다.
<syntaxhighlight lang="sql">
select A1, A2, ..., An
from r1, r2, ..., rm
where P
</syntaxhighlight>
Ai는 속성(attribute)을 나타낸다. Ri는 관계(relation, 즉 테이블)을 나타낸다. P는 조건(predicate)을 나타낸다. 이때 SQL 질의의 결과는 하나의 관계(relation)이다. 즉, "테이블"을 반환한다.
==Basic Structure of SQL Queries==
===The select Clause===
'''select''' 절은 질의 결과로 원하는 속성들(attributes)을 나열한다. 이는 관계 대수(relational algebra)의 투영 연산(projection)에 해당한다. 예를 들어 모든 교수의 이름을 구하려면
<syntaxhighlight lang="sql">
select name
from instructor
</syntaxhighlight>
위와 같이 작성한다. 이때 SQL에서 이름은 대소문자를 구분하지 않는다. 이때 테이블은 튜플의 집합이기 때문에 원칙적으로는 중복된 튜플이 있을 수 없으나, SQL은 질의의 결과에서 중복된 튜플을 허용한다. 아래는 여러가지 select 문법 예제이다.
<syntaxhighlight lang="sql">
select distinct dept_name --중복을 제거하려면 select뒤에 distinct 키워드를 사용한다.
from instructor
</syntaxhighlight>
<syntaxhighlight lang="sql">
select all dept_name --중복을 제거하지 않으려면 all 키워드를 사용한다.
from instructor
</syntaxhighlight>
<syntaxhighlight lang="sql">
--instructor table을 그대로 반환한다.
select * --"*"은 모든 속성을 의미한다.
from instructor
</syntaxhighlight>
<syntaxhighlight lang="sql">
select '437' --해당 리터럴 값을 출력한다.
select '437' as colname --컬럼의 이름을 colname으로 설정한다.
</syntaxhighlight>
위 코드의 실행 결과는 아래 테이블과 같다.
{| class="wikitable"
!437
|-
|437
|}
{| class="wikitable"
!colname
|-
|437
|}
<syntaxhighlight lang="sql">
--select 절에는 산술 연산이 가능하다.
select ID, name, salary/12 as monthly_salary --salary를 12로 나눈 결과가 출력, 속성의 별칭을 따로 설정함
from instructor
</syntaxhighlight>
===The where Clause===
'''where''' 절은 결과가 만족해야 하는 조건(predicate)을 지정하며, [[Relational algebra|relation algebra]]의 선택(selection) 연산에 해당한다. 예를 들어서 컴퓨터 공학과 교수의 이름을 구하기 위해서는 아래와 같은 코드를 사용한다.
<syntaxhighlight lang="sql">
select name
from instructor
where dept_name = 'Comp. Sci.'
</syntaxhighlight>
위에서 볼 수 있들이 비교 연산자 <, <=, >, >=, =, <>를 사용할 수 있으며, 논리 연산자 and, or, not 또한  사용 가능하다. 예를 들어, 컴퓨터공학 교수 중 연봉이 70000 초과인 사람은 아래와 같은 코드를 사용한다.
<syntaxhighlight lang="sql">
select name
from instructor
where dept_name = 'Comp. Sci.' and salary > 70000
</syntaxhighlight>
===The from Clause===
'''From''' 절은 질의에 사용될 관계들(테이블들)을 나열하며, [[Relational algebra|relation algebra]]의 cartesian product 연산에 해당한다. 예를 들어 모든 instructor와 teaches의 조합이 생성하기 위해서는 아래와 같은 code를 사용한다.
<syntaxhighlight lang="sql">
--사실상 Cartesian product 연산
select *
from instructor, teaches
</syntaxhighlight>
이때 두 table 간에 공통되는 속성(예: ID)이 존재한다면, 결과로 나오는 테이블에서는 instructor.ID, teaches.ID처럼 접두사로 테이블명을 붙여 구분한다. 이때 단독으로 cartesian product를 사용하지는 않지만, where 조건과 함께 join 연산의 기초로 사용된다.
===SQL Example===
1. 어떤 강의라도 한 교수의 이름과 해당 과목의 ID를 찾기
<syntaxhighlight lang="sql">
select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID;
</syntaxhighlight>
위 질의는 instructor 테이블과 teaches 테이블을 join 연산을 한다. 이때 join 조건은 instructor.ID = teaches.ID이며, 같은 ID를 가진 교수와 강의 정보를 연결한다. 이때 가져오는 속성은 name, course_id이다.
2. Art학과 소속이면서 어떤 강의라도 한 교수의 이름과 해당 과목의 ID를 찾기
<syntaxhighlight lang="sql">
select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID
  and instructor.dept_name = 'Art';
</syntaxhighlight>
위의 join 조건은 그대로 유지하면서, instructor.dept_name = 'Art'라는 추가 조건이 붙었다.


==각주==
==각주==
[[분류:데이터베이스 시스템]]
[[분류:데이터베이스 시스템]]

2025년 4월 2일 (수) 03:39 기준 최신판

상위 문서: SQL

개요

일반적인 SQL 질의는 다음 형식을 갖는다.

select A1, A2, ..., An
from r1, r2, ..., rm
where P

Ai는 속성(attribute)을 나타낸다. Ri는 관계(relation, 즉 테이블)을 나타낸다. P는 조건(predicate)을 나타낸다. 이때 SQL 질의의 결과는 하나의 관계(relation)이다. 즉, "테이블"을 반환한다.

Basic Structure of SQL Queries

The select Clause

select 절은 질의 결과로 원하는 속성들(attributes)을 나열한다. 이는 관계 대수(relational algebra)의 투영 연산(projection)에 해당한다. 예를 들어 모든 교수의 이름을 구하려면

select name
from instructor

위와 같이 작성한다. 이때 SQL에서 이름은 대소문자를 구분하지 않는다. 이때 테이블은 튜플의 집합이기 때문에 원칙적으로는 중복된 튜플이 있을 수 없으나, SQL은 질의의 결과에서 중복된 튜플을 허용한다. 아래는 여러가지 select 문법 예제이다.

select distinct dept_name --중복을 제거하려면 select뒤에 distinct 키워드를 사용한다.
from instructor
select all dept_name --중복을 제거하지 않으려면 all 키워드를 사용한다.
from instructor
--instructor table을 그대로 반환한다.
select * --"*"은 모든 속성을 의미한다.
from instructor
select '437' --해당 리터럴 값을 출력한다.
select '437' as colname --컬럼의 이름을 colname으로 설정한다.

위 코드의 실행 결과는 아래 테이블과 같다.

437
437
colname
437
--select 절에는 산술 연산이 가능하다. 
select ID, name, salary/12 as monthly_salary --salary를 12로 나눈 결과가 출력, 속성의 별칭을 따로 설정함
from instructor

The where Clause

where 절은 결과가 만족해야 하는 조건(predicate)을 지정하며, relation algebra의 선택(selection) 연산에 해당한다. 예를 들어서 컴퓨터 공학과 교수의 이름을 구하기 위해서는 아래와 같은 코드를 사용한다.

select name
from instructor
where dept_name = 'Comp. Sci.'

위에서 볼 수 있들이 비교 연산자 <, <=, >, >=, =, <>를 사용할 수 있으며, 논리 연산자 and, or, not 또한 사용 가능하다. 예를 들어, 컴퓨터공학 교수 중 연봉이 70000 초과인 사람은 아래와 같은 코드를 사용한다.

select name
from instructor
where dept_name = 'Comp. Sci.' and salary > 70000

The from Clause

From 절은 질의에 사용될 관계들(테이블들)을 나열하며, relation algebra의 cartesian product 연산에 해당한다. 예를 들어 모든 instructor와 teaches의 조합이 생성하기 위해서는 아래와 같은 code를 사용한다.

--사실상 Cartesian product 연산
select *
from instructor, teaches

이때 두 table 간에 공통되는 속성(예: ID)이 존재한다면, 결과로 나오는 테이블에서는 instructor.ID, teaches.ID처럼 접두사로 테이블명을 붙여 구분한다. 이때 단독으로 cartesian product를 사용하지는 않지만, where 조건과 함께 join 연산의 기초로 사용된다.

SQL Example

1. 어떤 강의라도 한 교수의 이름과 해당 과목의 ID를 찾기

select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID;

위 질의는 instructor 테이블과 teaches 테이블을 join 연산을 한다. 이때 join 조건은 instructor.ID = teaches.ID이며, 같은 ID를 가진 교수와 강의 정보를 연결한다. 이때 가져오는 속성은 name, course_id이다.

2. Art학과 소속이면서 어떤 강의라도 한 교수의 이름과 해당 과목의 ID를 찾기

select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID
  and instructor.dept_name = 'Art';

위의 join 조건은 그대로 유지하면서, instructor.dept_name = 'Art'라는 추가 조건이 붙었다.

각주